Decode JSON data in Java - java

I'm used to PHP, and decoding json data is just a line of code.
What would be the easiest way to do this in java?

Pick one of the libraries from the Java section at the bottom of the json.org page.

Gson
Userguide

have a look at
http://code.google.com/p/json-simple/
maybe it helps ;-)

I love Gson, it's very simple and easy to use. If you are interessted in more, here is a tutorial (german): http://blog.mynotiz.de/programmieren/java-json-decode-tutorial-2074/

Decoding json in java is not too hard. Google's gson api handles json data very well. A tutorial on decoding json data using gson is there in my blog http://preciselyconcise.com/apis_and_installations/json_to_java.php

I like Flexjson. It's lightweight and easy to use.
But I admit that I haven't bothered to compare all the alternatives :-)

There are many JSON libraries available in Java.
The most notorious ones are: Jackson, GSON, Genson, FastJson and org.json.
There are typically three things one should look at for choosing any library:
Performance
Ease of use (code is simple to write and legible) - that goes with features.
For mobile apps: dependency/jar size
Specifically for JSON libraries (and any serialization/deserialization libs), databinding is also usually of interest as it removes the need of writing boiler-plate code to pack/unpack the data.
For 1, see this benchmark: https://github.com/fabienrenaud/java-json-benchmark I did using JMH which compares (jackson, gson, genson, fastjson, org.json, jsonp) performance of serializers and deserializers using stream and databind APIs.
For 2, you can find numerous examples on the Internet. The benchmark above can also be used as a source of examples...
Quick takeaway of the benchmark: Jackson performs 5 to 6 times better than org.json and more than twice better than GSON.
Let me know if you have any questions.

Related

JsonObject vs JSONObject

I am a bit confused about Json Class in java libraries.
I could find these 3 at least and all seems Oracle Json Libraries
javax.json JsonObject
com.oracle.json JsonObject
org.json JSONObject
Why so many Oracle json class?
I could find these 3 at least and all seems Oracle Json Libraries
Actually, there are many more than that; see the list at https://www.json.org/json-en.html. Many are not Oracle classes. Indeed org.json.JSONObject is not an Oracle class.
Why so many Oracle json class?
History. It took a long time for Oracle to include JSON support in the official Java libraries. And by the time that they did, various other people / organizations had produced their own offerings, and developed their own followings.
(The reason the Oracle have two JSON implementations is that their design goals are significantly different.)
Which one is the recommended one and why?
There is no single recommendation. It very much depends on what your application requires in terms of JSON support. Do you want something light-weight? Do you need to fit in with an existing framework (e.g. Java EE or Java ME)? Do you need POJO "bindings"? Streaming parsers?
You are mixing up a lot of things here.
javax.json is part of the JSON-B and JSON-P Java EE API - used for JSON processing and data binding. And used in context of an application server (WildFly / Tomcat / ...)
whereas com.oracle.json refers to Java ME ("Micro Edition").
and org.json is just another parser, like GSON.
You wouldn't use com.oracle.json outside of a Java ME environment, but whether you use javax.json, org.json, GSON, Jackson, ... is up to your personal taste and requirements. But mostly, the application server already contains a JSON parser - for Wildfly, this was Jackson until JSON-B/P arrived rather late to the parsing party.

Is GSON using a cache mechanism to enhance encoding time for objects

I'm using GSON Library to encode java objects to JSON Strings and I am interested in how this works. I can suggest that GSON is using a cache mechanism to speedup the encoding, but I can't find a source of knowledge about that topic. In GSON documentation there is no information about it.
Could someone find information about cache behavior of GSON?
Thanks,
Ben

JSON <=> Java Object Mapper which translates well with J2objc

I use the org.json.* classes which are part of the j2objc distribution. The mapping of my DTO classes and the JSON Objects are made by hand so far.
I know there is GSOn and Jackson.
Is there a JSON to Object Mapper library which translates well with J2objc and works well in the translated Objective-C code?
Jackson and GSON are the two best libraries, and both have been translated using j2objc for iOS apps from different teams (no public ports that I'm aware of, though).
If you control the server-side of the app, though, protocol buffers are generally faster than any JSON->Java alternative. That is why they're used by most of Google's iOS apps, including Inbox and Sheets that use j2objc. j2objc now publicly supports protocol buffers.
To convert json data to java object you can use:
Jackson
Gson
Follow this link or this link for nice GSON tutorial.
I personally used GSON, and it is best library for json to java object conversion.
I don't know about conversion part of j2objc but you can follow this answer, if you want to do similar thing with iOS code
Edit
According to this answer, j2objc requires source code for conversion. You will get full source code from here.
Now, the important part, Gson can be converted to j2objc and you can find test repository to test support for gson-2.3 with j2objc here.

XML serialization library interoperability between Java and Python

I have been searching for an xml serialization library that can serialize and deserialize an (Java/Python) object into xml and back. I am using XStream right now for Java. If XStream had a python version to deserialize from xml generated by Xstream that would have done it for me. Thrift or such other libraries is not going to work unless they allow the data format to be xml. I am looking for suggestion for any library that can do it. - Thanks
Since Java and Python objects are so different in themselves, it's almost impossible to do this, unless you on both sides restrict the types allowed and such things.
And in that case, I'd recommend you use JSON, which is a nice interoperability format, even though it's not XML.
Otherwise you could easily write a library that takes XStream XML and loads it into Python objects, but it will always be limited to whatever is similar between Java and Python.
I don't think you're likely to find an automated way to serialise Java objects and deserialise into Python objects. They're different things, so if you want to translate, you'll have to write some code at one or both ends.
If it's really simple stuff - strings, numbers, booleans, and so on, then you might want to look into json, a very simple format with bindings for just about every language. Deserialising a json object in Python is as simple as:
json.loads('{"test":false}')
Another way to approach the problem might be to use Jython, an implementation of Python in Java, so you can use Java objects directly.
The problem is (like sort of suggested by other answers) that XStream is a Java object serialization framework, and not general data mapping/binding framework. This is by design (see XStream FAQ): upside is that it can nicely serialize and deserialize all kinds of Java objects out-of-box. Downside is that resulting XML structure is fairly rigid, and while you can rename things there isn't much other configurability.
But XStream is not the only Java XML handling library. I would suggest checking out either JAXB reference implementation or JibX as possibly better alternatives, so that you have more control over XML to process. This may be necessary to achieve good interoperability.
Does it really need to use XML?
For serializing structured data between Java and Python you might want to consider Google Protocol Buffers.

Is there a Java JSON library that uses the same pattern as XMLEncoder

I have looked at a few JSON libraries for Java, but few of them seem to follow the same serialization pattern as core Java serialization patterns. I would like to use a native Java JSON serialization object, but in the absence of that what third party library matches Native serialization and preferably will serialize anything marked serializable.
best I have seen on paper is flexJSON it looks fairly complete and a very close match to the XMLEncoder pattern.
I'm pretty fond of the Jackson JSON Processor. Its fast, and very flexible.
Have you looked at google's gson ? It will serialize any POJO hierarchy very nicely for you.

Categories