Skip to content

BSON Support#2518

Merged
stephenberry merged 7 commits intomainfrom
bson
Apr 23, 2026
Merged

BSON Support#2518
stephenberry merged 7 commits intomainfrom
bson

Conversation

@stephenberry
Copy link
Copy Markdown
Owner

@stephenberry stephenberry commented Apr 20, 2026

Summary

  • Adds full BSON read/write with automatic reflection — any type that already serializes to JSON serializes to BSON without additional metadata
  • Provides helper types (glz::bson::object_id, datetime, regex, javascript, decimal128, timestamp, binary, min_key, max_key) for BSON wire types that have no native JSON equivalent
  • Adds glz::bson_to_json converter producing MongoDB Canonical Extended JSON v2, plus a skip path that advances past deprecated BSON types (undefined, DBPointer, symbol, code-with-scope) without erroring
  • Cross-format fix: JSONB, CBOR, MsgPack, and BEVE map writers now honor skip_null_members, matching the struct-writer behavior

Test plan

  • 103 BSON tests (225 asserts) covering: primitive coercion with range checks, helper-type round-trips, strict/permissive unknown-key modes, variant auto-deduction from element tag, nested documents/arrays, top-level documents AND top-level arrays, deep-nesting depth guard, exact-fill enforcement, malformed-input rejection (truncated, trailing bytes, negative length prefix, inner/outer-length mismatch for binary subtype 0x02, legacy UUID subtype 0x03)
  • Byte-exact wire-conformance tests (length-prefix computation, regex option alphabetical ordering, subtype 0x02 redundant inner length)
  • bson_to_json converter coverage for all supported types, depth-limit enforcement, and trailing-byte rejection
  • Full suite green: 91/91 tests passing — no regressions in JSON, BEVE, JSONB, CBOR, MsgPack, networking, or RPC

FNV-1a 64-bit constant 14695981039346656037ULL overflows size_t when
size_t is 32 bits. Branch on sizeof(size_t) and use the standard 32-bit
FNV-1a constants (offset 2166136261, prime 16777619) on narrow
platforms. 64-bit path is unchanged.
@packit-as-a-service
Copy link
Copy Markdown

- docs/bson.md: rewrite glz::uuid section. Old text claimed every Glaze
  format supported it and that the reader accepted subtype 0x03 for
  legacy interop; both contradict the code (only BSON has a specialization,
  and read.hpp rejects 0x03 as driver-ambiguous).
- write.hpp: drop debug-only asserts on embedded NUL in cstring keys and
  regex pattern/options. The asymmetric debug-vs-release behavior was
  confusing; a downstream read already surfaces the malformed wire as
  syntax_error, matching the JSON/BEVE pattern.
- read.hpp + bson_to_json.hpp: attach "trailing bytes after document
  terminator" to the exact-fill syntax_error so both entry points produce
  the same diagnostic.
- write.hpp: rewrite enum comment to stop implying reflect_enums is
  honored on BSON and to spell out the interop-driven reason it isn't.
- header.hpp: add constexpr to regex/javascript/binary operator== so
  the std::string/std::vector-backed helper types match the consistency
  of object_id/datetime/timestamp. All supported compilers (C++23 with
  GCC 13+, Clang 18+, current MSVC) provide constexpr std::string and
  std::vector equality.
- docs/bson.md + read.hpp: document the std::string_view target lifetime
  rule. The reader aliases into the input buffer for non-assignable
  targets, so the buffer must outlive every decoded view. Added a
  Strings section to the docs and a pointer-comment at the reader site.
- tests/bson_test: fill coverage gaps flagged in review. Added
  glz::meta-annotated struct tests (renamed keys round-trip, wire bytes
  carry the mapped names, nested meta structs) and two uint64 boundary
  tests (INT64_MAX+1 rejected with invalid_length, INT64_MAX itself
  accepted) to lock in the integer write-path contracts.
@stephenberry stephenberry merged commit 84b8d78 into main Apr 23, 2026
49 checks passed
@stephenberry stephenberry deleted the bson branch April 23, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant