A small DSL for extracting-and-validating values out of a JSON object with path-aware error messages.
More...
|
| | JsonReader (nlohmann::json const &j, std::string path) |
| |
| JsonReader | at (std::string const &key) const |
| | Descend into a required sub-object.
|
| |
| bool | contains (std::string const &key) const |
| | Whether key is present on the wrapped object.
|
| |
| void | forbid_key (std::string const &key, std::string const &reason) const |
| | Throw if key is present and non-null on the wrapped object.
|
| |
| nlohmann::json const & | json () const |
| | The raw underlying JSON (escape hatch for unusual cases).
|
| |
| void | only_keys (std::initializer_list< char const * > allowed) const |
| | Assert the wrapped object contains only the given keys.
|
| |
| template<typename T> |
| T | optional (std::string const &key, T fallback) const |
| | Extract an optional typed value, falling back to fallback.
|
| |
| std::string const & | path () const |
| | The path prefix used in error messages.
|
| |
| template<typename T> |
| T | require (std::string const &key) const |
| | Extract a required typed value.
|
| |
| nlohmann::json const & | require_array (std::string const &key) const |
| | Return a const reference to a required array field.
|
| |
| std::string | require_enum (std::string const &key, std::initializer_list< char const * > allowed) const |
| | Extract a required string constrained to allowed values.
|
| |
| long long | require_int (std::string const &key, long long min=std::numeric_limits< long long >::min(), long long max=std::numeric_limits< long long >::max()) const |
| | Extract a required integer value, optionally constrained.
|
| |
| void | require_keys (std::initializer_list< char const * > keys) const |
| | Assert every key in keys is present on the wrapped object.
|
| |
| double | require_number (std::string const &key, double min=-std::numeric_limits< double >::infinity(), double max=std::numeric_limits< double >::infinity()) const |
| | Extract a required number (double), optionally constrained.
|
| |
| void | require_object () const |
| | Assert the wrapped value is a JSON object.
|
| |
| void | require_string_array (std::string const &key, bool non_empty=false) const |
| | Assert key is an array of strings, optionally non-empty.
|
| |
|
| void | expect_enum (std::string const &key, std::initializer_list< char const * > allowed) const |
| |
| void | expect_int (std::string const &key, long long min=std::numeric_limits< long long >::min(), long long max=std::numeric_limits< long long >::max()) const |
| |
| void | expect_number (std::string const &key, double min=-std::numeric_limits< double >::infinity(), double max=std::numeric_limits< double >::infinity()) const |
| |
| void | expect_array (std::string const &key) const |
| |
A small DSL for extracting-and-validating values out of a JSON object with path-aware error messages.
JsonReader wraps a nlohmann::json object and a dotted path (e.g. "config.pp"). Every extraction method throws std::runtime_error with a message that names the offending field:
"config.pp.lambda: expected number, got string" "config.pp: unexpected key 'lamda' (allowed: name, lambda)" "config.stop.min_size: must be in [2, ∞) (got 0)"
Used both at the top level (see serialization::validate_model_export) and inside every strategy's from_json. Unifies what validate_json_keys
j.at(k).get<T>() used to do piecemeal.
Scope is deliberately small:
- typed field extraction (
require<T> / optional<T>)
- enum-like string validation (
require_enum)
- numeric range validation (
require_number / require_int)
- unknown-key rejection (
only_keys)
- nested-object descent (
at)
- array access (
require_array)
The DSL is not a schema validator and cannot express composition (oneOf, allOf, recursive schemas). If you find yourself wanting those, bring in a real schema library instead of growing this file.
| long long ppforest2::JsonReader::require_int |
( |
std::string const & | key, |
|
|
long long | min = std::numeric_limits< long long >::min(), |
|
|
long long | max = std::numeric_limits< long long >::max() ) const |
Extract a required integer value, optionally constrained.
Accepts any JSON number whose value is an integer — both literals written without a fractional part (5) and equivalent numeric forms (5.0, 5e0). Non-integer fractions (5.5), non-finite numbers (NaN, Inf), and non-numeric types are rejected.
This matches JSON Schema's type: integer semantics, which is value-based: the spec (RFC 8259 §6) does not distinguish integer from non-integer JSON numbers at the format level, and JSON Schema defines integer as "any JSON number with zero fractional part." nlohmann's is_number_integer() is a parse-level tag (whether the literal had a decimal point) that's stricter than the spec — we deliberately don't inherit it. This means values round-tripped through languages that serialize all numbers uniformly (R, Python with default json.dumps) validate correctly.
- Parameters
-
| key | Field name within the wrapped object. |
| min | Inclusive lower bound (default: no lower bound). |
| max | Inclusive upper bound (default: no upper bound). |