Skip to content

T13b: JSON layer behind a pimpl - #11

Merged
kirre-bylund merged 3 commits into
devfrom
feat/json-pimpl
Nov 25, 2026
Merged

kirre-bylund merged 3 commits into
devfrom
feat/json-pimpl

Conversation

@kirre-bylund

Copy link
Copy Markdown
Contributor

Implements T13b: the JSON layer behind a pimpl.

D4 chose nlohmann/json behind a pimpl so the library is not part of our API. This is that pimpl — JsonValue is a public handle over an opaque implementation, and src/core/json.cpp is the only translation unit that includes the parser.

The handle exposes the shape — kind, typed reads, members, elements — and nothing about which library produced it. That keeps D4's "swap the parser" fallback a one-file change rather than a breaking change, and lets an adopter write a custom serialiser (D27) without a compile-time dependency on our choice.

Two design points worth your eye:

  • A sub-value is a path into the shared document, not a copy. operator[] on a large response is a lookup rather than a deep copy, and a JsonValue stays valid as long as any handle to it exists — which is what makes it safe to hold across a callback. The path is a sequence of keys rather than a string, so a key containing / or ~ needs no escaping and cannot be confused with a separator.
  • An absent key is Kind::Invalid, not Kind::Null. Those are different answers, and conflating them is how a decoder silently defaults a field — the failure mode D23 exists to prevent.

Parsing never throws: JSON_NOEXCEPTION plus the non-throwing overload, with is_discarded() mapped onto ErrorCode::MalformedResponse. A try/catch would not even compile under /EHs-c-.

Verification: 16 test functions. All 13 suites pass in both curl and no-curl configurations, zero warnings, both boundary checks clean. Sabotage-tested: disabling the is_discarded() check fails 18 assertions across the nine malformed-body cases.

The CI check that no public header includes the JSON library already existed and passes; only two TUs include it at all (json.cpp and config_file.cpp).

One thing deliberately not done: SerializeJson cannot rebuild an object, because the handle does not expose keys. Nothing in the SDK needs it — RawBody() is the original bytes (D7) — and adding a key iterator to the public surface to support a test helper would be the wrong trade. If T13c needs it for to_json, that is the point to revisit.

D4 chose nlohmann/json behind a pimpl so the library is not part of our API. This is
that pimpl: JsonValue is a public handle over an opaque implementation, and json.cpp is
the only translation unit that includes the parser.

The handle exposes the shape - kind, typed reads, members, elements - and nothing about
which library produced it. That is what keeps D4's "swap the parser" fallback a one-file
change rather than a breaking change, and what lets an adopter write a custom serialiser
(D27) without taking a compile-time dependency on our choice.

Two design points worth recording:

  - A sub-value is a PATH into the shared document, not a copy. operator[] on a large
    response is a lookup rather than a deep copy, and a JsonValue stays valid for as long
    as any handle to it exists - which is what makes it safe to hold across a callback.
    The path is a sequence of keys rather than a string, so a key containing '/' or '~'
    needs no escaping and cannot be confused with a separator.

  - An absent key is Kind::Invalid, not Kind::Null. Those are different answers, and
    conflating them is how a decoder silently defaults a field - which is the failure mode
    D23 exists to prevent.

Parsing never throws: JSON_NOEXCEPTION plus the non-throwing overload, with
is_discarded() mapped onto ErrorCode::MalformedResponse. A try/catch would not even
compile under /EHs-c-.

Verified by sabotage: disabling the is_discarded() check fails 18 assertions across the
nine malformed-body cases. The CI check that no public header includes the JSON library
passes, and only two TUs include it at all (json.cpp and config_file.cpp).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings November 25, 2026 07:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical and moderate issues can trigger undefined behavior, data loss, aborts, and contract violations in JSON serialization.

5 open issues
What changed in this PR

Implements T13b by adding a parser-independent JsonValue handle backed by a private nlohmann/json implementation, with parsing, typed access, nested traversal, lifetime-safe sub-values, malformed-response handling, serialization helpers, and dedicated tests.

Changes:

  • Added the public opaque JsonValue API and internal JSON implementation.
  • Integrated JSON parsing and serialization into the core library while keeping the dependency private.
  • Added CMake registration and comprehensive JSON-layer tests.
File Description
tests/​test_json.cpp Adds tests covering JSON kinds, typed reads, nested access, lifetimes, malformed input, and serialization.
tests/​CMakeLists.txt Registers and configures the JSON test executable and CTest entry.
src/​core/​json.h Declares the internal JSON parsing and serialization helpers.
src/​core/​json.cpp Implements the pimpl-backed JSON document, path-based access, typed conversions, parsing, and serialization.
include/​lootlocker/​json_value.h Defines the public parser-independent JsonValue handle API.
CMakeLists.txt Adds the JSON implementation source to the core library target.

Review effort: Lite

Comment thread src/core/json.cpp Resolved
Comment thread src/core/json.cpp Resolved
Comment thread src/core/json.cpp Resolved
Comment thread src/core/json.cpp Resolved
Comment thread src/core/json.cpp Resolved
Two were undefined behaviour, two were data loss, one was a contract violation.

  - AsInt64 and AsUInt64 cast an out-of-range double to an integer, which is undefined
    behaviour rather than a clamp. 1e100 is valid JSON and reaches that branch. Both now
    check the representable range first and saturate, so the non-representable result is
    defined rather than left to the platform.

  - SerializeJson rebuilt every number through AsDouble(), so 9007199254740993 came back
    as 9.007199254740992e+15 - silently changing valid JSON data. It also could not
    represent an object at all: an object inside an array serialised to an empty string,
    which the non-throwing parse turned into a discarded value, and dump() on a discarded
    value aborts under JSON_NOEXCEPTION.

    Both are fixed by serialising from the resolved document instead of rebuilding through
    the public surface. This is core, so it can reach the impl - and reaching for the
    public handle was the mistake, not the shortcut.

  - Size() accepted Kind::Object and reported its member count, contradicting the
    documented "arrays only" contract and disagreeing with At(), which returns invalid for
    an object.

One existing test asserted the buggy output: it expected "[1.0,2.0,3.0]" for "[1,2,3]".
That expectation was the bug, so it is corrected rather than preserved.

Verified by sabotage: restoring the object branch in Size() fails the contract test, and
routing numbers through double fails both precision assertions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread src/core/json.cpp Resolved
Comment thread src/core/json.cpp Resolved
Two owner questions on the PR, both fair.

  - "Should we not expose this granularity upwards as well?" Yes. JsonValue exposed Kind
    and the typed reads but not the integer/unsigned distinction that AsInt64 and AsUInt64
    depend on internally - so an adopter writing a from_json for a uint64 field could not
    tell an integer from a float, and would have to guess from the value. Guessing is how
    the 2^53 hazard comes back. IsInteger() and IsUnsigned() are now public.

  - "How is it getting in the source package?" It already was: the submodule is pinned to
    v3.11.3 and CMakeLists puts its single_include on core's PRIVATE path. I checked
    because the question implied it might not be, and git describe reported
    "v3.11.2-84-g9cca280a4" - which turned out to be a stale local tag list, not a real
    drift. The commit is exactly refs/tags/v3.11.3.

The third point was the substantive one: the README said nothing about the JSON layer at
all. It now documents the three things an adopter actually needs - RawBody() for a field
the SDK does not model, an ADL from_json against JsonValue for custom decoding, and the
fact that there is deliberately no parser seam, with the reason.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kirre-bylund
kirre-bylund merged commit 12868d0 into dev Nov 25, 2026
6 checks passed
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.

2 participants