Repository navigation
Conversation
The fromJson(Class) Javadoc claimed that only classes "explicitly supported and registered within the JSONObject context" can be deserialized, and declared an IllegalArgumentException for classes outside that set. No such registry exists: fromJson instantiates any class with a no-arg constructor and writes every non-static declared field whose name matches a JSON key, widening access with setAccessible(true). Private and transient fields are populated, and JSONPropertyIgnore does not apply because it only suppresses the bean to JSONObject direction. The binding rules are already released, so changing them here would break existing users. Instead document what actually happens, point out that binding is driven by whatever keys the JSON contains, and replace the non-existent IllegalArgumentException with the JSONException the method really throws. Adds JSONObjectTest#jsonObjectParseFromJson_10 to pin the current binding behavior so the documentation cannot drift from the implementation again. Fixes stleary#1076
|
This branch has not been deployed
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.



Description
Fixes #1076.
The
fromJson(Class)Javadoc documented a protection that was never implemented:There is no registry anywhere in the source. The method calls
setAccessible(true)on every non-static declared field and writes it when its name matches a key in the JSON, soprivate,transientand@JSONPropertyIgnore-exposed fields are all populated from whatever keys the input contains. The@throws IllegalArgumentExceptionline was wrong too: nothing in the method throws it, failures surface asJSONException.Since the binding rules themselves are already released, this PR leaves them untouched, per #1076 (comment), and only makes the Javadoc match reality:
@throws IllegalArgumentExceptionis replaced with theJSONExceptionthe method throws when the class has no accessible no-arg constructor or a field cannot be setfromJson(String, Class)now links to those rules instead of silently inheriting themTests
JSONObjectTest#jsonObjectParseFromJson_10binds a bean with a privateadmin, a privatetenantId, a transientsessionKeyand asuperAdminhidden behind an@JSONPropertyIgnoregetter. It asserts all four are populated, which is the behavior the Javadoc now describes. This is a characterization test: it passes before and after the change, and exists so the documentation cannot drift from the implementation again without a failing test.mvn javadoc:javadocsucceeds, so the new{@link}references and HTML are valid.No behavior change.