From: René Scharfe Date: Fri, 23 Mar 2018 20:11:26 GMT Subject: Re: [PATCH v2] json_writer: new routines to create data in JSON format Message-ID: <306104ac-37ac-8198-1988-03efa818cde3@web.de> In-Reply-To: <14bbf223-a4d1-3c11-cd2e-dff044e01fef@jeffhostetler.com> Am 23.03.2018 um 20:55 schrieb Jeff Hostetler: >>> +struct json_writer_level >>> +{ >>> +    unsigned level_is_array : 1; >>> +    unsigned level_is_empty : 1; >>> +}; >>> + >>> +struct json_writer >>> +{ >>> +    struct json_writer_level *levels; >>> +    int nr, alloc; >>> +    struct strbuf json; >>> +}; >> >> A simpler and probably more compact representation of is_array would >> be a strbuf with one char per level, e.g. '[' for an array and '{' >> for an object (or ']' and '}'). >> >> I don't understand the need to track emptiness per level.  Only the >> top level array/object can ever be empty, can it? > > My expectation was that any sub-object or sub-array could be empty. > That is, this should be valid (and the JSON parser in Python allows): > >     {"a":{}, "b":[], "c":[[]], "d":[{}]} Sure, but the emptiness of finished arrays and objects doesn't matter for the purposes of error checking, comma setting or closing. At most one of them is empty *and* unclosed while writing the overall JSON object -- the last one opened: { {"a":{ {"a":{}, "b":[ {"a":{}, "b":[], "c":[ {"a":{}, "b":[], "c":[[ {"a":{}, "b":[], "c":[[]], "d":[ {"a":{}, "b":[], "c":[[]], "d":[{ Any of the earlier written arrays/objects are either closed or contain at least a half-done sub-array/object, which makes them non-empty. René