akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 809 forks source link

Iterate over a const json_t * object #578

Closed nscnd closed 3 years ago

nscnd commented 3 years ago

Hi there,

First let me start by the fact that I love this library and it's simplicity. But the thing I find myself missing is a way to iterate over the key/value of a const json_t * object. json_object_foreach expands to json_object_iter which doesn't take a const json_t * Is there a way to acheive this ? Is this feature missing ? Is it by design ?

Thanks for any help on this matter.

akheron commented 3 years ago

Well, it's kind of by design. If you iterate over an object, you can use json_object_iter_set to modify the object via the iterator, or json_object_iter_value to grab a non-const json_t * to modify a value contained in the object. This could perhaps be circumvented by adding a separate "constant iterator" type and iteration functions that work with constant iterators.

nscnd commented 3 years ago

Thanks for your answer, it make senses when you put it like that. If I find the time I'll try to cook something up along those lines. Cheers,