BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Public Review of JSON-P Specification 1.1 is Now Open

Public Review of JSON-P Specification 1.1 is Now Open

This item in japanese

Public review of JSR 374: Java API for JSON Processing (JSON-P) version 1.1 is now open. This version is expected to be included in the release of J2EE 8 and keeps JSON-P current with JSON IETF standards. It includes support for:

JSON-P was introduced in 2013 with the release of J2EE 7, as an alternative to Gson and Jackson. It was designed to parse, generate, and query standard JSON documents.

JSR-367: Java API for JSON Binding (JSON-B), will also be included in the release of J2EE 8. JSON-B  was designed to bind JSON documents to Java objects. InfoQ has previously reported on the public review of JSON-B.

Getting Started

To get started with a JSON-P parse and pointer example, consider the following JSON file:

    
[
    {
    "publication":"New Vaadin Spring Release Introduces Enhanced View Management",
    "publicationDate":"2016-12-30 00:00:00",
    "author":"Michael Redlich",
    "publicationType":"Article",
    "publisher":"C4Media"
    },
    {
    "publication":"Pivotal Releases First Milestone of Next-Generation Spring Data Featuring Reactive Database Access",
    "publicationDate":"2017-01-19 00:00:00",
    "author":"Michael Redlich",
    "publicationType":"Article",
    "publisher":"C4Media"
    },
    {
    "publication":"Netflix Introduces Hollow, a Java Library for Processing In-Memory Datasets",
    "publicationDate":"2017-01-31 00:00:00",
    "author":"Michael Redlich",
    "publicationType":"Article",
    "publisher":"C4Media"
    }
]
    

The JSON file may be parsed using JsonParser:

    
public static final String JSON_FILE = "publications.json";

InputStream fis = new FileInputStream(JSON_FILE);

JsonParser parser = Json.createParser(fis);
while(parser.hasNext()) {
    JsonParser.Event event = parser.next();
    switch(event) {
        case START_ARRAY:
        case END_ARRAY:
        case START_OBJECT:
        case END_OBJECT:
        case VALUE_FALSE:
        case VALUE_NULL:
        case VALUE_TRUE:
            System.out.println(event.toString());
            break;
        case KEY_NAME:
            System.out.print(event.toString() + " " + parser.getString() + " - ");
            break;
        case VALUE_STRING:
        case VALUE_NUMBER:
        System.out.println(event.toString() + " " + parser.getString());
            break;
        }
    }
fis.close();
    

Using JsonPointer, a particular element in the JSON file can be found:

    
InputStream fis = new FileInputStream(JSON_FILE);

JsonReader jsonReader = Json.createReader(fis);
JsonArray jsonArray = jsonReader.readArray();
JsonPointer pointer = Json.createPointer("/1/publication");
JsonValue publication = pointer.getValue(jsonArray);
System.out.println(i + ": " + publication);
fis.close();
    

This example will find and display the second publication in the above JSON file. We prepared an entire project demonstrating JSON-P, Gson, and Jackson, which can be found on GitHub.

The latest specification can be downloaded for review from the JSR-374 website.

Well known Java EE evangelist Reza Rahman recently blogged about the role of JSON within J2EE 8:

These two APIs together are extremely important in making JSON a first class citizen of the standard Java platform, just like JAXP (Java API for XML Processing) and JAXB (Java API for XML Binding) did many years ago for XML. With these two APIs in place Java developers can simply think of JSON as yet another Java serialization format. No more third party libraries and no more configuration - things will simply work out of the box when it comes to processing JSON. In my view these APIs are so critical they should indeed be moved to a modular Java SE release, much like JAXB and JAXP are already a part of Java SE.

The final versions of JSON-P and JSON-B are expected to be released in April 2017.

Resources

Additional information on JSON-P can be found via the following resources:

Rate this Article

Adoption
Style

BT