If you have 75 annotations in a Java class and 25 more in the REST handler to do JSON than you are doing it wrong. If you have 500 DTO objects to manage every use case or every view then you are using JSON wrong.
Boon was invented after looking at what a SaaS company needed, and seeing what frameworks they were currently using. Boon was the reaction to an idea of let's have 500 DTOs to match each use case or view.
(Find this in the Boon wiki here. Part 1 is here Build your own JSON adventure with Boon.)
I could not handle writing 500 DTO objects for simple things, and I was sick of there being more annotations in my code then code, and class loaders issues, and , and … so Boon.
Let's say you have a large JSON stream / file / REST POST / Websocket call that looks like this (pretend what you see is large):
Sample JSON of teams
{
"teamInfo" : {
"teamRoster": {
"teamNames": ["duck", "chickens", "penguins"]
}
}
}
Sometimes you want to create a team info (TeamInfo). Sometimes you want just the list or Set. Sometimes you want just the team roster (TeamRoster). Different use cases use different parts of the JSON stream. Perhaps the JSON stream even gets put onto an event bus and many end points grab it and use different parts of the stream.
It is JSON so you should be able to add properties without breaking older event bus listeners.
It is JSON so you should be able to add properties without breaking older event bus listeners.
Sample classes (DTO or domain objects).
public static class TeamRoster {
Set<String> teamNames;
@Override
public String toString() {
return "TeamRoster{" +
"teamNames=" + teamNames +
'}';
}
}
public static class TeamInfo {
TeamRoster teamRoster;
@Override
public String toString() {
return "TeamInfo{" +
"teamRoster=" + teamRoster +
'}';
}
}
In this example we will read the JSON with Boon from the classpath or file system withjsonResource (full example below).
/* Using Boon style (easy) 2 parser. */
jsonObject = Boon.jsonResource(path);
Now if we just want the team info part of the JSON we do this:
/* Using Boon path. */
puts ("teamInfo", atIndex(jsonObject, "teamInfo"));
(puts is like System.out.println but better)
teamInfo {teamRoster={teamNames=[duck, chickens, penguins]}}
atIndex returns the teamInfo map.
You use atIndex to get just the part you care about. We can get the teamInfo, the teamRoster or the teamNames and different use cases (or different listeners on the bus might care about different things) so they can pick and choose what they want and ignore the rest. This is JSON. It is meant to be flexible.
Grabbing just the teamInfo.teamRoster
puts("Team Roster", atIndex(jsonObject, "teamInfo.teamRoster"));
Grabbing just the teamInfo.teamRoster and teamInfo.teamRoster.teamNames
puts("Team Roster", atIndex(jsonObject, "teamInfo.teamRoster"));
puts("Team Names", atIndex(jsonObject, "teamInfo.teamRoster.teamNames"));
output
Team Roster {teamNames=[duck, chickens, penguins]}
Team Names [duck, chickens, penguins]
How do we convert team names into a Set?
Easy.
List<String> teamNames = (List<String>) atIndex(jsonObject, "teamInfo.teamRoster.teamNames");
puts("Team Names", teamNames);
Set<String> teamNameSet = set(teamNames);
puts ("Converted to a set", teamNameSet);
output
Team Names [duck, chickens, penguins]
Converted to a set [duck, chickens, penguins]
How do we convert a specific path into the TeamInfo domain object without converting the entire JSON stream?
Getting just the TeamInfo from JSON
TeamInfo teamInfo = fromMap((Map<String, Object>) atIndex(jsonObject, "teamInfo"), TeamInfo.class);
puts(teamInfo);
How do we convert a specific path into the TeamRoster domain object without converting the entire JSON stream?
Getting just the TeamRoster from JSON
TeamRoster teamRoster = fromMap((Map<String, Object>) atIndex(jsonObject, "teamInfo.teamRoster"), TeamRoster.class);
Full example
/*
*/
package com.examples;
import org.boon.Boon;
import org.boon.IO;
import org.boon.json.JsonFactory;
import org.boon.json.JsonParserAndMapper;
import org.boon.json.JsonParserFactory;
import org.boon.json.ObjectMapper;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.boon.Boon.atIndex;
import static org.boon.Boon.puts;
import static org.boon.Maps.fromMap;
import static org.boon.Sets.set;
/**
* Created by Richard on 5/6/14.
*/
public class PartialDataTreeExample {
public static class TeamRoster {
Set<String> teamNames;
@Override
public String toString() {
return "TeamRoster{" +
"teamNames=" + teamNames +
'}';
}
}
public static class TeamInfo {
TeamRoster teamRoster;
@Override
public String toString() {
return "TeamInfo{" +
"teamRoster=" + teamRoster +
'}';
}
}
public static void main (String... args) {
File file = new File(".", "src/test/resources/teams.json");
String path = file.getAbsolutePath().toString();
puts ("PATH", path);
puts ("CONTENTS of PATH", IO.read(path));
/* Jackson style interface. */
ObjectMapper mapper = JsonFactory.create();
Object jsonObject = mapper.readValue(file, Object.class);
puts ("JSON Object", jsonObject);
/* Using Boon path. */
puts ("teamInfo", atIndex(jsonObject, "teamInfo"));
puts("Team Roster", atIndex(jsonObject, "teamInfo.teamRoster"));
puts("Team Names", atIndex(jsonObject, "teamInfo.teamRoster.teamNames"));
/* Using Boon style parser (fast). */
JsonParserAndMapper boonMapper = new JsonParserFactory().create();
jsonObject = boonMapper.parseFile(path);
/* Using Boon path. */
puts ("teamInfo", atIndex(jsonObject, "teamInfo"));
puts("Team Roster", atIndex(jsonObject, "teamInfo.teamRoster"));
puts("Team Names", atIndex(jsonObject, "teamInfo.teamRoster.teamNames"));
/* Using Boon style (easy) 2 parser. */
jsonObject = Boon.jsonResource(path);
/* Using Boon path. */
puts ("teamInfo", atIndex(jsonObject, "teamInfo"));
puts("Team Roster", atIndex(jsonObject, "teamInfo.teamRoster"));
puts("Team Names", atIndex(jsonObject, "teamInfo.teamRoster.teamNames"));
//There is also a Groovy style and a GSON style.
List<String> teamNames = (List<String>) atIndex(jsonObject, "teamInfo.teamRoster.teamNames");
puts("Team Names", teamNames);
Set<String> teamNameSet = set(teamNames);
puts ("Converted to a set", teamNameSet);
TeamInfo teamInfo = fromMap((Map<String, Object>) atIndex(jsonObject, "teamInfo"), TeamInfo.class);
puts(teamInfo);
TeamRoster teamRoster = fromMap((Map<String, Object>) atIndex(jsonObject, "teamInfo.teamRoster"), TeamRoster.class);
puts(teamRoster);
}
}
This five minute guide will get you up to speed using Boon in no time. Parse and serialize Java objects to/fro JSON up to 5x faster! Boon is not a JSON parsing project. It is more than that, but JSON parsing is intrinsic to what Boon is all about. Boon is the fastest way to serialize and parse JSON in Java so far. It is faster at object serialization, enabling JSON expressions, JSON parsing and much more. Boon JSON is FAST! In addition it has a very easy to use, convention-based API. This getting started guide covers making JSON REST calls, JSON object serialization, using annotations, and more.
Need to do ETL with JSON, and/or want to search sort JSON or Java graphs quickly.
Java Boon - Boon Data Repo using Indexed Collections for Java Beans, JSON and Maps like JDK 8 streams but faster. Many languages have sorting, slicing, dicing capabilities for Lists and Maps, and JSON. Boon adds this to Java. Boon Data Repo is an in-memory query engine that can use high speed indexed collections for very fast in-memory queries. This brief tutorial will show you how to use Boon to query JSON, Java objects and Java maps. And also how to do ELT transforms with JSON, Java objects and Java Maps.
Java Boon filtering for Java Beans, JSON and Java Maps like JDK 8 streams but much faster. Many languages have support for querying objects and filtering objects easily. Java does in JDK 8, but Boon adds it as well, and it can compliment features of JDK 8 as well as work in Java 7 land. Boon adds filtering to to Java. You can filter JSON, Java, and Maps. You can also use indexed queries which are a lot faster than linear search queries that come with JDK 8.
Many languages have path expressions. Boon adds this to Java. Boon has powerful path expressions that work with objects, lists and maps. It also works with JSON. Full examples of Java, Maps/Lists and JSON are included in this tutorial.
Just curious about what Boon is...
Boon comes with helper methods that allow you to easily read files, create lists, sets, maps, concurrent maps, sorted maps, sorted sets, etc. It provides slice notation, searching, easy IO (files, http, etc.). The helper methods are safeList, list, set, sortedSet, safeSet,safeSortedSet, etc. The idea is to make Java feel more like list and maps are built-in types.
What if Java collections and Java hierarchies were easily searchable? They are with Boon! What if it were easy to query a complex set of Java objects at runtime? What if there were an API that kept your object indexes (really just TreeMaps, and HashMaps) in sync.? Well then you would have Boon's data repo. This article shows how to use Boon's data repo utilities to query Java objects. Boon comes with a simple criteria API that makes working with Java collections a snap.
Boon has many utility methods to convert objects into maps, lists, JSON, etc. It also has many collection utility methods. One such utility method is deepCopy, which will deep copy a list or set. Programmer as a way to clone Collection e.g. List, Set, ArrayList, HashSet or any other implementation. Or they write a lot of boiler plate code and spend a lot of time doing things to copy lists and sets. With Boon you can do deep copies without messing around with clone.
Boon gets some inspiration from Groovy and Python when dealing with maps, lists and JSON. It addition to adding slice notation and such, which I covered before. Boon has first class support for converting between Maps, List, JSON and Java objects. Boon gets some inspiration from Ruby, Groovy, Python and Perl and aims to make Java programming just a bit more fun.
Thoughts
Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).
Further Reading:
If you are new to boon start here:
- Java Boon Byte Buffer Builder
- Java Boon Slice Notation
- Java Boon Slice's work with TreeSets
- Java Boon Description
- More...
- Boon Home
- Boon Source
- Introducing Boon October 2013
- Java Slice Notation
- What if Java collections were easy to search and sort?
- Boon HTTP utils
- Boon Java JSON parser Benchmarks or hell yeah JSON parsing is damn fast!
- Boon JSON parser is really damn fast! Part II
- Boon JSON parser Round III now just not fast as but much faster than other Java JSON parsers
- Boon World's fastest Java JSON parser Round IV from fast to blazing to rocket fuel aka Braggers going to brag
- Boon gets adopted by JSON Path as the default Java JSON parser
- Boon graphics showing just how fast Boon JSON parsing is - about 50% to 200% faster than the graphs shown here now so wicked fast became wickeder - just got sick of making graphics
- 10 minute guide to Boon JSON parsing after I added @JsonIgnore, @JsonProperty, @JsonView, @Exposes, etc.
- Hightower speaks to the master of Java JSON parsing, the king of speed The COW TOWN CODER!
- Boon provides easy Java objects from lists, from maps and from JSON.
No comments:
Post a Comment