Skip to content

Conversation

@sk02241994
Copy link
Contributor

#1003

Add support for JSON to POJO deserialization via JSONBuilder

Introduced JSONBuilder class:
Provides customizable mappings for type conversions (Function<Object, ?>) and collection instantiation (Supplier<?>), enabling extensible JSON deserialization logic.

Enhanced JSONObject:
Added support for converting JSON into Java POJOs using a new method: fromJson(Class).
Supports nested objects, collections (e.g., List), and custom type mappings via JSONBuilder.
Optional JSONBuilder can be passed via constructor or setter.
Added comprehensive unit tests in JSONObjectTest:
Covers deserialization of primitive types, nested classes, collections, and parameterized generics (e.g., List<List>).
Includes tests for custom type mapping (e.g., LocalDateTime).

@stleary
Copy link
Owner

stleary commented Sep 10, 2025

@sk02241994 I won't have time to review in depth until later this week. My initial take is that fromJson() is long overdue, glad to see someone submit an implementation, thanks for that! For now, it will be better to get everything working and released with just the default options, so JSONBuilder can wait for a future PR. Also, all of the new SonarQube issues will need to be addressed, or marked false-positive if appropriate.

public <T> T fromJson(Class<T> clazz) {
try {
T obj = clazz.getDeclaredConstructor().newInstance();
if (this.builder == null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per earlier comment, please omit JSONBuilder from the initial implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stleary Can you give me an idea on how you want that implementation to be done?
I went this route since there are some types like LocalDate/Time or UUID etc, which require some customization or processing before being converted.
Or are we avoiding converting to those types for now?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sk02241994 Sure, Take the behavior of a default JSONBuilder instance and hardcode it in the fromJson() functionality. Ideally, you might be able to make a case for keeping JSONBuilder, but removing it from the API for now. But only do this if you can keep the new code as easy to understand as possible. Right now, I don't think that is the case. More inline comments might help a bit. Developers often have difficulty commenting their code. A useful way to think of it is to imagine you are in a code review, and you are asked to explain why you did something a certain way, or what is the purpose of a class or local variable. Then just turn your explanation into a comment.

assertEquals(customClass, compareClass);
}

public static class CustomClass {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each CustomClass* should be in its own file in the org.json.junit/data directory

@sk02241994 sk02241994 force-pushed the feature-1003 branch 2 times, most recently from 4f67189 to f0b7d1a Compare September 28, 2025 10:15
@sk02241994
Copy link
Contributor Author

@stleary Sorry for the delay.
I have updated the to include docs and also solved the SonarQube issues (Some of those are false positives)
Also added methods to handle Enum and Map (They were missing in previous commit).

@stleary
Copy link
Owner

stleary commented Sep 29, 2025

@sk02241994 Can you do the implementation without adding any new classes? I understand that this will limit the types that fromJson() can support. The JavaDocs for the new methods should explain the limitations.

@sk02241994
Copy link
Contributor Author

@stleary Sorry for the delay. I have updated the code to deserialize limited number of classes. Only those which are present in the JSONObject can be deserialized. Since we need to TypeConverter and InstanceCreatory interface, I am keeping those but changing the access type to be default, so no one can access those.

@stleary
Copy link
Owner

stleary commented Oct 18, 2025

@sk02241994 The code cannot be accepted in its current form. fromJson() can be implemented without the new classes TypeConverter and InstanceCreator, while retaining all of the required functionality. This will make the code cleaner and easier to maintain. Let me know if you need some suggestions on how to do this.

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
D Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@sk02241994
Copy link
Contributor Author

@stleary I have updated the code there are some sonar issues which are irrelevant to the code changes I have made.

@stleary
Copy link
Owner

stleary commented Oct 28, 2025

Thanks @sk02241994, the changes look good, and I should be able to complete the review today.

@stleary
Copy link
Owner

stleary commented Oct 29, 2025

What problem does this code solve?
Implement fromJson() for JSONObject

Does the code still compile with Java6?
Yes

Risks
Low

Changes to the API?
2 new API methods are added

Will this require a new release?
No

Should the documentation be updated?
No

Does it break the unit tests?
No, new unit tests were added

Was any code refactored in this commit?
No

Review status
APPROVED

Starting 3-day comment window

@stleary stleary removed the In review label Oct 29, 2025
@stleary stleary merged commit 25f355a into stleary:master Oct 31, 2025
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants