Allow specifying whether top level elements should be comma separated

This commit is contained in:
ktkk 2026-06-05 15:16:02 +00:00
parent 0a0a6d918d
commit e2ba1175db
10 changed files with 310 additions and 8 deletions

9
json.c
View file

@ -4,13 +4,14 @@
#define UNREACHABLE() assert(false)
Json json_init(FILE* stream, Whitespace whitespace)
Json json_init(FILE* stream, Whitespace whitespace, bool comma_separate)
{
return (Json){
.stream = stream,
.whitespace = whitespace,
.indent_level = 0,
.next_punctuation = BEGINNING,
.comma_separate = comma_separate,
};
}
@ -169,7 +170,11 @@ void begin_value(Json* json)
/// End a JSON value.
void end_value(Json* json)
{
json->next_punctuation = COMMA;
if (json->indent_level > 0 || json->comma_separate) {
json->next_punctuation = COMMA;
} else {
json->next_punctuation = BEGINNING;
}
}
/// Increment the indent level by one.