marianorenteria / minimal-json

A minimal, but complete JSON parser and writer for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

minimal-json

Build Status

A fast and minimal JSON parser and writer for Java. It has been written for (and is used in) the Eclipse RAP project. However, I hope that it is useful for others as well, therefore I keep this repository up-to-date.

Reading JSON

Read a JSON object or array from a Reader or a String:

JsonObject jsonObject = JsonObject.readFrom( jsonString );
JsonArray jsonArray = JsonArray.readFrom( jsonReader );

Access the contents of a JSON object:

String name = jsonObject.get( "name" ).asString();
int age = jsonObject.get( "age" ).asInt(); // asLong(), asFloat(), asDouble(), ...

Access the contents of a JSON array:

String name = jsonArray.get( 0 ).asString();
int age = jsonArray.get( 1 ).asInt(); // asLong(), asFloat(), asDouble(), ...

Iterate over the members of a JSON object:

for( Member member : jsonObject ) {
  String name = member.getName();
  JsonValue value = member.getValue();
  // process value
}

Iterate over the values of a JSON array:

for( JsonValue value : jsonArray ) {
  // process value
}

Writing JSON

Create a JSON object and add some values:

new JsonObject().add( "name", "John" ).add( "age", 23 );

Create a JSON array and add some values:

new JsonArray().add( "John" ).add( 23 );

Write JSON to a Writer:

jsonObject.writeTo( writer );

Create JSON as a String:

jsonArray.toString();

Performance

Below is the result of a rough performance comparison with other parsers, namely org.json, Gson, Jackson, and JSON.simple. In this benchmark, an example JSON text (~30kB) is parsed into a Java object and then serialized to JSON again.

Disclaimer: This benchmark is restricted to a single use case and to my limited knowledge on the other libraries. It may be unfair as it ignores other use cases and perhaps better ways to use these libraries. Most JSON parsers have more advanced functions and may be more suitable for other use cases. The purpose of this benchmark is only to ensure a reasonable reading and writing performance compared to other state-of-the-art parsers.

It seems that reading performance is good average, and writing performance is very good.

Read/Write performance compared to other parsers

License

The code is available under the terms of the Eclipse Public License.

About

A minimal, but complete JSON parser and writer for Java