HeadlinesBriefing favicon HeadlinesBriefing.com

JDK Introduces Simple JSON API

Hacker News •
×

JEP 540, "Simple JSON API," is now in incubator, aiming to provide a standard, low-ceremony API within the Java Platform for parsing and generating RFC 8259 compliant JSON documents, eliminating the need for external libraries for basic tasks.

This initiative supersedes JEP 198 and focuses on a small, easy-to-learn API that strictly adheres to the JSON standard. It avoids complex features like data binding and streaming, prioritizing simplicity and readability for code that navigates and extracts data. The API is designed for quick exploration of unfamiliar JSON documents with fast-failing methods and clear error messages, while also handling missing or unexpected values resiliently.

The motivation stems from JSON's ubiquity and the desire for Java code to perform simple JSON operations as easily as Python or Go. A built-in JSON API would also enable its use within the JDK itself, for instance, in configuration files, which currently lack support for structured data like arrays.

The API is built around the `Json Value` interface, with sub-interfaces for JSON primitives (String, Number, Boolean, Null) and structures (Object, Array). The `Json` class provides a `parse` method to create a tree of `Json Value` instances from a JSON string or char array. This strict parsing ensures interoperability and predictability. The API also simplifies JSON generation, allowing for straightforward creation of JSON objects and arrays.