-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
json start/end position implementation #4517
base: develop
Are you sure you want to change the base?
Conversation
Thanks for the effort! However, adding two I am hesitant how to continue here. |
I wonder if this is something that could be done with the data in the custom base class, so that only those that want to opt in to this behavior could enable it. That does make it a custom class, and not |
Taking that advice, I'm gonna add a new class like below to use as a json custom base class
We will use |
const parser_callback_t cb, | ||
const bool allow_exceptions_ = true, | ||
lexer_t* lexer_ = nullptr) | ||
: root(r), callback(cb), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad merge: put back std::move()
Abstract
Referring to discussion: 4455, this pull request introduces the implementation to retrieve the start and end positions of nested objects within the JSON during parsing.
Motivation
We have a service implementation with JSON schema where a field within the nested objects contains the hash value for that object. The service verifies the hash value of each of the nested objects before operating on the rest of the data sent.
For example, consider the following JSON:
Here, data_hash contains the hash of the object "details". In order to verify the data hash, we need to be able to retrieve the exact string that parsed out "details" including the spaces and newlines. Currently there is no way to achieve this using nlohmann/json parser.
Changes proposed
size_t start_position
andsize_t end_position
.Memory considerations
We considered storing substrings in the output JSON objects and sub-objects directly as well, however, considering the memory footprint increase that it would create, we opted for the option where only two size_t fields are stored per basic_json created.
Validation
We have added tests to the class_parser test suite that cover the following cases:
Since the change affects the sax_parser, for each of these test cases we validate scenarios where no callback is passed, a callback is passed that accepts all fields, and a callback is passed that filters specific fields.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.