Coder Social home page Coder Social logo

minimal-json's Introduction

minimal-json

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.

minimal-json's People

Contributors

ralfstx avatar

Watchers

Erwin Haasnoot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.