----- Original Message ----- > From: "cay horstmann" <cay.horstm...@gmail.com> > To: "core-libs-dev" <core-libs-dev@openjdk.org> > Sent: Sunday, May 18, 2025 7:55:12 AM > Subject: Re: Towards a JSON API for the JDK
Hello Cay, > +1 for having a JSON battery included with the JDK. And for "Our primary goal > is > that the library be simple to use for parsing, traversing, and generating > conformant JSON documents." > > Generating JSON could be easier. Why not convenience methods Json.newObject > and > Json.newArray like in https://github.com/arkanovicz/essential-json? > > Parsing with instanceof will work, but is obviously painful today, as your > example shows. The simplification with deconstruction patterns is not > impressive either. > > JsonValue doc = Json.parse(inputString); > if (doc instanceof JsonObject(var members) > && members.get("name") instanceof JsonString(String name) > && members.get("age") instanceof JsonNumber(int age)) { > // use "name" and "age" > } else throw new NoSuchArgumentException(); > > vs. Jackson > > String name = doc.get("name").asText(); > int age = doc.get("age").asInt(); > ... > > If only there was some deconstruction magic that approximates the JavaScript > code > > const doc = { name: "John", age: 30 } > const { name, age } = doc We already have that, it's called a record :) Basically, you are advocating for a mapping JsonObject <--> record. [...] > > Cheers, > > Cay > Here is a simple JsonObject mapper using java.util.json types. https://github.com/forax/json-object-mapper I believe this is also the fastest possible ... it leverages method handles to generate the mapping code (it's only 128 lines of code, obviously, it's a prototype) https://github.com/forax/json-object-mapper/blob/master/src/main/java/json/RecordMapperImpl.java It's mostly on par with hand-coded code (i've just a supplementary lookup through a StableValue) // Benchmark Mode Cnt Score Error Units // RecordMapperBench.mappingSimple avgt 5 15,057 ± 0,050 ns/op // RecordMapperBench.recordMapper avgt 5 16,997 ± 0,044 ns/op regards, Rémi