JEP 540 Proposed to Target JDK 28 with a Simple JSON API
JEP 540, Simple JSON API, has progressed to Target status for JDK 28. It introduces a compact API for parsing and generating JSON documents without external dependencies. Focused on core tasks, it provides an immutable value hierarchy. The API allows simple traversal and conversion while enforcing strict syntax rules. Feedback during incubation will shape its future development. By A N M Bazlur…
The JEP 540 proposal aims to introduce a new JSON API for JDK 28, providing a simple and JDK-provided solution for working with RFC 8259 JSON documents. This compact API will be housed in the incubating jdk.incubator.json module, allowing for potential future changes or removal based on developer feedback. The proposed API focuses on common JSON tasks, such as parsing configuration files, inspecting REST responses, and generating small JSON payloads.
The core of the API is the Json class and its sealed JsonValue interface, which defines six non-sealed subinterfaces for JSON objects, arrays, strings, numbers, booleans, and null. Instances of JsonValue are immutable and thread-safe.
The design of the API centers around declaring access methods directly on JsonValue, enabling callers to traverse objects and arrays without frequent casting. Access methods like get(String) on non-object values, get(int) on non-array values, requesting missing members, or using invalid indices will result in a JsonValueException. Parsing a complete in-memory JSON document can be done using Json.parse(String) or Json.parse(char[]), which returns a JsonValue.
The main ergonomic trade-offs of this API involve the construction of JSON values. Factory methods on the corresponding interfaces are used to create JSON values, such as creating a boolean using Json.Boolean(true). While this approach makes JSON types clear, it also adds ceremony since Java primitives must be wrapped when placed in objects or arrays.
The API prioritizes strictness, rejecting syntax such as comments, trailing commas, and duplicate object-member names, which could lead to interoperability issues. Invalid syntax and duplicate names are reported through unchecked JsonParseException instances that include the line and position of the detected error.
The API does not provide a lenient parsing mode or syntax extensions, and it does not expose a structured JSON path. The sealed value hierarchy integrates well with pattern matching for handling different JSON types. Conversion methods, such as asInt() and asLong(), require an exact integral value within the destination type's range, while asDouble() converts numbers to finite doubles, which may result in some precision loss.
Optional member access through tryGet(String) returns an Optional JsonValue, distinguishing between missing members and explicitly containing JSON null values. The API also emphasizes the distinction between missing members and explicitly null values, as tryGet() returns an Optional containing JsonNull for the latter, while tryValue() returns an empty Optional for JsonNull.
Written by urgent.news from InfoQ's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.