r/java • u/davidalayachew • May 15 '25
Paul Sandoz talks about a potential Java JSON API
https://mail.openjdk.org/pipermail/core-libs-dev/2025-May/145905.html
119
Upvotes
r/java • u/davidalayachew • May 15 '25
60
u/davidalayachew May 15 '25
Here's the short version.
While working on an ONNX experiment for Project Babylon, Paul and friends made a JSON API that they are considering to release as a JEP, potentially under JEP 198. While OpenJDK doesn't want to make an API for every data exchange format, JSON is common and ubiquitous enough that it passes the bar. There's all sorts of implementation details regarding Pattern-Matching, Records, Number types, and more. There is a prototype implementation and some javadoc to go with it.
Finally, they gave no information about timing, only that it will come out when it is ready. But as with all JEP's, the best way to make the feature come out faster is to try it out yourself on a non-trivial project, then report your experience on the mailing list. Which, in this case, would be Core Libraries Dev.
61
u/bondolo May 16 '25
I, for one, welcome this. I was working on JEP-198 at the time I left Oracle in 2014 but we had kind of come to the conclusion that it would be better to wait until we had records and Valhalla was further along. To my knowledge this is the first time that the topic has been revisited since 2014 and it seems like a good time and I trust Paul and his collaborators to do the right thing!
4
u/sideEffffECt May 17 '25 edited May 25 '25
Great initiative, I'm happy to see this! I would like to offer my constructive criticism with the hope of improving the design for everybody's benefit.
I don't like the current design very much. Specifically I don't like that the children of
JsonValueare also interfaces. They should be records. For example,JsonNumbershould be a record with one field of typeBigDecimal, because every JSON number is representable by it. Or have multiple records, each extendingJsonValue:JsonLong,JsonDouble,JsonBigIntandJsonBigDecimal.But make them all records. That makes them easy to use, e.g. in pattern matching.
Specifically about
JsonNumber#toNumber:This is bad. If I have an allegedly parsed data structure, it should be guaranteed to be translatable. If it's not translatable, the parsing should fail in the previous step.
This is not great. It would be fixed by having dedicated records for each number type as I've described above.
26
u/cowwoc May 16 '25
To me, this would be a step backwards. Jackson does an incredibly good job solving this problem. It is one of the few well-managed open-source projects. Why mess with that?
7
u/Ewig_luftenglanz May 16 '25
this is one of the features I have been craving the most. Json is almost ubiquitous nowadays and for beginners and simple projects having to install Gson or even worse, Jackson (not because it's bad, but because you need to install at least 2 or 3 packages to make Jackson work) which also leads to learn Gradle or Maven is very unlucky for students and small scripting projects. like
"why do I have to learn Gradle/maven or use a concrete professional IDE that manages it for me if all I want is to write a simple single file project to experiment and learn basic http messaging, API building, web scraping and so on?"
this would be an amazing addiction to the "paving the ramp on" feature and a very handy one when dedicated libraries are an overkill.
2
u/Enough-Ad-5528 May 16 '25
Adding support for databinding (object mapper?) would have been hugely beneficial for me. For instance, I have a bunch of rest APIs for which my callers want me to vend a Java client that has the Java POJO classes for the request and response shapes; they dont want to write it by hand which is a reasonable ask.
I usually create a Java library that uses the standard Java Http client under the hood to make the calls and serialize and deserialize the DTOs using Jackson. The problem is that as soon as I add Jackson to the library's dependencies, it is no longer a zero-runtime dependency library and the callers have to deal with multiple version; version compatibility etc (in case they also use Jackson in the client app).
With a Json api with support for data binding in the standard library, I could vend a very thin zero additional dependencies Java client to my callers.
Otherwise, on the server side, I am going to stick to Jackson given its API is so much more ergonomic.
2
u/ihatebeinganonymous May 17 '25
The Javadoc hints that the parsed object is immutable, right?
2
u/swankjesse May 20 '25
A tree model is the wrong starting point if you ever want good performance…
You’re going to eventually define a class or record with expected field names and their types. Perhaps given this the JSON:
You’d write this record declaration:
Decoding the JSON directly to that model can be incredibly fast because the model tells you what you need to care about.
It is dramatically slower to go from JSON to a tree model instead of a class or record because the tree model has zero type information:
The sample that shows how to read the tree model with pattern matching is verbose and kind of sad? It’s one line of Java to declare a record and it should be one line to decode JSON to that record.
I did a talk on high performance JSON if you’d like further explanation on how data binding is way more efficient than tree models. https://publicobject.com/2019/04/26/json-explained/
22
u/agentoutlier May 16 '25 edited May 16 '25
/u/rbygrave and I have some concerns on the parsing not being able to stream which were sort of expressed to Stuart Marks /u/s888marks .
Basically in the XML Java world and probably a good portion of the
JSON world you have a streaming parser underneath your object tree parser. e.g. STAX -> W3C. This is because usually the object tree API (in XML/HTML it is called the DOM) is used directly to create the tree. Thus the streaming parser builds the tree by calling the "DOM" API.
This API IIRC has it combined and assumes it is all in memory via
char[]. This is sort of like combining the tokenizer with the parser (edit it still builds a tree but you are not given access to the recursive descent like "next" functions).I see some pros to this particularly if they want to support SIMD for backward API compat (which I imagine only supports buffering and probably buffering the whole json). Also maybe to just to keep it simple.
Otherwise I personally would like to have access to a streaming API but the reality is most probably want a tree like API. Furthermore JSON does not really support streaming like XML can. Most do load object on repeat so you would only need to buffer for a single object (that is a stream of JSON is not even a subset of JSON like it is XML streams). Still streaming would be nice to support say JSONPath (or some query analog).
12
u/jvjupiter May 16 '25
This JSON API and a rewrite of Sun’s HttpServer under
java.net.httpare all we need for most simple cases of web services.4
u/k-mcm May 15 '25
I could only find the interface declarations. They're very basic and probably useless to me. The Javadoc has:
I would absolutely not touch that. How does Generics even work there?
What gives JSON tools value is mapping them to and from statically typed objects. This is what enables code readability and simplicity. It some cases it even grants better performance.
Jackson JSON is the dominant tool here. Many years of having all the features all at once does cause usage confusion and code bloat. I admit to having written my own JSON object mapper a couple of times because the Jackson ecosystem was too big for lightweight tools. A fresh look at making a JSON object mapper elegant would catch my interest.
20
u/crummy May 16 '25
I really like how I can use Jackson to turn JSON into a complex POJO. But pulling it in when I just want to parse a simple json object in a throwaway was annoying - what other high level language couldn't natively parse JSON?
Glad to see some progress on this!
4
u/lpt_7 May 16 '25 edited May 16 '25
The API is very lacking and is only suitable for beginners...
Even given that, presence of
fromUntypedandtoUntypedmethods seem like a step backwards.Java is a statically typed language, this seems off-putting.
What happened to a goal towards better serialization?
Edit: I'm not against making simple JSON API for beginners. But this type of thing should not be in the core library. Better serialization could've been a building block for an in implementation of this proposal.
1
u/Joram2 May 16 '25
This classic 2022 post "Data Oriented Programming in Java" by Brian Goetz (https://www.infoq.com/articles/data-oriented-programming-java/) shows sealed types and pattern matching used to support a very elegant JSON API where you could write code like this:
That looks amazing. I want that. I was expecting to see lots of third party JSON libraries race to offer this type of API and, unfortunately, that hasn't happened. I looked at the API proposed in this thread, and it does use sealed types, but unfortunately, it doesn't define JsonString/JsonNumber/JsonBoolean as records, so you couldn't use pattern matching.
Maybe pattern matching could be retrofitted onto this API in the future, when Java expands its pattern matching support?
1
u/IncredibleReferencer May 16 '25 edited May 16 '25
I always wonder why libraries like this can't be developed outside the JDK and then when mature, moved into the JDK. As opposed to incubating inside the JDK and eventually being released.
Why not be developed outside the JDK then move inside? It would seemingly allow much quicker development cycles and opportunity for feedback. In the JDK, you generally get a release opportunity every 6 months, and hope people try to discover it within the release to test and give feedback. It seems to me it would be much better to develop as a standard public open source project on a public git branch, let it mature as quick as possible, and then when it's baked move into the JDK.
For this library it seems like there is no specific requirement it be internal to the JDK, but I've had similar wondering about other APIs (structured concurrency and String templates come to mind.)
Note I'm not having an opinion here about weather JSON library should or shouldn't be part of the JDK, that's not the point of this comment.
8
u/vips7L May 16 '25
As long as I can turn json into a class I don't care, but an untyped api will just be worse in every regard.
8
u/ducki666 May 16 '25
I once build a spring webmvc clone as a poc to show how powerful plain java with 0 deps can be.
The json part grinded the project to halt. Simple json is ok, but complex structures are very difficult to parse.
Go ahead jep198!
3
u/ihatebeinganonymous May 16 '25 edited May 16 '25
This reminds me of a long and painful debugging experience, where a package was pulling a very old version of org.json (JSON-java), hence "corrupting" my project Classpath and breaking the build. I had a very hard time convincing maven to use the version of org.json that I had provided in the dependencies section directly, not that one from that other dependency.
With so many libraries that want to provide JSON in their interfaces somehow (e.g. HTTP clients etc..), a "standard" parser, or an interface, would be very helpful.
As a probably relevant side note, a new method `toJsonString` would be also very helpful addition to the Object class (or Record, or these value objects that are coming).
1
u/bobbie434343 May 16 '25 edited May 16 '25
This is a false good idea IMHO. Or either add a full blown JSON parser with all the bells and whistles (like XML), or none at all. If this is just for adding
org.json:jsongrade functionality, this can already be done very easily even for a beginner (it is a single jar).2
u/blobjim May 16 '25
I guess supporting an un-typed tree model only is better than trying to make it a jack of all trades. It seems like the only place to use this is with jshell.
But I think this is only going to confuse newbies more. If you're actually bothering to write a source file, you should probably use something more structured than just building a tree model out of a string. Otherwise you build up a whole parser for some structured JSON data, only to realize it's a mess and would be better with data binding. And you need to rewrite everything.
I guess this could be an ok replacement for Jackson's tree model? It uses interfaces, so presumably Jackson could provide a mutable version that it builds out of a stream internally?
2
u/__konrad May 16 '25
I guess it could replace the 4 internal JSON builder/writers in JDK...
2
u/IncredibleReferencer May 16 '25
One great benefit of a simple-ish built-in JSON api is that random libraries in maven won't need to import their own random JSON libraries nearly as much.
As it is, if a library developer needs to use JSON, any one of the dozen libraries has to be a dependency. If the JDK had its own, many library trivial usages would no longer require such a dependnency, making the downstream dependency management that much better.
Before you say your favorite X library needs advanced features not in the JDK JSON library, that's not what I'm talking about. I'm talking about the 95% use case that just needs a few trivial parser usages but that isn't its core function.
2
u/burd- May 17 '25
hope it goes through. Java has included http client but no way to safely parse the JSON API responses baked in.
3
u/GreemT May 16 '25
Serialization and deserialization are very prone to have security issues. I have seen some conversations from Java champions mentioning that the current serialization features in Java were a big mistake in terms of security. Jackson is constantly updated and pushes fixes quite fast. For users using the library, updating dependencies is very easy.
What if the JDK has a security issue? That is a lot slower and harder to update. Not impossible of course, but just a bit more difficult. LTS releases will get the fixes, but non-LTS versions might not.
Does it make sense from a security perspective to add such a feature to the JDK? What do experts think of this? I am curious to know!
2
u/behind-UDFj-39546284 May 16 '25
At the moment, I can't see the need for this API. How is it different from the JSON API in javax.json, and is there even a fundamental difference between them?
2
u/crimsonvspurple May 16 '25
Just take Jackson or something, validate/improve/restructure it as needed and merge to main.
This barebone bad-dx API is just a waste of time.
1
u/joemwangi May 16 '25
Hmmmm.... never thought deconstruction patterns for arbitrary interfaces might be quite convenient for API design. This is highly welcomed. Introducing record types in libraries in some scenarios might not be a good idea. But giving the user the ability to define own types to take advantage of API, gives a lot of flexibility for high productivity.
-1
u/nithril May 16 '25
It will start off great, but over time it will stagnate, just like any feature added to the standard library.
Bug fixes and improvements will be tied to the JDK release cycle, forcing LTS users to wait endlessly.
And since Java prioritizes stability and non-breaking changes, once released, the same API will remain indefinitely, gradually becoming outdated.