BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Public Review of JSON-P Specification 1.1 is Now Open

| by Michael Redlich on Mar 08, 2017. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Cool by 邓 均

Cool

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT