daw::json::to_json
allows optional policy flags to control the output. The defaults are specified below under Format Policy
Flags. All the enum's below are in the daw::json::options
namespace
The example below shows output with the defaults and with Pretty formatting enabled. To see a working example using this code, refer to cookbook_output_flags1_test.cpp
struct OutputFlags1 {
std::string s;
std::vector<int> ary;
};
namespace daw::json {
template<>
struct json_data_contract<OutputFlags1> {
static constexpr char const s[] = "s";
static constexpr char const ary[] = "ary";
using type = json_member_list<json_string<s>, json_array<ary, int>>;
static inline auto to_json_data( OutputFlags1 const &v ) {
return std::forward_as_tuple( v.s, v.ary );
}
};
} // namespace daw::json
int main( ) {
using namespace daw::json::options;
auto const v = OutputFlags1{ "Hello", { 1, 2, 3 } };
puts( daw::json::to_json( v ).c_str( ) );
puts( daw::json::to_json( v, output_flags<SerializationFormat::Pretty> ).c_str( ) );
}
The program will output
{"s":"Hello","ary":[1,2,3]}
{
"s": "Hello",
"ary": [
1,
2,
3
]
}
Enable or disable formatted output
Minified
- Output the minimal JSON documentPretty
- Use newlines and indentation to format JSON document
Minified
Control how the file is indented
Tab
- use tab character for indentationSpace1
- Indent 1 spaceSpace2
- Indent 2 spacesSpace3
- Indent 3 spacesSpace4
- Indent 4 spacesSpace5
- Indent 5 spacesSpace8
- Indent 8 spacesSpace10
- Indent 10 spaces :) https://youtu.be/SsoOG6ZeyUI
Space2
Allow for a JSON document that is ASCII only and each byte has a value <= 0x7F
None
- Do not restrict to 7bit ASCIIOnlyAllow7bitStrings
- Restrict all string member values and all member names to 7bits, escaping all values >= 0x7F.ErrorInvalidUTF8
- Throw an error when invalid UTF8 is encountered
ErrorInvalidUTF8
Change how newlines are delimited
n
- Use\n
to delimit newlinesrn
- Use\r\n
to delimit newlines
n
With arrays/classes output optionally output a trailing comma
No
- Do not output trailing comma's. This is conformantYes
- Add non-conformant trailing commas to array elements and class members
No