|
DAW JSON Link
|
Use the json_raw mapping type to parse or serialize a complete JSON value as raw text. This is useful for preserving a section of a document for later or for passing that section to another JSON library.
For a string-like destination, the type must be constructible from a char const * and a std::size_t. For serialization, the supplied value must support std::begin and std::end, with iterators whose value type is char.
If raw_json contains R"({"answer":42})", it is emitted as an object rather than as an escaped JSON string:
Most JSON DOM types are not directly constructible from a character range and do not iterate over their serialized representation. A custom constructor can adapt parsing, while to_json_data can provide the other library's serialized string for output.
The following example uses placeholder names for the other library's parse and dump operations:
When parsing the following document:
ParseOtherJson receives a pointer and size covering {"answer":42}. When serializing, the string returned by dump is copied directly into the output, without surrounding quotes or escaping.
Use an owning std::tuple when the other library returns a temporary string. std::forward_as_tuple would store a reference to that temporary and leave a dangling reference after to_json_data returns.
json_raw must be callable with (char const *, std::size_t) and return the mapped C++ type.If the destination type is itself constructible from (char const *, std::size_t), omit the custom constructor:
to_json_data must be a character range accepted by std::begin and std::end; its iterator value type must be char.If the third-party type itself iterates over serialized JSON characters, it can be returned directly from to_json_data. Most DOM types instead iterate over array or object elements, so returning their dump, serialize, or equivalent string is usually required.
json_custom configured with options::JsonCustomTypes::Any also accepts and emits complete JSON values, but does so through user-supplied conversion functions. Prefer json_raw when the mapped value is already a character range containing serialized JSON. As with json_raw, serialized text is copied without validation.