json
json copied to clipboard
json start/end position implementation
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:
{
"name": "foo",
"data":
{
"type": "typeA",
"value": 1,
"details": {
"nested_type": "nested_typeA",
"nested_value": 2
}
},
"data_hash": "hashA"
}
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
- Add two fields to basic_json:
size_t start_position
andsize_t end_position
. - Add a reference to the lexer in json_sax_parser to retrieve the current position in the input string.
- Whenever a BasicJsonType is created by the parser, calculate the start and end positions for that object from the original string and store those values.
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:
- Array inside an object
- Objects inside arrays
- Doubly nested objects
- String fields
- Integer and float fields
- Float values with insignificant digits
- Boolean fields
- Null fields
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.
- [x] Changes are described in the pull request, or an existing issue is referenced.
- [x] The test suite compiles and runs without error.
- [x] Code coverage is 100%. Test cases can be added by editing the test suite.
- [x] The source code is amalgamated; that is, after making changes to the sources in the
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
- The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with
#ifdef
s or other means. - Specifically, I am aware of compilation problems with Microsoft Visual Studio (there even is an issue label for this kind of bug). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
- Please refrain from proposing changes that would break JSON conformance. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
- Please do not open pull requests that address multiple issues.