Upload
sullis
View
11.400
Download
0
Embed Size (px)
DESCRIPTION
Comparing JSON Lib
Citation preview
Comparing JSON libraries
Sean SullivanPortland Java User Group
July 19, 2011
JavaScript Object Notation
pentagon hexagon json
{ “hello” : “world”, “count” : 1 }
JSON is a data interchange format
{
“name” : “Mock Crest Tavern”,
“tags” : [ “music”, “beer”, “tots” ],
“location” :
{
“city” : “Portland”,
“state” : “Oregon”
}
}
• based on a subset of the JavaScript programming language
• text format
• language independent
JSON
RFC 4627
How is JSON useful to Java developers?
Web Services
“In the first version of our API, data was available in XML, with JSON support added largely as an afterthought. XML as a means for delivering developer-friendly data hasn’t aged well. Seeing the growing inclination of developers and API makers alike towards JSON, we’ve decided to eliminate XML support and put the energy spent there into getting our JSON implementation just right.”
July 2011
http://engineering.tumblr.com/post/7541361718/introducing-tumblrs-new-api
JSON libraries
• json.org library
• Google GSON
• Jackson
• lift-json
http://json.org/javadoc
import org.json.*;
JSONObject obj = new JSONObject();
obj.put(“Hello”, “World”);
obj.put(“count”, 1);
String json = obj.toString();
http://jackson.codehaus.org/
import org.codehaus.jackson.*;
import org.codehaus.jackson.map.*;
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(foo);
Foo foo2 = mapper.readValue(json, Foo.class);
http://code.google.com/p/google-gson/
import com.google.gson.*;
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
String json = gson.toJson(foo);
Foo foo2 = gson.fromJson(json, Foo.class);
Jackson vs GSON
• GSON serializes all non-transient non-static fields. GSON accesses fields directly with Java’s Reflection API
• Jackson uses JavaBean methods (setter methods / getter methods)
JSON and Scala
lift-json
JSON and Android
json.org JsonReader, JsonWriter
Android 1.x Y N
Android 2.x Y N
Android 3.x Y Y
Benchmarks
Related topics
Native JSON
http://blog.mozilla.com/webdev/2009/02/12/native-json-in-firefox-31/
http://blogs.msdn.com/ie/archive/2008/09/10/native-json-in-ie8.aspx
For more information
• http://www.json.org
• http://en.wikipedia.org/wiki/JSON
Thank you