Skip to main content
The qjs:bjson module provides functions to serialize and deserialize JavaScript values using QuickJS’s internal binary format. This is useful for:
  • Saving and loading program state
  • Inter-process communication with QuickJS applications
  • Caching compiled bytecode
  • Storing complex data structures efficiently
The binary JSON format is specific to QuickJS and is not compatible with other JSON serialization formats. It should only be used for communication between QuickJS instances or for QuickJS-specific storage.

Serialization

bjson.write(obj, flags)

Serializes a JavaScript value into the QuickJS internal binary format.
any
required
The value to serialize. Can be any JavaScript value including objects, arrays, functions, and modules (with appropriate flags).
number
Optional bitwise OR of serialization flags (see below)
ArrayBuffer
ArrayBuffer containing the serialized data

Write Flags

Flags control what types of values can be serialized:

bjson.WRITE_OBJ_BYTECODE

Allows serializing functions and modules (including their bytecode).
Function bytecode serialization allows you to save compiled code and reload it without re-parsing.

bjson.WRITE_OBJ_REFERENCE

Allows serializing object references, preserving object identity.
Without WRITE_OBJ_REFERENCE, each occurrence of an object is serialized separately:

bjson.WRITE_OBJ_SAB

Allows serializing SharedArrayBuffer instances.
SharedArrayBuffer is used for sharing memory between worker threads.

bjson.WRITE_OBJ_STRIP_DEBUG

Strips debug information when serializing bytecode, reducing size.
Strip debug information when:
  • You need smaller file sizes for production deployment
  • Source code security is a concern
  • Stack traces with line numbers are not needed
Keep debug information when:
  • You need meaningful error messages with line numbers
  • You’re debugging or developing

bjson.WRITE_OBJ_STRIP_SOURCE

Strips source code when serializing bytecode.
Stripping source code prevents Function.toString() from returning the original source.

Combining Flags

Flags can be combined using bitwise OR:

Deserialization

bjson.read(buf, pos, len, flags)

Deserializes data from the QuickJS binary format back into JavaScript values.
ArrayBuffer
required
ArrayBuffer containing serialized data
number
default:0
Byte position to start reading from
number
default:"buf.byteLength"
Number of bytes to read
number
Optional bitwise OR of deserialization flags (see below)
any
The deserialized JavaScript value

Read Flags

bjson.READ_OBJ_BYTECODE

Allows deserializing functions and modules.
Only deserialize bytecode from trusted sources. Malicious bytecode could compromise security.

bjson.READ_OBJ_REFERENCE

Allows deserializing object references.
Use the same flags for both write() and read() to ensure proper serialization and deserialization.

Use Cases

Performance Considerations

Advantages of Binary JSON:
  • Preserves JavaScript types (Date, typed arrays, etc.)
  • Handles circular references (with WRITE_OBJ_REFERENCE)
  • Can serialize functions and bytecode
  • Generally faster serialization/deserialization
  • More compact for certain data types
Disadvantages:
  • QuickJS-specific format (not portable)
  • Not human-readable
  • Requires QuickJS to deserialize
When to use each:
  • Use binary JSON for QuickJS-to-QuickJS communication, state persistence, or bytecode caching
  • Use regular JSON for interoperability with other systems, debugging, or configuration files

Security Considerations

Never deserialize untrusted binary JSON data with READ_OBJ_BYTECODE flag. Malicious bytecode can execute arbitrary code.

Complete Example

Next Steps

std Module

Learn about file I/O for saving serialized data

os Module

Use with Workers for inter-thread communication

Overview

Return to stdlib overview