Crdt.Cbor_simd
crdt · API reference
CBOR with simdjsont integration for Value.t encoding/decoding
CBOR encoding/decoding using simdjsont with SIMD acceleration.
This module provides CBOR support for Value.t, extending simdjsont's CBOR capabilities with byte string support (major type 2) and CBOR-specific simple values (undefined).
Encoding uses simdjsont's optimized CBOR encoder where possible. Decoding uses simdjsont's SIMD-accelerated CBOR parser.
Encoder
type encoder = {
mutable buf : Buffer.t;
}Encoder state using Buffer for efficiency
val create_encoder : ?capacity:int -> unit -> encoderval reset_encoder : encoder -> unitval encoder_contents : encoder -> bytesval encoder_contents_string : encoder -> stringval write_byte : encoder -> int -> unitWrite a single byte
val write_head : encoder -> int -> int -> unitWrite CBOR header with major type and length
val write_uint : encoder -> int -> unitWrite CBOR unsigned integer (major type 0)
val write_negint : encoder -> int -> unitWrite CBOR negative integer (major type 1)
val write_int : encoder -> int -> unitWrite CBOR integer (chooses unsigned or negative encoding)
val write_bytes : encoder -> bytes -> unitWrite CBOR byte string (major type 2)
val write_string : encoder -> string -> unitWrite CBOR text string (major type 3)
val write_array_header : encoder -> int -> unitWrite CBOR array header (major type 4)
val write_map_header : encoder -> int -> unitWrite CBOR map header (major type 5)
val write_float : encoder -> float -> unitWrite CBOR float64 (major type 7, additional 27)
val write_null : encoder -> unitWrite CBOR null
val write_undefined : encoder -> unitWrite CBOR undefined
val write_bool : encoder -> bool -> unitWrite CBOR boolean
val write_value : encoder -> Value.t -> unitWrite a complete Value.t as CBOR
Decoder
type decoder = {
data : string;
mutable pos : int;
len : int;
}Decoder state
val create_decoder : string -> decoderval create_decoder_bytes : bytes -> decoderval decoder_remaining : decoder -> intval decoder_has_more : decoder -> boolval decoder_pos : decoder -> intval set_decoder_pos : decoder -> int -> unitval read_byte : decoder -> intRead a single byte
val peek_byte : decoder -> intPeek at next byte without consuming
val read_length : decoder -> int -> intRead CBOR length based on additional info
val read_bytes_raw : decoder -> int -> bytesRead raw bytes from decoder
val read_string_raw : decoder -> int -> stringRead raw string from decoder
val read_float : decoder -> floatRead CBOR float64
val read_float16 : decoder -> floatRead CBOR float16 (half-precision)
val read_float32 : decoder -> floatRead CBOR float32 (single-precision)
val read_value : decoder -> Value.tRead a complete CBOR value as Value.t
Convenience Functions
val shared_encoder : encoderval encode : Value.t -> bytesval encode_string : Value.t -> stringval decode : bytes -> Value.tDecode CBOR bytes to Value.t
val decode_string : string -> Value.tDecode CBOR string to Value.t
Low-level Access for Streaming
val read_string_only : decoder -> stringRead a CBOR string (expects major type 3)
val read_uint : decoder -> intRead CBOR unsigned integer
simdjsont Integration
For JSON-compatible CBOR values, we can leverage simdjsont's SIMD-accelerated parsing. This section provides conversion functions.
val value_to_json : Value.t -> Simdjsont.Json.t optionConvert Value.t to simdjsont JSON (for JSON-compatible values only)
val json_to_value : Simdjsont.Json.t -> Value.tConvert simdjsont JSON to Value.t
val encode_via_simdjsont : Value.t -> string optionEncode JSON-compatible Value.t using simdjsont's CBOR encoder
val decode_via_simdjsont : string -> (Value.t, string) resultDecode CBOR to Value.t using simdjsont (JSON-compatible values only)