Can anyone recommend a good Java JSON library (better than the one from http://json.org/ )? I've also found JSON-lib, which definitely looks like an improvement, but I'm wondering if there is anything that is even better than that?
thanks
Source: Tips4all
I notice that there is also a library called google-gson. I haven't tried it yet. Here's how it describes itself:
ReplyDeleteGson is a Java library that can be used to convert Java Objects into its JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.
I've used JSONLib, FlexJSON and Gson all with great success. Each has its best use.
ReplyDeleteJSONLib is awesome as a core JSON library when you just want to process all elements of a JSON.
JSONArray cms = jsonObject.getJSONArray("containerManifests");
for (Object o : cms) {
JSONObject cm = (JSONObject) o;
String cmId = cm.getString( "cmId" );
}
FlexJSON shines with its deepSerialize method that can properly handle serializing all get methods presented in a bean obtained from Hibernate (lazy loaded).
ContainerManifest cm = cmDAO.read( cmId );
String cmJson = new JSONSerializer().deepSerialize( cm );
Gson seems to be the best API to use when you want to convert a json to a Java class. Other API only call set methods on the high level classes in the bean structure. If you have a deep bean structure, everything else will be implemented with dyna beans. Causes havoc elsewhere. Gson fully populates all low level values by calling all set methods for all data found in the JSON.
Gson gson = new Gson();
ContainerManifest cm = gson.fromJson( json, ContainerManifest.class );
YMMV
Andrew
I can't truly recommend this, because I've never used it, but Jackson sounds promising. The main reason I mention it is that the author, Tatu Saloranta, has done some really great stuff (including Woodstox, the StAX implementation that I use).
ReplyDeleteUPDATE: A year ago I started actually using Jackson and I can confirm that it is awesome :-). I especially like being able to switch back and forth between using a "tree model" (similar to XML DOM) and object mapping. For example, let's say I have the following JSON:
{
"parent":
{
"irrelevantobject":
{
"foo": 1,
"bar": 2
},
"interestingobject":
{
"qux": 3
}
}
}
The data I really want is inside "interestingobject". The rest is fluff that I don't care about (maybe returned by some REST API).
In addition, I have the following Java class:
public class InterestingObject {
int qux;
}
I can use the following code to navigate through the JSON document and then map the object I want:
// Create a standard Jackson mapper object.
ObjectMapper mapper = ...
// Find the node I want using a DOM-like model.
JsonNode rootNode = mapper.read(theJsonString, JsonNode.class);
JsonNode interestingObjectNode = rootNode.get("parent").get("interestingobject");
// Parse it into a Java object.
MyInterestingObject interestingObject = mapper.treeToValue(interestingObjectNode,
MyInterestingObject.class);
Without this capability, I would be stuck writing extra wrapper classes.
Another impressive Jackson feature is the ability to map classes that you don't own (in other words, map a third-party Java class when you can't change the source code). See this blog entry for more details.
Jackson is amazingly customizable. I could list many, many more features :-).
Finally, don't miss 7 Killer Features that set Jackson apart from competition.
I can recommend http://json-lib.sourceforge.net/. We have used it in few projects without problems.
ReplyDeleteI have no personal experience with the following approach,but it could make sense to consider:
ReplyDeleteXStream(xml <-> java data binding) with Jettison driver (xml<->json mapper), more details are available here.
That's from their site:
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("product", Product.class);
System.out.println(xstream.toXML(product));
Just went through this exercise. I wanted to represent arbitrary JSON as nearest Java equivalent. For me that is a HashMap/ArrayList Object. json-simple was excellent: tiny, with a simple API that generates HashMap/ArrayList with a single call. It also has extensions for object serialization/deserialization.
ReplyDeleteI also tried gson: API was very object serialization oriented, and type safe, could not do what I needed simply.
I've been meaning to try Flexjson. It looks like it uses reflection on bean-style properties and uses similar rules as Hibernate/JPA lazy-loading for serialization, so if you give it an object to serialize it will leave out collections (unless you tell it to include them) so you don't end up serializing the entire object graph. The json.org library does pretty well with serializing basic beans with reflection, but doesn't have these advanced features. Might be worth checking out, especially if you use an ORM solution.
ReplyDeleteGson can also be used to serialize arbitrarily complex objects. Here is how you use it:
ReplyDeleteGson gson = new Gson();
String json = gson.toJson(myObject);
Gson will automatically convert collections to JSON arrays. Gson can serialize private fields and automatically ignores transient fields.
While deserializing, Gson can automatically convert JSON arrays to collections or arrays. Gson uses the specified class as the primary specification for deserialization. So, any extra fields available in the JSON stream are ignored. This helps design secure systems that prevent injection attacks.
You can also extend Gson's default serialization and/or deserialization behavior by registering custom type adapters.
For more details: see the user guide at the project: http://code.google.com/p/google-gson/
Disclosure: I am one of the co-authors of Gson.
I've used JSON Tools library and it works well.
ReplyDeleteYou may try using GSON.
ReplyDeleteIt's downloadable at http://google-gson.googlecode.com/files/google-gson-1.4-release.zip
Quite simple to use actually.
I used it to parse JSON results from Yelp and there is a simple example here:
URL URLsource = null;
JsonElement jse = null;
BufferedReader in;
try {
URLsource = new URL("YELP_API_REQUEST");
in = new BufferedReader(new InputStreamReader(URLsource.openStream(), "UTF-8"));
jse = new JsonParser().parse(in);
in.close();
System.out.println(jse.toString());
JsonArray jsa = jse.getAsJsonObject().getAsJsonArray("businesses");
System.out.println(jsa.size());
for (int i= 0; i<jsa.size(); i++ ) {
System.out.println(jsa.get(i));
System.out.println("===========================================================");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I read a review of a few JSON libraries here: Review
ReplyDeleteI wrote a JSON "pull-api" parser (3 classes, 18K), which I really like using. I find the pull metaphor much more usable than the event metaphor, and creating a document tree using pull is trivial.
ReplyDeleteFWIW I didn't much care for the www.json.org parser either. My biggest complaint with the offerings out there is the size of them - we target a download-constrained applet market. I remember lying in bed one night at about 2am wondering "how hard could it be", after a bit I got up and started writing - this tiny parser is the result.
JSON Tools looks good too.
The following generalized recursive code uses my parser to parse a JSON file into a "DataStruct" - essentially a map of lists (Note that DataStruct and Callback are objects from another package which are not published with this parser:
/**
* Parse a generalized data structure from a JSON input stream.
* <p>
* All values are added using the <code>crtmbrcbk</code> callback.
* <p>
* <b><u>Reminder</b></u>
* <p>
* When using a reflected method, don't forget to configure your code obfuscator to retain it in unobfuscated form.
*
* @param psr The parser to use.
* @param tgt Target object to which to add members; if this is null a new object is created using the callback.
* @param maxlvl Maximum level to recursively parse substructures, including arrays (objects at a deeper level are silently ignored).
* @param crtmbrcbk A callback object invoked to create a member value.
* @see #createMemberCallback(Object,String)
*/
static public Object parseObject(JsonParser psr, Object tgt, int maxlvl, Callback crtmbrcbk) {
return _parseObject(psr,tgt,maxlvl,crtmbrcbk,new Object[4],false);
}
static private Object _parseObject(JsonParser psr, Object tgt, int maxlvl, Callback crtmbrcbk, Object[] crtprm, boolean arr) {
int evt; // event code
if(tgt==null) { tgt=crtmbrcbk.invoke(crtprm,psr,null,"",null); }
while((evt=psr.next())!=JsonParser.EVT_INPUT_ENDED && evt!=JsonParser.EVT_OBJECT_ENDED && evt!=JsonParser.EVT_ARRAY_ENDED) {
String nam=psr.getMemberName();
switch(evt) {
case JsonParser.EVT_OBJECT_BEGIN : {
if(nam.length()>0) {
if(maxlvl>1) { _parseObject(psr,crtmbrcbk.invoke(crtprm,psr,tgt,nam,null),(maxlvl-1),crtmbrcbk,crtprm,false); }
else { psr.skipObject(); }
}
else {
_parseObject(psr,tgt,maxlvl,crtmbrcbk,crtprm,false);
}
} break;
case JsonParser.EVT_ARRAY_BEGIN : {
if(!arr) {
_parseObject(psr,tgt,maxlvl,crtmbrcbk,crtprm,true); // first level of any array is added directly to the inherently list-supporting object
}
else {
if(maxlvl>1) { _parseObject(psr,crtmbrcbk.invoke(crtprm,psr,tgt,nam,null),(maxlvl-1),crtmbrcbk,crtprm,true); }
else { psr.skipArray(); }
}
} break;
case JsonParser.EVT_OBJECT_MEMBER : {
crtmbrcbk.invoke(crtprm,psr,tgt,nam,psr.getMemberValue());
} break;
}
}
return tgt;
}
҉ works
ReplyDeleteI was looking for a very simple way to do Php json encode in (server) and Java json decode (client), all seems too much to do only to achieve this.
I came out to this which is really simple and it works perfect. Please see: more details click here
I recommend fastjson,just as its name,really fast
ReplyDeleteI've used Jettison and it works well.
ReplyDeleteI liked J2J - Json2Java from sourceforge.(http://sourceforge.net/projects/json2java/)
ReplyDeleteReally easy to map JSON to almost any java object by only annotating the class using JsonElement and then passing the java and class to JsonReader like:
MyClass myclass = (MyClass) new JsonReader(MyClass.class, jsonString).toObject();
give javajson a try. i wrote it and would definitely love some feedback:
ReplyDeletehttps://sourceforge.net/projects/javajson/
I was reading articles about the JAX-RS, and i realize that there isn't an onfficial lib for serialize and pojo to json object!
ReplyDeleteYou may also look at json webserice , your api end point described in better way, also you can expose your api in soap,
ReplyDeleteI will add one more tip for excelent JSON<->JAVA binding lib PoJSON, it is written specifically for the NetBeans IDE (by Petr Hrebejk) and thus it is used in production on large project.
ReplyDeleteIt is easy to use in any project, it is just not placed on the web as a separate project (afaik), however it is in a separate package "org.codeviation" here:
http://hg.netbeans.org/main/file/9609b899e64d/kenai/src/org/codeviation
I have developed an Add-on for Android's in-built JSON Parser (or.json.*), which helps to convert JSON to Java Object-
ReplyDeletehttp://prasanta-paul.blogspot.com/2011/04/json-to-java-bean-conversion-for.html
After exploring and actually using most of the major libraries listed here, I ended up writing a simplified API that is much easier to use and more fun to work with:
ReplyDeletehttp://sharegov.blogspot.com/2011/06/json-library.html
A nice new library for Java is Serotonin JSON. It has lots of nice features.
ReplyDeleteI say "new", but it's really been around for years, just not as open source.