git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] json_writer: new routines to create data in JSON format

From
JHJeff Hostetler <git@jeffhostetler.com>
Date
Mar 23, 2018, 20:32 UTC
Message-ID
<e4a64bc0-40eb-9285-c3b5-f034d78ffb85@jeffhostetler.com>
In-Reply-To
<306104ac-37ac-8198-1988-03efa818cde3@web.de>
On 3/23/2018 4:11 PM, René Scharfe wrote:
Show 44 quoted lines
> 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é
> 

good point. i'll revisit. thanks. Jeff

Previous: René ScharfeNext: Jeff King
Message 8 of 10 in “routines to generate JSON data”
  1. routines to generate JSON datagit@jeffhostetler.com, Mar 21, 2018
  2. json_writer: new routines to create data in JSON formatgit@jeffhostetler.com, Mar 21, 2018
  3. Junio C HamanoMar 21, 2018
  4. Jeff HostetlerMar 23, 2018
  5. René ScharfeMar 23, 2018
  6. Jeff HostetlerMar 23, 2018
  7. René ScharfeMar 23, 2018
  8. Jeff HostetlerMar 23, 2018
  9. Jeff KingMar 22, 2018
  10. Ævar Arnfjörð BjarmasonMar 22, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.