|
DAW JSON Link
|
json_submember<Name, JsonPath, JsonMember> maps one C++ value to a value below a named JSON object or array member. JsonPath is relative to Name and uses the same dot and square-bracket syntax as the path overloads of from_json.
For example, this JSON stores reading below two object members and the first element of an array.
The mapping selects only the integer at payload.readings[0].value.
Parsing finds payload, follows the relative path, and passes the selected JSON value to the mapping for JsonMember. JsonMember must be unnamed; it can be an explicit mapping such as json_string_no_name<std::string> or a type such as int for which an unnamed mapping can be deduced.
Serialization reconstructs the minimal hierarchy described by the path.
This is a projection rather than preservation of the input document. Object members and array elements not represented by the mapping are discarded during parsing and cannot be recreated by to_json.
The following invariants apply:
JsonPath must not be empty.JsonMember must be an unnamed JSON mapping or have an unnamed deduced mapping.Name and the complete relative path must exist while parsing. A nullable JsonMember does not make a missing path optional.json_data_contract must not contain multiple json_submember mappings with the same outer Name.[0] at every array step. Any other index would require inventing preceding elements and is rejected.An invalid or mismatched container step reports ErrorReason::InvalidJSONPath. A well-formed path that is absent reports ErrorReason::JSONPathNotFound. Attempting to serialize a path containing an array index other than exactly [0] reports ErrorReason::OutputError through the configured JSON error handler.
In C++17, declare both names as static character arrays and pass those arrays as the first two template arguments. C++20 and later permit the string-literal form shown above.
A complete parsing, serialization, and error-handling example is exercised by test_json_submember.cpp.