Skip to content
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

How to reuse the parser and serializer intermediate storage? #1847

Closed
vinniefalco opened this issue Nov 20, 2019 · 8 comments
Closed

How to reuse the parser and serializer intermediate storage? #1847

vinniefalco opened this issue Nov 20, 2019 · 8 comments

Comments

@vinniefalco
Copy link

If I am parsing or serializing multiple JSON in a row, is there a way to re-use the intermediate storage used for those operations in between values?

@nlohmann
Copy link
Owner

What do you mean?

@vinniefalco
Copy link
Author

Lets rewind. Does this library allocate temporary storage when parsing a JSON?

@tlee-vertiv
Copy link

Do you expect parsing the json in-place?

@vinniefalco
Copy link
Author

No, I'm looking at the use-case of parsing a JSON into an nlohmann::basic_json (with default parameters). Maybe nlohmann doesn't allocate temporary storage, perhaps it only uses the regular stack?

@nlohmann
Copy link
Owner

The lexer (https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/input/lexer.hpp#L1481) and the parser (https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/input/parser.hpp#L487) manage their state in some member variables. It is not the state of the parsing (i.e., the stack needed to process the input), but rather the glue between input, lexer, and parser, as well as some variables to hold parsed values, error codes, etc. The actual hierarchy is managed in the SAX parser implementations (https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/input/json_sax.hpp#L317).

None of this can currently be reused.

@vinniefalco
Copy link
Author

That was very helpful thanks. The one-off variables are not interesting but these are:

std::vector<bool> states;

and

std::vector<BasicJsonType*> ref_stack {}

These could be reused if the caller desires to parse several JSON texts in a row.

@nlohmann
Copy link
Owner

Could be. Any idea how to achieve this with a nice API?

@vinniefalco
Copy link
Author

Hard to say, I would have to study the library in-depth to figure out how to do it harmoniously. You could just pack them up into a class and require the caller pass the address of it as the last parameter, or if the parameter is left out than a temporary one is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants