Open d9k opened 3 days ago
it could be nice to serialize to "better" JSON5 too, with non-quoted identifiers where possible and trailing commas... I'll consider it!
Thanks, that would be great!
and trailing commas
It would be better if JSON5 seialization would be configurable with flags.
Personally I would love omit quotes for identifiers to make resulting .json5
file smaller but contary trailing commas would at to it's size so I was not planning to serialize with them.
I've started implementing this in this branch: https://github.com/mortie/json5cpp/tree/serialize. The API is:
struct Json5::SerializationConfig {
// Whether or not to add a trailing ',' after the last element
// of an object/array.
bool trailingCommas = true;
// Whether or not to omit quotes in keys when possible.
bool bareKeys = true;
// The string used for each level of nesting.
// Set to 'nullptr' to avoid whitespace completely.
const char *indent = "\t";
};
void Json5::serialize(
std::ostream &, const Json::Value &,
const Json5::SerializationConfig &conf = {}, int depth = 0);
For this input JSON document:
{
"name": "John Doe",
"user ID": 99,
"aliases": [
"John Smith",
"Thingum Bob"
]
}
it will produce the following JSON5 with trailingCommas=true, bareKeys=true, indent="\t"
(the defaults):
{
aliases: [
"John Smith",
"Thingum Bob",
],
name: "John Doe",
"user ID": 99,
}
With trailingCommas=false, bareKeys=true, indent=nullptr
, it produces this compact representation:
{aliases:["John Smith","Thingum Bob"],name:"John Doe","user ID":99}
And with trailingCommas=false
and bareKeys=false
, it produces valid JSON:
{
"aliases": [
"John Smith",
"Thingum Bob"
],
"name": "John Doe",
"user ID": 99
}
Does that look satisfactory or is there something that should be supported that I've missed?
You can already serialize to JSON (which is valid JSON5) with JsonCpp, using the
<<
operator. However, I guess it could be nice to serialize to "better" JSON5 too, with non-quoted identifiers where possible and trailing commas... I'll consider it!