JsonObject vs JSONObject - java

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.

Related

What is the relationship between Jersey, JAXB, JAX-RS, Moxy, Jackson, EclipseLink Moxy, json and xml? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am coming from Node.js background and have quite a good understanding of RESTful web services.
Now I am trying to build RESTful web services using Java. I understand core Java but completely new to Java based web development.
I come to conclusion after some tutorials that I need to use Jersey framework to build my RESTful API. I understand that Jersey is some sort of reference implementation of JAX-RS.
But I fail to understand relationship between various other terms and components like JAXB, Jackson, EclipseLink Moxy, jersey-media-moxy, Jettison, JSON-P JSON, XML, etc. that come across my readings. The only thing that I could conclude was it is not so straight forward like JavaScript to covert Java objects into XML or JSON equivalent.
My question is what is the relationship between these terms mentioned above and how they fit together if I am developing a Java based RESTful API.
There sure is a lot of terminology in the Java world and that can create a significant learning curve for new developers. It's not that it's particularly difficult to pass JSON or XML documents around using Java, it's just that the various bits and pieces you need to do it have sprouted terminology over the years. I've tried to list my understanding of the terms you've used below...
XML - you know what XML is, right? The extensible markup language. It's what we had before JSON became the big thing.
JSON - oh, well, JSON is the new big thing. It's a human readable object serialisation format, less verbose than XML. Very popular nowadays. It's the new magic bullet, good for what ails ya, gonna solve all your problems...
JAXB - the "Java Architecture for XML Binding" in the Java ecosystem is the primary mechanism for turning XML data into objects which you can then interact with, and vice versa. It's important to realise that it's an API and not an implementation, so it mostly defines a set of annotations and simple classes / interfaces in the javax.xml.bind package. To do anything useful with JAXB you need an implementation. There's a reference implementation included in the Glassfish application server. Most application servers will have some kind of implementation of JAXB.
Jackson - a library for data binding. It supports both XML and JSON as document formats, and implements the JAXB API. You can use Jackson as your implementation of JAXB, or you can just use the Jackson API directly.
EclipseLink Moxy - an alternative implementation of the JAXB API. Like Jackson, it also has its own API. You can choose to use it, or not. You probably don't want to use both Jackson and Moxy.
Jersey-media-moxy - as you mentioned, Jersey is an implementation of JAX-RS. One aspect of JAX-RS is passing documents around - often XML or JSON. To do that Jersey needs to know what underlying library to use for data-binding or stream processing. So jersey-media-moxy exists as a kind of jersey plugin dependency which you can use to configure Jersey to use Moxy for your object serialisation needs. There's an equivalent package for using jackson called jersey-media-json-jackson.
Jettison - Yet Another serialisation library for converting Java objects to Json and back.
JSON-P - an API for processing JSON either as a stream of events or via data-binding to an object. This API is still in development. You might ask how it is that anybody does json processing without it - the answer is that they either utilise proprietary library APIs (such as Jackson or Moxy) or they use a library which repurposes the JAXB API to work with JSON (Jackson definitely allows this, I'm not sure about Moxy). JSON-P will make it easier to work directly with JSON features, without all the XML-concepts which JAXB brings in.

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.

How do I serialize a Java object such that it can be deserialized by pickle (Python)?

I'm using a Python service that uses pickled messages as part of its protocol. I'd like to query this service from Java, but to do so, I need to pickle my message on the client (Java). Are there any implementations of pickle that run on the JVM (ideally with minimal dependencies)?
Clarification: Modifying the server side is not an option, so while alternate serializations would be convenient, they won't solve the problem here.
Some additional investigation yielded pyrolite, an MIT-licensed library that allows Java and .NET programs to interface with the Python world. In addition to remote object functionality, it (more importantly) includes a pickle serializer and de-serializer.
You can use a Java JSON serializer like GSON or Jackson to serilaise quite easily and use a python json pickle to deserialize

Transform any ActionScript class in XML

For a program i'm writing I need to marshal ActionScript classes to a format that is later to be read by Java (and back again).
What solutions exists for such a need ?
Is there a way (like when using Java through XMLEncoder/XMLDecoder) to generate that XML using standard flex libraries (that would be compatible with later decoding using XMLDecoder) ?
Or is there a good existing library ?
EDIT Yes, this question is a duplicate of Are there any tool mapping of JavaBeans to ActionScript Class through xml serialization and deserialization?, but I will accept correct answers and eventually start a bounty if no answer satisfies my needs. (in other words, i plan to make the previous - unanswered - question a duplicate of mine).
EDIT 2 To be even more precise, I have an application divided in two parts : one Flex GUI and one Java core. They communicate over a non http layer which requires data to be sent in XML. In this context, I replicated my Java objects in Flex (using GAS3) and now want some of these objects to be sent from Flex to Java and back again.
For that purpose, I have to serialize objects (on the Flex end) in XML and deserialize them in Java (and all that back again).
We are using http://www.spicefactory.org/parsley/index.php which supports XML-to-object conversions back-and-forth. Their documentation is very decent: http://www.spicefactory.org/parsley/docs/2.4/manual/.
See describeType function if you really want XML. But I seriously recommend considering the other serializations formats too.
You can look into JSON. The classes needed for the actionscript serialization/deserialization are part of the as3corelib library.
You might also want to take a look at BlazeDS.
Solution used was to put XStream on the java side and FleXMLer (with some adaptations that can be found there : https://github.com/Riduidel/FleXMLer) on the Flex side. it works quite good (once FleXMLer is adapted to XStream architecture).

Decode JSON data in 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.

Categories