skeeto / pdjson

C JSON parser library that doesn't suck
The Unlicense
281 stars 36 forks source link

json_peek() changes the value returned by json_get_position() #15

Open acuozzo opened 5 years ago

acuozzo commented 5 years ago

json_peek() seems as though it should be non-destructive, but since it calls json_next() it changes the value returned by json_get_position().

It similarly changes the value returned by json_get_string() and friends.

skeeto commented 5 years ago

Yup, this is true, and your expectations for json_peek() are reasonable.

boris-kolpackov commented 4 years ago

This may not be the expected behavior but perhaps it's the simplest? The two alternative semantics that come to mind are:

  1. json_get_*() return values corresponding to the last json_next() event and there is no way to get values for the peeked event.

  2. As the first option but there is an additional API to get the values corresponding to the peeked event (e.g., a parallel json_peek_*() family of functions).

The first option will be simpler to implement but it will be a limitation compared to the current semantics (which allows one to retrieve both values). I believe the second option will lead to quite a bit of additional complexity (second set of values, their management, etc).

So perhaps the current semantics is not that bad? Either way, it would be helpful to clarify what the future approach will be (I am currently working on a JSON parser for C++ that is based on pdjson and it would be helpful to know if I can rely on the current semantics).