Replies: 1 comment 1 reply
-
This sounds cool! Yes, please do! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am working on a project which requires iterating into the whole JSON document as a tree, I find that others had the same need (see in example #3469) and I need an efficient solution (meaning that, in example, flattening the whole JSON document is not an option in my use-case scenario).
I am thus writing a "tree_iterator", which can be seen as both an iterator and a json_pointer (it is actually a subclass of json_pointer but also meets the requirements of an STL iterator), usable like:
Which will produce from '{"a":{"b":{"c":[1,2,3]}},"x":"some"}' something like:
The template supports three iteration modes: "pre" which iterates also over objects and arrays and returns the object/array before its contents (preorder), "post" which does postorder and "in" which iterate only in leaf values (the closer reasonable thing to "inorder" for a non-binary tree).
So on the same json:
Will produce:
And, to say, flatten could be rewritten as:
All methods will be at most O(d * log s), where d is the depth of the "tree" and s is the size of objects; the "tree_iterator" contains simply a JSON pointer and a reference to the JSON object itself, no recursion, the class fails gracefully if the JSON is modified invalidating the pointer.
The code would take some benefit by replacing the escape/unescape functions with linear ones (are currently O(square) for keys like "~~~~~~~~~~"), which I already rewrote and will gladly contribute.
I can integrate this class either as a separate project or by making it an addition to the library; I am wondering if it can be useful to propose it as a PR, as in the second case I will have my project use a fork until the PR kicks in.
So my question is: do you think this "tree iterator" could be a contribution of general benefit to the library?
Thanks,
A.
Beta Was this translation helpful? Give feedback.
All reactions