BSON

Parsing

Serde.SerdeBson.parse_bsonFunction
parse_bson(x::Vector{UInt8}; dict_type = Dict{String,Any}, max_depth = 1000) -> AbstractDict

Decode a BSON byte array into a dictionary without mapping to a target type.

Supported BSON types: Double (→ Float64), String (→ String), Document (→ Dict), Array (→ Vector), Binary (→ Vector{UInt8}), ObjectId (→ BSONObjectId), Boolean (→ Bool), DateTime (→ DateTime), Null (→ nothing), Regex (→ Regex), Int32/Int64 (→ Int64), Timestamp (→ BSONTimestamp), Decimal128 (→ BSONDecimal128).

Arguments

  • x::Vector{UInt8}: the raw BSON bytes. Must represent a top-level BSON document.

Keyword arguments

  • dict_type::Type{<:AbstractDict}: concrete dict type for objects (default Dict{String,Any}).
  • max_depth::Int = 1000: cap on nested-document recursion depth, which guards against stack overflow on adversarial inputs. Raise (or lower) freely; there is no hard limit.

Returns

An AbstractDict for the top-level document.

Throws

  • ParseError: if x contains invalid or unsupported BSON.

Examples

julia> bytes = to_bson(Dict("x" => 1, "y" => 2));

julia> parse_bson(bytes)
Dict{String, Any}("x" => 1, "y" => 2)

See also: from_bson, try_from_bson.

source

Deserialization

Serde.SerdeBson.from_bsonFunction
from_bson(::Type{T}, x::Vector{UInt8}) -> T
from_bson(strategy, ::Type{T}, x::Vector{UInt8}) -> T
from_bson(f::Function, x::Vector{UInt8}) -> Any

Decode a BSON byte array and deserialize it into type T.

Pass a context object strategy (e.g. CamelCase()) to apply global field-renaming.

Arguments

  • ::Type{T}: target type to construct.
  • x::Vector{UInt8}: raw BSON bytes.
  • strategy: optional context object.

Returns

A value of type T.

Throws

Examples

julia> struct Point; x::Int; y::Int; end

julia> bytes = to_bson(Point(1, 2));

julia> from_bson(Point, bytes)
Point(1, 2)

See also: try_from_bson, to_bson, parse_bson.

source

Serialization

Serde.SerdeBson.to_bsonFunction
to_bson(data) -> Vector{UInt8}
to_bson(strategy, data) -> Vector{UInt8}

Serialize data into a BSON byte array.

The top-level value is always encoded as a BSON document. Nested structs produce sub-documents. Vector{UInt8} fields are encoded as BSON binary. DateTime values use the BSON datetime type (milliseconds since Unix epoch). Regex values are encoded as BSON regex. nothing and missing are encoded as BSON null.

Pass a context object strategy (e.g. CamelCase()) to apply global field-renaming.

All serialization traits (Serde.ser_name, Serde.ser_value, Serde.ser_skip) are applied.

Arguments

  • data: the value to serialize (struct or AbstractDict).
  • strategy: optional context object.

Returns

Vector{UInt8} — the raw BSON bytes.

Examples

julia> struct Point; x::Int; y::Int; end

julia> bytes = to_bson(Point(1, 2));

julia> from_bson(Point, bytes)
Point(1, 2)

See also: from_bson.

source