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
Serialization
bjson.write(obj, flags)
Serializes a JavaScript value into the QuickJS internal binary format.
The value to serialize. Can be any JavaScript value including objects, arrays, functions, and modules (with appropriate flags).
Optional bitwise OR of serialization flags (see below)
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.
What are object references?
What are object references?
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.
When should I strip debug info?
When should I strip debug info?
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
- You need meaningful error messages with line numbers
- You’re debugging or developing
bjson.WRITE_OBJ_STRIP_SOURCE
Strips source code when serializing bytecode.
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 containing serialized data
Byte position to start reading from
Number of bytes to read
Optional bitwise OR of deserialization flags (see below)
The deserialized JavaScript value
Read Flags
bjson.READ_OBJ_BYTECODE
Allows deserializing functions and modules.
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
Saving Application State
Saving Application State
Bytecode Caching
Bytecode Caching
Worker Communication
Worker Communication
Performance Considerations
Binary JSON vs Regular JSON
Binary JSON vs Regular JSON
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
- QuickJS-specific format (not portable)
- Not human-readable
- Requires QuickJS to deserialize
- 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
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