(json-org) Fail on non-Object input for JSONObject instead of quietly returning empty Object - #90
Open
pjfanning wants to merge 3 commits into
Open
Conversation
…ning empty Object
`JSONArrayDeserializer` verifies it is given a START_ARRAY (added for
[datatype-json-org#15]), but `JSONObjectDeserializer` never got the equivalent
check. It reads `currentToken()` and, when it is not START_OBJECT, simply falls
through the property loop -- returning an empty `JSONObject` and leaving the
parser pointing in the middle of the value it did not consume.
That silently corrupts values rather than reporting an error:
[{"a":1},42,{"b":2}] -> [{"a":1}, {}, {"b":2}]
[{"a":1},[7,8],{"b":2}] -> [{"a":1}, {}, {}, {}]
Add the matching guard, wording it like the Array one. Entry with a
PROPERTY_NAME is still allowed, since deserializers may be invoked with the
parser already positioned inside the Object.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
JSONArrayDeserializerverifies that it was given aSTART_ARRAY(guard added for [datatype-json-org#15]), butJSONObjectDeserializernever got the equivalent check:When the current token is not
START_OBJECT, the loop simply does not run: an emptyJSONObjectis returned and the parser is left pointing in the middle of the value that was never consumed.This silently produces wrong values rather than reporting an error:
List<JSONObject>)[{"a":1},42,{"b":2}][{"a":1}, {}, {"b":2}][{"a":1},[7,8],{"b":2}][{"a":1}, {}, {}, {}]— 4 elements, all data lostThe second case desyncs the parser outright; it only surfaces at all because
FAIL_ON_TRAILING_TOKENSis enabled by default.Fix
Add the matching guard, worded like the Array one, so both deserializers behave consistently:
Entry with a
PROPERTY_NAMEis still permitted, since a deserializer may be invoked with the parser already positioned inside the Object —TypeInformationTestcontinues to pass.Tests
New
FailOnNonObjectTest(7 cases): number/string/array input, the two list cases above that previously corrupted data, plus checks that{}and well-formed Object lists still work.Full
json-orgsuite: 19 tests, all green.