Skip to content

Flag to always write trailing commas #258

@seanmiddleditch

Description

@seanmiddleditch

Is your feature request related to a problem? Please describe.

One of JSON's largest weaknesses as a source file format (e.g. for storing game asset source data) is the prohibition on trailing commas at the end of an array or object. These create problematic merge conflicts if two different authors add an item to a list, which cannot be auto-resolved by most merge tools and cannot be safely manually merged without editing one of the lines to add the missing comma. JSON5 and other formats resolve this by allowing trailing commas.

Rather than implementing the entirety of #224 to output minimal JSON5, a fantastic intermediate step would be to allow a flag to be set which causes yyjson to always emit trailing commas.

Describe the solution you'd like

A new YYJSON_WRITE_TRAILING_COMMAS write flag. If set, yyjson would always emit a trailing comma after the last item in an array or object, perhaps only in pretty mode.

if I'm understanding the code correctly, this might just be putting the two lines starting at

cur -= 2;
behind a condition on the trailing commas flag being unset? It looks like a trailing comma+newline is normally always written and then that code removes those and replaces with just a newline.

Describe alternatives you've considered

A flag could be set on a per-array/-object basis for more control. I do not myself see much need for that, however, certainly not enough to justify eating up one of the few remaining reserved bits in the tag.

Additional context

This would pair nicely with #257 to make yyjson more ideal for writing human-centric config files or source data asset files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions