Boon, another JSON Parser, Faster and lighter than GSON
Boon and JSON
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 probably the fastest way to serialize and parse JSON in Java so far for your project.. 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.
(Boon should be the fastest for most use cases that I have seen for REST, but there are some clear areas where Jackson and FASTJson are faster. At the moment, Boon is not the fastest way to encode JSON if you have a large strings with unicode outside of the ASCII range. The plan is to close the gap, but currently this makes Jackson faster for these use cases. Also please note that Boon is a newer project and not as mature as Jackson or GSON.)
Serialization
Serialization is achieved by:
Jackson style
ObjectMapper mapper = JsonFactory.create();
mapper.writeValue(dst, myBean); // where 'dst' can be File, OutputStream or Writer
Gson Style
ObjectMapper mapper = JsonFactory.create();
String dst = mapper.toJson(myBean);
Full Data Binding
When using full data binding, deserialization type must be fully specified as something other than Object.class. For example:
Jackson style
MyBean value = mapper.readValue(src, MyBean.class); // 'src' can be File, InputStream, Reader, String
Gson style
MyBean value = mapper.fromJson(src, MyBean.class); // 'src' can be File, InputStream,
Boon supports both Jackson and Gson style annotations so if you are coming from those project or are used to those projects, Boon should just work.The main complication is handling of Generic types: if they are used, one has to use the two argument version of readValue to work around Java Type Erasure:List<MyBean> beans = mapper.readValue(src, List.class, MyBean.class);
"Simple" Data Binding
As mentioned above, simple here just means that range of value types is limited to core JDK types. If this is acceptable, deserialization type can be simply defined as Object.class. This can apply at root level (for calls to ObjectMapper, as well as at lower level -- whatever value are declared to be of basic Object type will use Simple data binding. Simple like this:Object root = mapper.readValue(src, Object.class); Map<String,Object> rootAsMap = mapper.readValue(src, Map.class);
Complete code listing for tutorial so far
package org.boon.json; import org.boon.Lists; import java.io.File; import java.util.List; import java.util.Map; import static org.boon.Boon.puts; /** * Created by rick on 1/4/14. */ public class JsonTutorial { public static class MyBean { String name = "Rick"; @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } } public static void main (String... args) throws Exception { MyBean myBean = new MyBean(); File dst = File.createTempFile("emp", ".json"); ObjectMapper mapper = JsonFactory.create(); puts ("json string", mapper.writeValueAsString( myBean )); mapper.writeValue( dst, myBean ); // where 'dst' can be File, OutputStream or Writer File src = dst; MyBean value = mapper.readValue(src, MyBean.class); // 'src' can be File, InputStream, Reader, String //MyBean value = mapper.readValue(src, MyBean.class); // 'src' can be File, InputStream, Reader, String puts ("mybean", value); Object root = mapper.readValue(src, Object.class); Map<String,Object> rootAsMap = mapper.readValue(src, Map.class); puts ("root", root); puts ("rootAsMap", rootAsMap); MyBean myBean1 = new MyBean(); myBean1.name = "Diana"; MyBean myBean2 = new MyBean(); myBean2.name = "Rick"; dst = File.createTempFile("empList", ".json"); final List<MyBean> list = Lists.list( myBean1, myBean2 ); puts ("json string", mapper.writeValueAsString( list )); mapper.writeValue( dst, list ); src = dst; List<MyBean> beans = mapper.readValue(src, List.class, MyBean.class); puts ("mybeans", beans); } }
Output from the above listing:json string {"name":"Rick"} mybean MyBean{name='Rick'} root {name=Rick} rootAsMap {name=Rick} json string [{"name":"Diana"},{"name":"Rick"}] mybeans [MyBean{name='Diana'}, MyBean{name='Rick'}]
Examples of it "just works"
Full Data Binding (POJO) ExampleBoon's ObjectMapper "just works" for mapping JSON data into plain old Java objects ("POJOs"). For example, given JSON data:{ "gender": "MALE", "name": { "first": "Richard", "last": "Hightower" }, "verified": true }
To create the above JSON file, you could serialize this Java POJO:public class User { public enum Gender { MALE, FEMALE }; public static class Name { private String first, last; public Name( String _first, String _last ) { this.first = _first; this.last = _last; } public String getFirst() { return first; } public String getLast() { return last; } public void setFirst(String s) { first = s; } public void setLast(String s) { last = s; } } private Gender gender; private Name name; private boolean isVerified; public Name getName() { return name; } public boolean isVerified() { return isVerified; } public Gender getGender() { return gender; } public void setName(Name n) { name = n; } public void setVerified(boolean b) { isVerified = b; } public void setGender(Gender g) { gender = g; } }
To produce the above JSON, you can create a serialize User as follows:ObjectMapper mapper = JsonFactory.create(); User user = new User(); user.setGender( User.Gender.MALE ); user.setName(new User.Name("Richard", "Hightower")); user.setVerified( true ); puts ( mapper.writeValueAsString( user ) );
This JSON gets produced:{ "gender": "MALE", "name": { "first": "Richard", "last": "Hightower" }, "verified": true }
It is just as easy to read and write files:Now to write/serialize and then read/deserialize this user to/fro to a file.Write to a file:File file = File.createTempFile( "user", ".json" ); mapper.writeValue( file, user );
Read from a fileUser userFromFile = mapper.readValue( file, User.class ); puts ( userFromFile );
The puts method is like System.out.println of sorts and is part of Boon.Using Streams, Readers, Writers, etc.
Boon also works with Streams, Readers, Writers, etc.Read from a inputStream using JDK 1.7 Files utility:Path path = Paths.get(file.toString()); InputStream inputStream = Files.newInputStream(path); User userFromInput = mapper.readValue( inputStream, User.class ); puts ( "userFromInput", userFromInput );
Read from a reader using JDK 1.7 Files utility:Reader reader = Files.newBufferedReader( path, StandardCharsets.UTF_8 ); User userFromReader = mapper.readValue( reader, User.class ); puts ( "userFromReader", userFromReader );
Boon and REST
Boon ships with an IO library that makes it easy to read/write from URLs, and other file systems:User userFromURL = mapper.readValue( IO.read("http://fromsomewebsite/user.json"), User.class ); puts ( "userFromURL", userFromURL );
Boon also makes making JSON calls to REST webservices easy as you can use Boon's HTTP utilities as follows:String results = HTTP.postJSON("http://foo.com/bar/add/user", mapper.writeValueAsString( user ) ); AddUserResponse response = mapper.readValue( results, AddUserResponse.class );
Here is an example of getting a listing of users from a REST call:List <User> userList = mapper.readValue( HTTP.getJSON("http://foo.com/bar/user/listing"), List.class, User.class );
Complete code listing so far for tutorial
package org.boon.json; import org.boon.Lists; import org.boon.core.Dates; import java.io.File; import java.io.InputStream; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; import java.util.List; import java.util.Map; import static org.boon.Boon.puts; /** * Created by rick on 1/4/14. */ public class JsonTutorial { public static class MyBean { String name = "Rick"; @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } } public static class User { public enum Gender { MALE, FEMALE }; public static class Name { private String first, last; public Name( String _first, String _last ) { this.first = _first; this.last = _last; } public String getFirst() { return first; } public String getLast() { return last; } public void setFirst(String s) { first = s; } public void setLast(String s) { last = s; } @Override public String toString() { return "Name{" + "first='" + first + '\'' + ", last='" + last + '\'' + '}'; } } private Gender gender; private Name name; private boolean verified; private Date birthDate; public Name getName() { return name; } public boolean isVerified() { return verified; } public Gender getGender() { return gender; } public void setName(Name n) { name = n; } public void setVerified(boolean b) { verified = b; } public void setGender(Gender g) { gender = g; } public Date getBirthDate() { return birthDate; } public void setBirthDate( Date birthDate ) { this.birthDate = birthDate; } @Override public String toString() { return "User{" + "gender=" + gender + ", name=" + name + ", isVerified=" + verified + '}'; } } public static void part1 () throws Exception { MyBean myBean = new MyBean(); File dst = File.createTempFile("emp", ".json"); ObjectMapper mapper = JsonFactory.create(); puts ("json string", mapper.writeValueAsString( myBean )); mapper.writeValue( dst, myBean ); // where 'dst' can be File, OutputStream or Writer File src = dst; MyBean value = mapper.readValue(src, MyBean.class); // 'src' can be File, InputStream, Reader, String puts ("mybean", value); Object root = mapper.readValue(src, Object.class); Map<String,Object> rootAsMap = mapper.readValue(src, Map.class); puts ("root", root); puts ("rootAsMap", rootAsMap); MyBean myBean1 = new MyBean(); myBean1.name = "Diana"; MyBean myBean2 = new MyBean(); myBean2.name = "Rick"; dst = File.createTempFile("empList", ".json"); final List<MyBean> list = Lists.list( myBean1, myBean2 ); puts ("json string", mapper.writeValueAsString( list )); mapper.writeValue( dst, list ); src = dst; List<MyBean> beans = mapper.readValue(src, List.class, MyBean.class); puts ("mybeans", beans); } public static void part2 () throws Exception { ObjectMapper mapper = JsonFactory.create(); User user = new User(); user.setGender( User.Gender.MALE ); user.setName(new User.Name("Richard", "Hightower")); user.setVerified( true ); user.setBirthDate( Dates.getUSDate( 5, 25, 1980 ) ); puts (mapper.writeValueAsString( user )); //Now to write and then read this as a file. File file = File.createTempFile( "user", ".json" ); mapper.writeValue( file, user ); User userFromFile = mapper.readValue( file, User.class ); puts ( "userFromFile", userFromFile ); Path path = Paths.get(file.toString()); InputStream inputStream = Files.newInputStream(path); User userFromInput = mapper.readValue( inputStream, User.class ); puts ( "userFromInput", userFromInput ); Reader reader = Files.newBufferedReader( path, StandardCharsets.UTF_8 ); User userFromReader = mapper.readValue( reader, User.class ); puts ( "userFromReader", userFromReader ); } public static void main (String... args) throws Exception { part1(); part2(); } }
Project :
Reference :
Comments