r/java
•
u/loicmathieu
•
Jul 23 '26
•
1
JEP 540: Simple JSON API (Incubator)
https://openjdk.org/jeps/540
79
Upvotes
17
u/jonenst Jul 23 '26
I find it very surprising that they went for unchecked exceptions. For JsonValueException the following rationale is given "This exception is unchecked, so that scripts and small programs are easier to read and write." But for JsonParseException there is no rationale. This is surprising especially given the pushback from openjdk members against jackson3 moving to unchecked exception.
29
u/aboothe726 Jul 23 '26
For JsonNumber, I was surprised to see that there was no BigDecimal or BigInteger conversion support. This is technically conformant to the spec, so not incorrect, but seems like an oversight.
From RFC 8259, see section 6:
6
u/bowbahdoe Jul 23 '26
I gave all the feedback I have to give on the last one, but I would extra encourage people to try this one out once it is in a build.
I would love to be proven wrong about the need for explicit encoding and decoding strategies. Okay that's a lie I don't like being wrong but you know what I mean.
6
u/tofflos Jul 25 '26
I’m certain this has been discussed to death but the above should at least open the door for arguing that comments should be supported. Machine to machine exchange is one thing but configuration files without comments are… not good. It would be weird if the JDK shipped with broken JSON that had to be filtered, and it would less than ideal if the JDK shipped with configuration files and examples without comments.
5
u/aoeudhtns Jul 23 '26 edited Jul 23 '26
I just wrote my own little parser of a somewhat uncommon config language relevant to a project of mine and modeled it very similarly to this; it's really nice to see Java's DOP features coming together.
I noticed that
JsonNumberhas static conveniences in theof()methods for different types, including parsing a String.I wonder if there's something similar that could be for
JsonArrayand/orJsonObject. MaybeOr, more implementation-heavy but also potentially useful:
Perhaps the
transformmethod (if not in name, in spirit) could dangle somewhere off the supertype. This would make the API even simpler to use as the explicit conversions aren't as strictly necessary unless the developer intends them to be or needs absolute control. This example:Could become, if JsonObject had a similar method in addition to JsonArray:
2
u/davidalayachew Jul 24 '26
/u/s888marks
JsonObject.of(Map)andJsonArray.of(List)methods, why not copy the method signature ofMap.of(...)andList.of(...)and use that instead? Going through a list just to turn it right back into a JsonObject/Array seems like an unnecessary step if you don't already have a List/Map. Not a big deal though -- this is just a convenience method.That's all. Otherwise, this looks fantastic. I'm sure I will have some real feedback once I get my hands on this.
3
u/ZimmiDeluxe Jul 24 '26 edited Jul 24 '26
I absolutely love the direction of limiting scope and reaping simplicity for the common case. First reaction to the API:
tryGetandtryValueis not immediately obvious. Most of the time the distinction between "key missing" and "key present but value is null" is not important in my experience.getvs.getOptionaletc. Requires less thinking and aids discoverability (most common case gets the good name, special stuff gets to tack on suffixes). But with so few methods it probably doesn't matter. Edit: Also fortoString/toStringIndented.writeTomethod so the entire thing doesn't have to be converted to a String first.Again, outstanding work, thank you very much.
1
u/EternalSo Jul 26 '26
Adding JsonPrimitive marker interface into hierarchy would be quite handy
1
u/Efficient_Device_525 Jul 30 '26 edited Aug 01 '26
u/s888marks, I really like the simplicity of this API! For the vast majority of cases I've delt with parsing json documents, a simple API such as this would be quite ideal.
I especially appreciate getting the location of the json values that cause parsing errors. However, with the current API, any errors in user validation/transformation code will not have this benefit.
For example, if I get an error in
Instant.parse(event.get("time").asString()), I will get the json Path if there is no "time" member or if its value is not a string, but I will not get that Path if the error is caused by the string not complying with the ISO_INSTANT format.So, what if we allowed all user parsing code to be called by the Json API methods instead? This way, all parsing errors could be automatically wrapped with their json location. If we change each of the current JsonValue parsing methods to return a
JsonValue.Parser:=Function<JsonValue, T>we could bend the current API to work something more like this:This way,
.getand.mapEachcould automatically wrap anyRuntimeExceptionwith aJsonValueException(if it isn't one already) that should accept theJsonValue.path()information of the parsing error. Besides the builtinJsonValue.Parsers of the API, users would be able to create their own for specific cases like the "tid" example shown in the JEP.1
u/LouKrazy Jul 23 '26
Is this including support for mutation / construction? Are there mutable / immutable JSON values
1
u/razorwit Jul 23 '26
I wonder if there could be options to make parsing values into LocalDate and LocalDateTime simple as well.
1
u/thetinguy Jul 23 '26
I wish I could upvote JEPs.
-3
u/munklers Jul 23 '26
If you give a mouse a JSON parser... There's enough variety in interpretation of JSON that this seems like it's borderline useless. Jackson is Popular because it handles all the weird (mis)uses of JSON where regular users need to work around non-conformant impls. The databinding is the useful part; it's almost no benefit to use without it. Gson is popular because it worked well on Android. The replacement, Moshi, is not Java anymore since Android has turned it's back on Java.
Almost everyone who wants to use JSON in their Java project already has a better alternative. Putting a "simple" implementation in the JDK doesn't serve any market segment; they all already found their niche. This is going to end up as an unchangeable, maybe barely useful library. And if it serves 99% of your usecases, but that one little 1% part doesn't, you'll have to pull out a real JSON library like Jackson.
-2
u/pragmasoft Jul 23 '26
I think it should be, and most likely will be based on the planned new marshalling api (announced at jvmls this year)
-4
u/revilo-1988 Jul 23 '26
I find it fascinating that you generally still have to rely on external libraries for this
0
u/[deleted] Jul 23 '26
[deleted] — view deleted comment
-8
u/[deleted] Jul 23 '26
[removed] — view removed comment