That's exactly my point. The "specific format" issue doesn't get easier or harder with 
"serialization 2.0".

Il 21/05/25 10:56, Viktor Klang ha scritto:
Hi,

The way to layer a cake like this is as follows:

[ class ]                         <- describes the general structure of the 
type (the types, names, and order of the parameter lists)
[ instance ]                  <- describes the specific structure of the 
instance (the values)
[ structure ]                 <- converts to/from structure to instance (think 
constructor / deconstructor pairs)
[ specific format ]    <-  converts to/from structure to specific format (think 
MyCompany JSON Order format version 5)
[ general format]      <- parses / generates the general format (think XML / 
JSON / CSV / etc)
[ IO ]                               <- reads / writes the general format 
(think file / socket / etc)


As for the question how to represent a Timestamp or similar, is answered in the 
[specific format] layer of this cake. Why? Because the next version of the 
specification of that layer might change how timestamps should be represented 
at that layer.

Now, while it is possible to short-circuit some of the layers of the cake by 
embedding *specific format* decisions at the *class* level, it means that 
there's now a tight (and somewhat exclusive) link between a *specific format* 
and *class*, with the implication that you cannot output the same *instance* as 
both XML and JSON (as it embeds the decisions of a specific *json schema*. If 
that was the default, and only, way to go about this, what *specific format* 
should a library decide to adhere to?

The cake layering at the beginning of this email allows types to be used by 
many different *specific formats* and even *general formats*, such that you 
could read something from JSON-over-HTTPS and output it as XML-over-SFTP.

Cheers,
√

*
*
*Viktor Klang*
Software Architect, Java Platform Group
Oracle
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*From:* core-libs-dev <core-libs-dev-r...@openjdk.org> on behalf of Cay Horstmann 
<cay.horstm...@gmail.com>
*Sent:* Wednesday, 21 May 2025 09:55
*To:* core-libs-dev@openjdk.org <core-libs-dev@openjdk.org>
*Subject:* Re: Towards a JSON API for the JDK
Data binding is indeed complex because real-life JSON is messy. I reviewed some 
code in which I use Jackson data binding. Here is an example of a sticky issue. 
I have Content that's either a list of strings or a list of string pairs (don't 
ask). The JSON is

{ "strings": [..., ..., ... ] }

or

{ "pairs": [ { "first", ..., "second", ... },  ..., ... ]

The JavaScript client relies on the fact that exactly one of "strings" and 
"pairs" exists. No, I can't change the JSON.

I deserialize into a record Content(List<String> strings, List<Pair<String>> 
pairs) {}. And tweak Jackson to not to serialize entries with null values. It's not pretty, but 
it is easier than doing tree navigation, since this sits deeply in an otherwise fairly regular 
structure.

Could that work with Serialization 2.0? I suppose. Looking at https://www.youtube.com/watch?v=mIbA2ymCWDs 
<https://www.youtube.com/watch?v=mIbA2ymCWDs>, a marshaller can't dynamically produce either a 
List<String> or a List<Pair<String>>, so it would offer both in the 
Marshalled<Content> object, with one of them being null. I would need to tell the wire format generator 
to write the non-null one. Which is ok--that's what I do now with Jackson.

Here is another common issue. You have a record TimedPoint(int x, int y, Instant when) {}. How 
do you want to serialize the Instant? As an ISO 8601 string? Millis since the epoch? Who makes 
that choice? Slide 29 says "Let the class author be in charge". WHICH class author? 
Can java.time.Instant make a universal choice, for all possible wire formats? Surely not. Can 
TimedPoint? Maybe. Or it is the job of the wire format generator to do that with 
Marshalled<TimedPoint>?.

So here is my point. If the JDK were to include a JSON data mapper, that data 
mapper is either rigid or flexible. Designing a flexible data mapper is hard. 
Serialization 2.0 would not make it any easier.

My hunch is that flexible JSON data mapping is hard to do within the scope of 
the JDK. Would it follow Jakarta JSON Binding and be tied to that spec? Or 
strike out on its own? Neither seems attractive.

Ethan and Rémi have presented rigid JSON mappers. They handle nothing but 
numbers, strings, booleans, maps, lists, records. Is that useful? Surely, for 
simple programs. For that, there is no reason to wait for Serialization 2.0. If 
and when that comes, it would be a natural extension.

Cheers,

Cay

Il 20/05/25 21:46, Paul Sandoz ha scritto:
Data binding is a complex feature, even if some examples make it appear simple. 
Our intention is to explore alignment with the serialization 2.0 effort, when 
we are ready to so. Hence, I would urge folks to be patience and watch out 
Brian and Viktor’s Devoxx 2024 talk on the topic.

Paul.

On May 20, 2025, at 12:21 PM, Swaranga Sarma <sarma.swara...@gmail.com> wrote:

A potential advantage that we (the OpenJDK community) can more easily do is 
devise an API that resonates with direction the Java platform is heading, 
specifically around the pattern matching and serialization 2.0

Right, but most of the discussion here seems to be on the low level tree API 
and how one might navigate it (perhaps using pattern matching). I am not seeing 
any discussion on the serialization/deserialization or any reasoning why that 
is not the goal of the new JEP. Basically a very focused and narrow API:

```
var jsonStr = "..."
var myRecord = Json.deserialize(jsonString, UserRecord.class); //and a few 
other variants
var serialized = Json.serialize(myRecord)
```

This leaves the tree API open to reimplementation later when pattern matching 
and other features are firmly in place in the JDK. I am making a case that this 
would be more immediately useful for the most common and simpler cases.


Regards
Swaranga


--

Cay S. Horstmann | https://horstmann.com <https://horstmann.com>


--

Cay S. Horstmann | https://horstmann.com

Reply via email to