Skip to content

Conversation

@MukjepScarlet
Copy link
Contributor

Purpose

Like Java 9 and Guava.

Description

closes #2923

Checklist

  • New code follows the Google Java Style Guide
    This is automatically checked by mvn verify, but can also be checked on its own using mvn spotless:check.
    Style violations can be fixed using mvn spotless:apply; this can be done in a separate commit to verify that it did not cause undesired changes.
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Thanks for the pull request! To me this looks quite reasonable and the implementation looks good1, but let's see what the project members think about this.

Footnotes

  1. It will need unit tests though, but might be good to wait for the feedback from the project members, to avoid wasted work.

* @return a new JsonObject.
* @since $next-version$
*/
public static JsonObject of(
Copy link
Contributor

Choose a reason for hiding this comment

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

These of overloads make it a bit verbose, though I guess there is no better way at the moment and Guava and the JDK do it like this as well.

The other alternative would be to only have maybe of 1-3 and for the rest require the user to use a different approach, e.g. the above mentioned of(Map).

Would be good to wait for the opinion of one of the project members.

Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Thanks for the changes!

I think you slightly need to adjust the types so that subtypes of JsonElement don't cause issues. See comments.

@MukjepScarlet
Copy link
Contributor Author

@Marcono1234 Can we use some codegen technique to generate factory methods for them? I don't know if we can modify an existing class, for example, adding a static method.

@Marcono1234
Copy link
Contributor

Which factory methods are you referring to?

Sorry if my previous comment was a bit confusing; I just meant that the methods you added should use ? extends JsonElement (as shown in the individual comments), but nothing else.

Or do you want to use code generation to generate the JsonObject#of methods automatically to avoid the code duplication? In my opinion the code duplication there is ok since it is quite simple (it is not ideal but there is maybe no better solution). Setting up code generation just for that does not seem to justify the additional complexity.

MukjepScarlet and others added 7 commits October 29, 2025 20:04
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Copy link
Contributor

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! A few more minor things.

@Marcono1234
Copy link
Contributor

@eamonnmcmanus, what do you think?


/** Creates an empty JsonArray. */
@SuppressWarnings("deprecation") // superclass constructor
private JsonArray(ArrayList<JsonElement> elements) {

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
JsonElement.JsonElement
should be avoided because it has been deprecated.
MukjepScarlet and others added 3 commits November 8, 2025 12:29
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
@MukjepScarlet
Copy link
Contributor Author

I will probably split this PR into 2 or more for easier review & merging.

@eamonnmcmanus
Copy link
Member

Sorry for not responding earlier. I completely agree that the JsonArray and JsonObject APIs are somewhat awkward, and in particular it is unnecessarily inconvenient to build up a tree of JsonElement objects using those APIs. However I'm not sure that just adding these factory methods is the right way to improve things. My feeling is that we might be better off with a general API that translates an arbitrary tree of Java objects (using List, Map, Number and so on) into the corresponding JsonElement tree. We would need to take the time to specify such an API carefully before sending any PRs. I hope to create a Gson issue shortly where we can discuss approaches.

@MukjepScarlet
Copy link
Contributor Author

I think extensions about addAll can be a simpler PR, like addAll(JsonElement...)
Also I noticed Gson is going to be Java 8, so we can have collectors for JsonArray and JsonObject too.

@MukjepScarlet
Copy link
Contributor Author

My feeling is that we might be better off with a general API that translates an arbitrary tree of Java objects (using List, Map, Number and so on) into the corresponding JsonElement tree.

Generally we have something like DTO list/map which can't be simply parsed into JSON tree without serializer. If we only allow Map to JsonObject, this API might be not so useful.

@eamonnmcmanus
Copy link
Member

My feeling is that we might be better off with a general API that translates an arbitrary tree of Java objects (using List, Map, Number and so on) into the corresponding JsonElement tree.

Generally we have something like DTO list/map which can't be simply parsed into JSON tree without serializer. If we only allow Map to JsonObject, this API might be not so useful.

I think what you are describing is what Gson already does with its TypeAdapter API. What I'm describing is a way to build JSON trees more conveniently, typically with List.of(...) and Map.of(...), where the only types that appear in the compound expression are List, Map, and primitives (in the JsonPrimitive sense). So I shouldn't have said "an arbitrary tree of Java objects". But I hope to spell all that out in a separate issue to make it clearer.

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.

JsonArray.of & JsonObject.of

4 participants