Strict size checks for conversions to fixed size containers#2558
Strict size checks for conversions to fixed size containers#2558domsoll wants to merge 6 commits into
Conversation
…ion of fixed size containers from json MNT: updated amalgamation header
ENH: test cases for std::pair and std::tuple added
ENH: more test cases for c arrays and std::array added MNT: redundant array type check for std::array conversion reverted MNT: updated amalgamation header
|
The change will break existing code, so it cannot be introduced in the 3.x.x versions. I would propose to guard the test/throwing code with a preprocessor symbol so that the feature can be switched on if needed. What do you think? |
…disabled ENH: test cases adapted for strict size checks option MNT: updated amalgamation header
|
Thank's for your input, you are right, an introduction in 3.x.x is problematic, I guess the preprocessor symbol is the best option then. The behavior without the strict container size option should now be unchanged, including the type of exceptions thrown in case of errors. |
|
@domsoll Can you please update to the latest |
|
(Related: #3010) |
# Conflicts: # CMakeLists.txt # include/nlohmann/detail/conversions/from_json.hpp # include/nlohmann/detail/macro_scope.hpp # single_include/nlohmann/json.hpp
|
The pull request is now updated to the latest |
|
Thanks! Can you please run the amalgamation (make amalgamate)? |
|
I did update amalgamation before with make amalgamate. Anyway, I reverted the brace placement manually again, so the check should hopefully pass now. |
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
The current implementation accepts conversion of JSON arrays to fixed size containers like std::array, std::pair or std::tuple, as long as there are sufficient elements in the JSON array to fill the container.
The conversion only implicitly fails if there are to few elements via the out of range exception when trying to access further elements.
I suggest to strictly check for matching JSON array size when converting to fixed size containers instead.
In my opinion a mismatch in size should be considered a type error, as in the sense, that i.e. a std::array<int, 4> is a different type than a std::array<int, 5>.
Silently discarding extra JSON array elements might hide problems with mismatching serialized data and result in unexpected behavior.
If a fixed size container is extracted, it explicitly expresses that a certain number of elements is expected, if this expectation is not met by the JSON object, throwing a type error seems to be the safest way to handle the situation.
Though according to the test cases, the current behavior seems at least partially intentional, so I guess, this is up for debate, which approach is preferable.
Anyway, the commits in this pull request add strict size checks for the conversion to std::array, std::pair, std::tuple and c-style arrays.
There are also explicit JSON type checks for more expressive exceptions when the JSON type is not an array at all.
Test cases are adapted accordingly and some extra test cases are added to check for the new size restrictions.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmanndirectory, runmake amalgamateto create the single-header filesingle_include/nlohmann/json.hpp. The whole process is described here.Please don't
#ifdefs or other means.