serialize
Serialize JSON-shaped data into compact binary using msgpack or a simplified protobuf encoder. Input is always a JSON string and binary output is returned as a base64 string. Import it with import serialize.
serialize.msgpack
| Function | Signature | Description |
|---|---|---|
encode | serialize.msgpack.encode(json: string) → string | Encode a JSON string to base64-wrapped msgpack |
decode | serialize.msgpack.decode(data: string) → string | Decode base64 msgpack back into a JSON string |
size | serialize.msgpack.size(json: string) → int | Byte count of the msgpack encoding (0 on invalid JSON) |
serialize.protobuf
A simplified encoder backed by msgpack internally, since true protobuf requires schemas.
| Function | Signature | Description |
|---|---|---|
encode | serialize.protobuf.encode(json: string) → string | Encode a JSON string to a base64 binary string |
decode | serialize.protobuf.decode(data: string) → string | Decode a base64 binary string back into a JSON string |
Example
import serialize
let payload = "{\"id\":7,\"name\":\"widget\"}"
// compact binary round-trip
let packed = serialize.msgpack.encode(payload)
print(serialize.msgpack.size(payload)) // encoded byte count
print(serialize.msgpack.decode(packed)) // {"id":7,"name":"widget"}