-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose lexer, as a StAX parser #1219
Comments
So you basically just want to have public access to the |
@nlohmann Yes. AFAIK,
Thanks! |
I think I would need to see examples to better understand this. |
I need an example to understand what should be done. |
This is a feature request.
What I want to do is to find the position of an entry in a JSON file, where the description of the entry is given as a JSON pointer. I think
nlohmann/json
already contains sufficient features to do this, but unfortunately most of those features are hidden (namespace detail
) or inaccessible (private
).For example, I want to see what tokens consist the given JSON pointer, but the only exposed interface of
json_pointer
is to restore the original string that was used to build the pointer. I think it would be nice if there are token-based interfaces (builder, accessors, etc.), because besides actual accessing through a JSON object, that's what we are usually interested in when we deal with JSON pointers.Well, though not convenient, it was not very difficult to write my own tokenizer for JSON pointers. But how about JSON documents? To find out the position of a specific entry inside a JSON document, I need to parse the document, fighting against all the details of the JSON standard, unicode stuffs, error handling, etc.. I don't need the resulting DOM JSON object (indeed I can't, because the document size is quite big), and the SAX parser doesn't help neither because it just removes the byte position information except for error handling. (Personally, I prefer StAX to SAX, and I believe that's the common stance.)
It seems that the complicated jobs (unicode stuffs, number parsing, BOM's, escape syntax, input adapter, etc.) are all done by
nlohmann::detail::lexer
. The role of parser is to do those complicated jobs themselves and enable us to focus on the things that are interesting to us. Besides the loss of byte position information (which is vital if we want to work with a large JSON file directly without scanning/rewrite the whole file everytime), the SAX parser included innlohmann/json
, in its current form, is not sufficient for that goal. All it offers is just the ability to determine what to do when something comes up, which enforces us to implement a fairly complicated state machine, and callback functions full of complicated branchings.I've roughly looked at implementation details of
nlohmann::detail::lexer
, and it seems that it provides sufficient features to allow me to write my own parser. The implementation is high-quality, the interface is well-structured, well-conforming the standard, and the code is well-documented. I like it!Why not refine some interfaces and then expose it outside the
namespace detail
?The text was updated successfully, but these errors were encountered: