Skip to content

How to Parse JSON to from Java Object using Boon Example

Richard Hightower edited this page Feb 17, 2015 · 9 revisions

In this example you will learn how to parse a JSON String to Java and how to convert Java Object to JSON format using Boon.

This is a parody of How to Parse JSON to from Java Object .

JSON, JavaScript Object notation, is an object notation. JSON is also valid JavaScript object notation. It is used as an interchange format. Because it is small and compact, it is often used in place of XML. JSON is also featured prominently as a interchange format in Microservices due to its tolerant reader and promiscuous writer features. JSON is schema-less.

Java does not have any built-in support to parse JSON but there is a JSON JSR for parsing JSON so expect it soon. Java developers have choice when picking a JSON parsing lib: GSON, Jackson, JSON-simple, and of course Boon which also is the built-in parser for Groovy and is a standalone project focusing on reflection, JSON path, object querying and more.

Boon is a very very high performance JSON parser among other things. It has no external dependency and solely depends on the JDK. Boon JSON has a similar interface as Jackson and GSON.

Boon JSON is also powerful and provides full binding support for common JDK classes as well as any Java Bean class, e.g. Player in our case. It also provides data binding for Java Collection classes e.g. Map as well Enum.

Let's get started.

Boon Library

The complete Boon library consists of 1 jar file that are used for many diffident operation. I am going to assume that you use maven, gradle, or their ilk.

<dependency>
    <groupId>io.fastjson</groupId>
    <artifactId>boon</artifactId>
    <version>0.31</version>
</dependency>

You DO NOT need Default Constructor in Your Bean Class

When I first run my program, it just worked because I used Boon damn it.

Converting an object to JSON

        Player kevin = new Player("Kevin", "Cricket", 32, 221, 
                          new int[]{33, 66, 78, 21, 9, 200});
        final String json = toJson(kevin);

        puts("JSON", json);

Reading an object from JSON

        Player somePlayer = fromJson(json, Player.class);
        
        puts("They are equal", somePlayer.equals(kevin));
JSON {"name":"Kevin","sport":"Cricket",
       "age":32,"id":221,"lastScores":[33,66,78,21,9,200]} 
They are equal true 

Boon can mimic the Jackson interface or the GSON interface

        Player kevin = new Player("Kevin", "Cricket", 32, 221, 
                new int[]{33, 66, 78, 21, 9, 200});

        final ObjectMapper mapper = JsonFactory.create();


        final File file = File.createTempFile("json", "exmaple.json");
        mapper.writeValue(file, kevin);



        Player somePlayer = mapper.readValue(file, Player.class);

        puts("They are equal", somePlayer.equals(kevin));

This will also create a temp file in a temp folder which will be deleted by the OS because who would want stray json files all over their hard disk.

That's all about how to parse JSON String in Java and convert a Java object to JSON using Boon. Though there are couple of more good open source library available for JSON parsing and conversion e.g. GSON and JSON-Simple but Boon is one of the best and feature rich, its also tried and tested library in many places since it gets included with Groovy. All parsers and mappers have their own caveats. It is best to test.

QBit the Java microservice lib for Java uses Boon, JSON, and supports HTTP, WebSocket, JSON, REST and can send 200M inproc messages a second.

Clone this wiki locally