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.
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
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
0
Alexandre SimõesManager / Technology SpecialistCommented:
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.
0
Alexandre SimõesManager / Technology SpecialistCommented:
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

gudii9Author Commented:
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
0
Alexandre SimõesManager / Technology SpecialistCommented:
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

0
gudii9Author Commented:
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.
0
gudii9Author Commented:
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
0
Alexandre SimõesManager / Technology SpecialistCommented:
The explanation is pretty much bellow that sentence but it also differs a bit between technologies.

For instance, I'm more of a .Net guy when it come to server-side code and Marshalling in the .Net world is mainly related to the way of establishing communication between managed and unmanaged components. This involves translating types and workarounds of mapping unmanaged "language" with managed.

On the other hand, in the JAVA world, I often see Marshalling used pretty much as Serialization. Mainly moving objects back and forth like Object <=> XML.
I see that in your tags you have JAVA so probably you're just speaking about exactly the same thing.

Now lets go crazy and ready deep into the documentation RFC 2713
Scroll down into point:
2.3 Marshalled Objects

   To "marshal" an object means to record its state and codebase(s) in
   such a way that when the marshalled object is "unmarshalled," a copy
   of the original object is obtained, possibly by automatically loading
   the class definitions of the object.  You can marshal any object that
   is serializable or remote (that is, implements the java.rmi.Remote
   interface).  Marshalling is like serialization, except marshalling
   also records codebases. Marshalling is different from serialization
   in that marshalling treats remote objects specially. If an object is
   a java.rmi.Remote object, marshalling records the remote object's
   "stub" (see Section 2.5), instead of the remote object itself.  Like
   serialization, when an object is marshalled, the entire tree of
   objects rooted at the object is marshalled. When it is unmarshalled,
   the tree is reconstructed.

This sound much more structured as a definition, especially this part:
Marshalling is like serialization, except marshalling also records codebases.
This means that, Serialization is a way to transport object state while Marshalling is the way of transporting whole code bases like full objects with real code that can be executed.
This definition comes from the JAVA world and perfectly aligns with the definition in the .Net world.

Bottom line, whenever you see XML <=> Object data hydration/dehydration you should be speaking about Serialization.
If what comes on the wire is full classes with methods and logic that you can use, then it's marshalling.

I hope this made things a bit more clear for you because for me was a nice ride :)

Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
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.
0
Alexandre SimõesManager / Technology SpecialistCommented:
Yep. That's it.
That's pretty much what I wrote above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.