As a big fan of Java EE 7, recently I had a look at the JSON Processing API.
As you know JSON is a data exchange format widely used in web services and other connected applications. The new JSON Processing API (JSR 353), is a lightweight API to parse, transform, and query JSON data using a object model or streaming model.
You can easily build an JSON Object:
[code]
JsonObject model = Json.createObjectBuilder()
.add("title", "Building Microservices")
.add("author", "Sam Newman")
.add("publisher", "O’Reilly")
.add("pages", 278)
.add("ISBN", "978-1491950357")
.add("price", 12.0)
.add("currency", "EUR")
.build();
System.out.println(model);
[/code]
And writing an JSON model to a stream are just a few lines.
[code]
StringWriter stringWriter = new StringWriter();
writeModel(stringWriter, model);
try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {
jsonWriter.writeObject(model);
} catch(JsonException je)
{
je.printStackTrace();
}
String jsonData = stringWriter.toString();
System.out.println(jsonData);
[/code]
An JSON file you can read like this:
[code]
InputStream is = getClass().getResourceAsStream(fileName);
try(JsonReader reader = Json.createReader(is))
{
JsonStructure jsonStructure = reader.read();
System.out.println(jsonStructure);
} catch(JsonException je)
{
je.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
[/code]
And there is even a streaming model for parsing and writing JSON. Here you can see the parsing.
[code]
InputStream is = …
…
try(JsonParser parser = Json.createParser(is))
{
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;
}
}
} catch(JsonException je)
{
je.printStackTrace();
}
…
[/code]
And here comes the writing example.
[code]
FileWriter writer = new FileWriter(fileName);
…
try(JsonGenerator generator = Json.createGenerator(writer))
{
generator.writeStartObject()
.write("title", "Building Microservices")
.write("author", "Sam Newman")
.write("publisher", "O’Reilly")
.write("pages", 278)
.write("ISBN", "978-1491950357")
.write("price", 12.0)
.write("currency", "EUR")
.writeEnd();
}
…
[/code]
It was fun playing around with the API. I think the fact that JSON Processing (Project) does not explicitly support JSON binding limits the usage. I read that a future JSR may consider a JSON Binding similar to JAXB. So you can use the Java API for JSON Processing with JAX-RS resource methods, but when does it make sense?
In my opinion, if you are not the owner of the bounded context and model then you can use the JSON Processing API. Otherwise I would use JAXB with annotations. Jersey, the reference implementation for JAX-RS supports for binding JSON data from Restful resource methods to Java objects using JAXB. With JAXB you can automatically read and write XML and JSON.
I hope you had fun reading my article.
Examples on Github: https://github.com/claudioaltamura/javaee/tree/master/jsonp
Here you can get an overview: http://docs.oracle.com/javaee/7/tutorial/jsonp002.htm.
This is the project site: https://jsonp.java.net
and here you can have a look a API Javadoc: https://json-processing-spec.java.net/nonav/releases/1.0/fcs/javadocs/index.html