Basic Usage (Browser / WASM)¶
This guide covers converting between JSON and Protobuf in the browser. Always
await init() once before any call.
Core Functions¶
| Function | Description |
|---|---|
init() (default export) |
Load & instantiate the .wasm (call once) |
compileProtoFromSources() |
Compile .proto sources held in memory |
jsonToProtobuf() |
Convert a JSON string to Protobuf bytes |
protobufToJson() |
Convert Protobuf bytes to a JSON string |
DescriptorCache |
Pre-decoded pool for high-throughput conversion |
No compileProto(path)
There is no filesystem in the browser, so the disk-based compileProto is not exposed.
Compile from in-memory sources, or load a pre-compiled descriptor's bytes.
All binary values are Uint8Array. Every function throws on error.
Compiling¶
const descriptor = compileProtoFromSources({ "schema.proto": "syntax=\"proto3\"; ..." }, "schema.proto");
See Proto Files & Compilation for imports and well-known types.
JSON → Protobuf¶
jsonStr: string,descriptorBytes: Uint8Array,messageType: string- returns
Uint8Array
Protobuf → JSON¶
protobufBytes: Uint8Array,descriptorBytes: Uint8Array,messageType: string,pretty: boolean- returns
string
Supported Protobuf Features¶
Scalar types¶
message User { string name = 1; int32 age = 2; int64 id = 3; double score = 4; bool active = 5; bytes data = 6; }
JSON.stringify({ name: "Alice", age: 30, id: 123456789, score: 95.5, active: true, data: "base64==" });
Nested messages¶
message Address { string street = 1; string city = 2; }
message User { string name = 1; Address address = 2; }
Enums¶
On input, enums accept the name or the number; on output they are emitted as the numeric value:
enum Priority { LOW = 0; MEDIUM = 1; HIGH = 2; }
message Task { string title = 1; Priority priority = 2; }
jsonToProtobuf('{"title":"x","priority":"HIGH"}', d, "Task"); // name in
JSON.parse(protobufToJson(wire, d, "Task", false)).priority; // 2 (number out)
Repeated fields & maps¶
Oneof fields¶
Set exactly one arm; only the set field appears on output.
Default values¶
proto3 defaults (empty string, 0, false, enum 0, empty list/map) are omitted from the
output JSON.
Error Handling¶
Failures throw a standard Error:
try {
jsonToProtobuf("not json", descriptor, "user.User");
} catch (err) {
console.error((err as Error).message); // "Invalid JSON: ..."
}
Next Steps¶
- Proto Files & Compilation
- Advanced Features —
DescriptorCache, Web Workers, security, Zod - API Reference