sheredom / json.h

🗄️ single header json parser for C and C++
The Unlicense
710 stars 79 forks source link

Added input buffer offsets to DOM objects and a line offset buffer. #39

Closed alexshafranov closed 8 years ago

alexshafranov commented 8 years ago

As discussed in #38 I'm adding line information to DOM objects.

sheredom commented 8 years ago

Thanks for this! I'm going to show this PR to some of the other users of the lib and make sure they are ok with it (I assume they will be of course). Give me a few days or so for them to comment :smile:

Awesome work!

alexshafranov commented 8 years ago

Sure! Thanks for looking at the code

sheredom commented 8 years ago

So I've had some feedback that for some of the users that this will effect their memory budgets too highly.

But, I've actually had an idea of how to implement what you want, while retaining that the 'default' approach doesn't contain this data. TL;DR I'm working on an alternative now watch this space! :smile:

alexshafranov commented 8 years ago

Right, good to know you have an alternative plan in mind :)

Out of curiosity, are you maybe going to store json_value_s contiguous in the allocated buffer, so that offsets can be accessed by an index?

sheredom commented 8 years ago

That would be a much bigger change than I had in mind.

My current thought was just to have another struct json_value_ex_s - that contained the offset + line_no + row_no. Then have a parsing option for json_parse_ex that replaces all json_value_s' with json_value_ex_s' instead, and that way you would just need to cast the json_value_s -> json_value_ex_s to get the location information of any value.

Would that be sufficient for your use-case?

alexshafranov commented 8 years ago

Cool, that will work for my use case, except that I also added offset information to object keys in json_object_element_s. Not sure how to handle that at the moment

sheredom commented 8 years ago

Hmm. I'm guessing it is really useful to you to know where the key exactly is because that allows you to error when the key doesn't match your schema in some way?

alexshafranov commented 8 years ago

Yes, that's how I wanted to use it.

But now I'm actually thinking that key offset is probably the only thing I need. Like if the value isn't of the expected format, then the corresponding key location is reported

alexshafranov commented 8 years ago

So what if we just remove an offset field from json_value_s and keep the one in json_object_element_s? Would that be suitable for memory concerned users?

sheredom commented 8 years ago

They basically don't want any extra memory for their normal use cases, that's why I've done #40 where you just cast the value -> value_ex, and the object name string -> string_ex to get what you need! :smile:

alexshafranov commented 8 years ago

Awesome, *_ex approach should work for me!

sheredom commented 8 years ago

40 is now merged, thanks for this inspirational MR though! Really helped me understand what you needed :smile: