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 -> encoder
val reset_encoder : encoder -> unit
val encoder_contents : encoder -> bytes
val encoder_contents_string : encoder -> string
val write_byte : encoder -> int -> unit

Write a single byte

val write_head : encoder -> int -> int -> unit

Write CBOR header with major type and length

val write_uint : encoder -> int -> unit

Write CBOR unsigned integer (major type 0)

val write_negint : encoder -> int -> unit

Write CBOR negative integer (major type 1)

val write_int : encoder -> int -> unit

Write CBOR integer (chooses unsigned or negative encoding)

val write_bytes : encoder -> bytes -> unit

Write CBOR byte string (major type 2)

val write_string : encoder -> string -> unit

Write CBOR text string (major type 3)

val write_array_header : encoder -> int -> unit

Write CBOR array header (major type 4)

val write_map_header : encoder -> int -> unit

Write CBOR map header (major type 5)

val write_float : encoder -> float -> unit

Write CBOR float64 (major type 7, additional 27)

val write_null : encoder -> unit

Write CBOR null

val write_undefined : encoder -> unit

Write CBOR undefined

val write_bool : encoder -> bool -> unit

Write CBOR boolean

val write_value : encoder -> Value.t -> unit

Write a complete Value.t as CBOR

Decoder

type decoder = {
  data : string;
  mutable pos : int;
  len : int;
}

Decoder state

val create_decoder : string -> decoder
val create_decoder_bytes : bytes -> decoder
val decoder_remaining : decoder -> int
val decoder_has_more : decoder -> bool
val decoder_pos : decoder -> int
val set_decoder_pos : decoder -> int -> unit
val read_byte : decoder -> int

Read a single byte

val peek_byte : decoder -> int

Peek at next byte without consuming

val read_length : decoder -> int -> int

Read CBOR length based on additional info

val read_bytes_raw : decoder -> int -> bytes

Read raw bytes from decoder

val read_string_raw : decoder -> int -> string

Read raw string from decoder

val read_float : decoder -> float

Read CBOR float64

val read_float16 : decoder -> float

Read CBOR float16 (half-precision)

val read_float32 : decoder -> float

Read CBOR float32 (single-precision)

val read_value : decoder -> Value.t

Read a complete CBOR value as Value.t

Convenience Functions

val shared_encoder : encoder
val encode : Value.t -> bytes
val encode_string : Value.t -> string
val decode : bytes -> Value.t

Decode CBOR bytes to Value.t

val decode_string : string -> Value.t

Decode CBOR string to Value.t

Low-level Access for Streaming

val read_string_only : decoder -> string

Read a CBOR string (expects major type 3)

val read_uint : decoder -> int

Read 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 option

Convert Value.t to simdjsont JSON (for JSON-compatible values only)

val json_to_value : Simdjsont.Json.t -> Value.t

Convert simdjsont JSON to Value.t

val encode_via_simdjsont : Value.t -> string option

Encode JSON-compatible Value.t using simdjsont's CBOR encoder

val decode_via_simdjsont : string -> (Value.t, string) result

Decode CBOR to Value.t using simdjsont (JSON-compatible values only)