Return to site

☕ JAVA 28 gets its own mini JACKSON/Gson

Java may finally parse JSON without Jackson. 👀

· java

🔸 TLDR

JEP 540 brings an incubating, dependency-free JSON API to JDK 28 for simple parsing, navigation and generation.

It is intentionally NOT a Jackson/Gson replacement:

▪️ no object data binding

▪️ no streaming API

▪️ no schema validation

▪️ no permissive JSON extensions

The interesting trade-off? Reading JSON is simple. Creating it is more controversial.

JEP 540 also supersedes JEP 198, proposed back in 2014: the same year Java 8 shipped. 😅

Section image

🔸 1️⃣ ENABLE THE INCUBATOR MODULE

java --add-modules jdk.incubator.json App.java

The API lives in jdk.incubator.json, so applications must explicitly resolve the incubator module.

#JDK28

🔸 2️⃣ PARSE JSON

JsonValue json = Json.parse(body);

Json.parse() converts an RFC 8259 JSON document into the JDK's immutable JsonValue tree.

#JSON

🔸 3️⃣ NAVIGATE WITHOUT CASTING

int temperature = Json.parse(body)
    .get("properties")
    .get("periods")
    .get(0)
    .get("temperature")
    .asInt();

get(String) and get(int) live directly on JsonValue, enabling navigation without repeatedly casting to objects or arrays.

#JavaAPI

🔸 4️⃣ HANDLE OPTIONAL MEMBERS

json.tryGet("nickname")
    .map(JsonValue::asString)
    .ifPresent(IO::println);

tryGet() returns an Optional when an object member may not exist.

#Optional

🔸 5️⃣ DISTINGUISH JSON NULL

json.get("middleName")
    .tryValue()
    .ifPresent(IO::println);

tryGet() deals with missing members, while tryValue() lets code treat JSON null explicitly.

#NullSafety

Section image

https://javafullstack2027.mystrikingly.com

🔸 6️⃣ USE PATTERN MATCHING

long id = switch (json.get("id")) {
    case JsonNumber n -> n.asLong();
    case JsonString s -> Long.parseLong(s.asString());
    default -> throw new IllegalArgumentException();
};

The sealed JSON hierarchy works naturally with modern Java pattern matching.

#PatternMatching

🔸 7️⃣ CREATE A JSON OBJECT

var user = JsonObject.of(Map.of(
    "name", JsonString.of("Duke"),
    "age", JsonNumber.of(30),
    "active", JsonBoolean.of(true)
));

Construction is strongly typed: Java values are explicitly converted to JSON values.

#TypeSafety

🔸 8️⃣ CREATE A JSON ARRAY

var providers = JsonArray.of(List.of(
    JsonString.of("SUN"),
    JsonString.of("SunRsaSign"),
    JsonString.of("SunEC")
));

And THIS is where the debate starts. 🔥

Several developers immediately asked why this cannot simply be:

JsonArray.of("SUN", "SunRsaSign", "SunEC");

The current design keeps JSON types explicit, but its "low ceremony" goal is already being challenged.

#DeveloperExperience

🔸 9️⃣ EXPECT STRICT JSON

Json.parse("""
    {"name":"Duke",}
    """);

❌ Trailing commas are rejected.

Comments, syntax extensions and duplicate object-member names are rejected too. Invalid input produces JsonParseException.

#FailFast

🔸 🔟 GENERATE JSON

String compact = json.toString();
// {"service":"web_server","id":3}

String pretty =
    Json.toDisplayString(json, "  ");
//{
//  "service": "web_server",
//  "id": 3
//}

toString() generates compact JSON while toDisplayString() produces a formatted representation.

#Serialization

🔸 WHY THIS MATTERS

For years the answer to JSON in Java has effectively been:

▪️ Jackson

▪️ Gson

▪️ JSON-P / JSON-B

▪️ another dependency

JEP 540 asks a different question:

Should parsing a small REST response or JSON file really require another library?

For large applications, Jackson isn't going anywhere.

But for scripts, JShell, small utilities, JDK internals and simple integrations, a built-in JSON API could be extremely useful.

The strongest criticism is also legitimate:

If the stated objective is "low ceremony", construction APIs such as:

JsonString.of("SUN");

repeated throughout an object feel surprisingly ceremonial.

Fortunately, that's precisely why this is an Incubator API. 🧪

Its shape can still change based on developer feedback.

🔸 TAKEAWAYS

▪️ JDK 28 is getting a native SIMPLE JSON API through JEP 540.

▪️ It targets small JSON tasks, not Jackson-style data binding.

▪️ Parsing and navigation are remarkably compact.

▪️ The sealed hierarchy fits modern Java pattern matching nicely.

▪️ Parsing is intentionally strict.

▪️ JSON construction currently has significantly more ceremony.

▪️ Incubation gives the community a chance to improve that API before it stabilizes.

#Java #JDK28 #JEP540 #JSON #OpenJDK #JavaDevelopment #JavaDeveloper #Programming #SoftwareEngineering

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇