What is Serialization?
- JSON: Human-readable, widely supported, but verbose
- Protocol Buffers: Efficient binary format by Google
- MessagePack: Binary JSON alternative
- BORSH: Deterministic binary format for blockchain
Why BORSH?
- Deterministic: The same data always produces the same binary output
- Efficient: Minimal overhead for blockchain storage
- Simple: Easy to implement in multiple languages
- Self-describing: No schema negotiation required
BORSH Specification
Primitive Types
Complex Types
[1, 2, 3] → 03 00 00 00 01 02 03
Structs: Fields serialized in order
Rust
Implementations
Rust (Reference)
Rust
JavaScript
Javascript
Elixir
Elixir
Working with NEAR Protocol
- Transaction serialization: Signing and submitting transactions
- State storage: Contract state is BORSH-encoded
- Cross-contract calls: Arguments and return values
Example: Transaction Signing
Javascript
Common Pitfalls
- Byte order: BORSH uses little-endian; don't assume big-endian
- String encoding: Always UTF-8, with 4-byte length prefix
- Optional fields: Encoded as 0 (None) or 1 + value (Some)
- Enum indices: Start at 0, must match definition order