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

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.

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.

How to convert swagger JSON objects into Java class objects

Sorry for my naive question, I am looking for some tools that would automatically convert JSON objects (that are generated in swagger UI) to a Java class objects in Eclipse.
Are there any such tools or plugins that can integrate swagger code with Eclipse Java and Groovy Grails framework. It would save a lot of time by not rewriting a huge amount of Java code every time when I want to change something in JSON objects.
I suggest to use autorest tool to generate code and models. It supports multiple languages including Java. Here is the github repository autorest
Just follow simple steps-
npm install -g autorest
autorest --input-file=https://******url******/swagger/v1/swagger.json --java --namespace=****namespace*****
You can use Google Gson Library in java.
You could use that or Groovys JSONsluprer but I depending on what you're trying to achieve it might just be easiest if you have a swagger to start with swagger codegen https://github.com/swagger-api/swagger-codegen tools and have it make those for you. If not you can always look to investigate how they did it.

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

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