Repository navigation
T13b: JSON layer behind a pimpl - #11
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical and moderate issues can trigger undefined behavior, data loss, aborts, and contract violations in JSON serialization.
5 open issues
Check signed integer range before casting floating-point values · New Check unsigned integer range before casting floating-point values · New Handle unsupported array elements before pushing them into JSON · New Return zero from Size() for objects · New Preserve integer precision when serializing JSON numbers · New
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
JsonValueAPI 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
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>
kirre-bylund
commented
Nov 25, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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 —
JsonValueis a public handle over an opaque implementation, andsrc/core/json.cppis 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:
operator[]on a large response is a lookup rather than a deep copy, and aJsonValuestays 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.Kind::Invalid, notKind::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_NOEXCEPTIONplus the non-throwing overload, withis_discarded()mapped ontoErrorCode::MalformedResponse. Atry/catchwould 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.cppandconfig_file.cpp).One thing deliberately not done:
SerializeJsoncannot 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 forto_json, that is the point to revisit.