-
Notifications
You must be signed in to change notification settings - Fork 102
How to Parse JSON to from Java Object using Boon Example
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>
When I first run my program, it just worked because I used Boon damn it.
Player kevin = new Player("Kevin", "Cricket", 32, 221,
new int[]{33, 66, 78, 21, 9, 200});
final String json = toJson(kevin);
puts("JSON", 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
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.
YourKit supports Boon open source project with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .Net profiler.