Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java object to json

Hi,

java object to json conversion is called marshalling or serialization?

 otherway around from json to java object is called deserialization or unmarshalling?

 How to achieve this conversion. what are json advantages compared to other data formats like text, XML etc.

when to use JSON.stingify and JSON.parse methods.
Please advise.
Avatar of Kanti Prasad
Kanti Prasad

Hi

In general XML is document-oriented and JSON is data-oriented. JSON can be mapped more easily to object-oriented systems.

The below links will give you an idea how to implement and know its advantages

http://howtodoinjava.com/2014/06/16/jackson-examples-convert-java-object-to-from-json/

Json vs XML
http://www.json.org/xml.html
Avatar of Alexandre Simões
This is  very nice question.
I thought of it for a minute and in my head they were pretty much the same.

I went to dig a bit more and found this answer:
Marshaling and serialization are loosely synonymous in the context of remote procedure call, but semantically different as a matter of intent.

In particular, marshaling is about getting parameters from here to there, while serialization is about copying structured data to or from a primitive form such as a byte stream. In this sense, serialization is one means to perform marshaling, usually implementing pass-by-value semantics.

It is also possible for an object to be marshaled by reference, in which case the data "on the wire" is simply location information for the original object. However, such an object may still be amenable to value serialization.
This other comment addresses the same thing but in a different way:
Marshalling is usually between relatively closely associated processes; serialization does not necessarily have that expectation. So when marshalling data between processes, for example, you may wish to merely send a REFERENCE to potentially expensive data to recover, whereas with serialization, you would wish to save it all, to properly recreate the object(s) when deserialized.
Sorry I didn't put anything out of my own gut but these seemed very good explanations.
How to achieve this conversion. what are json advantages compared to other data formats like text, XML etc.
This is another topic, and for me the biggest advantage is the payload.
JSON documents are much smaller than XML.
Another big advantage is that JavaScript supports JSON by default while XML is a bit harder to handle (a lot sometimes depending on the complexity of your schema).
I mentioned JavaScript because it's where JSON is mostly used, but you can use it anywhere else.

To achieve the conversion in JAVA I usually use GSON.


when to use JSON.stingify and JSON.parse methods.
This is really only if a system sends you JSON as a string instead of as an object or vice-versa.
An example that popup in my head is the postMessage "trick" to communicate with IFrames. It only allows text to be passed in the message. So if you need to pass an object, you need to convert it to string and on the receiver parse it back to JSON.
Avatar of gudii9

ASKER

This is really only if a system sends you JSON as a string instead of as an object or vice-versa.

string means java string or javascript string. please advise
Hi mate,
basically it depends on where the data is coming from.

If the flow is Client -> Server then it's a JavaScript string.
If the flow is Server -> Client then it's a JAVA string.


JSON.parse and JSON.stringify are client-side functions, meaning, JavaScript.

The difference between a json object and a json string is just the following:
var jsonstring = '{ "name": "Alex" }';
var jsonobject = { "name": "Alex" };

JSON.parse(jsonstring); // gets a json object out of that string
JSON.stringify(jsonobject); // converts the object into its string representation

Open in new window

Avatar of gudii9

ASKER

If the flow is Client -> Server then it's a JavaScript string.
If the flow is Server -> Client then it's a JAVA string.

you mean like below

If the flow is towards Client -> Server then it's a JavaScript string.
If the flow is towards Server -> Client then it's a JAVA string.
Avatar of gudii9

ASKER

Marshaling and serialization are loosely synonymous in the context of remote procedure call, but semantically different as a matter of intent.

i am still bit unclear. Any good links with examples on each? please advise
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

i saw one link on this
http://stackoverflow.com/questions/770474/what-is-the-difference-between-serialization-and-marshaling

Serialization: When you serialize an object, only the member data within that object is written to the byte stream; not the code that actually implements the object.

Marshalling: Term Marshalling is used when we talk about passing Object to remote objects(RMI). In Marshalling Object is serialized(member data is serialzied) + Codebase is attached.

so Serialization is part of Marshalling.

CodesBase is information that tells the receiver of Object where the implementation of this object can be found. Any program that thinks it might ever pass an object to another program that may not have seen it before must set the codebase, so that the receiver can know where to download the code from, if it doesn't have the code available locally. The receiver will, upon deserializing the object, fetch the codebase from it and load the code from that location.
Yep. That's it.
That's pretty much what I wrote above.