vivek
vivek

Reputation: 2867

How is JSON related to serialization?

What is serialization?

Serialization is the process of converting the object into stream of bytes to send it over a network or store it in a file in such a way that the object can be reconstructed again.

What is JSON?

JSON is the data interchange format, having a fixed structure. When de-serialized it return the Javascript object.

I did some Googling but can't find the how they are related? They are often mentioned together. I need to know what is going on under the hood when we say that we need to serialize the data in JSON.

Upvotes: 2

Views: 1073

Answers (3)

heikkim
heikkim

Reputation: 2975

JSON is basically structured text.

One example would be a client-server architecture, such as web browser - server communication is. The data from the server can be serialized from native format to JSON (text) and transferred through the network (stream of bytes) to the client (web browser). When the data gets to the client (web browser), it is deserialized to a Javascript object and handled (maybe something is displayed to the user or the UI manipulated - non-essential).

A Java example

Let's say we've a simple class in our server application, which is written in Java:

class Person {
    String name;
    int age;
}

and we've an instance of this class:

Person person = new Person();
person.name = "John";
person.age = 32;

We've chosen to use JSON as the data format in server-to-client communications. Converting our instance of a person to JSON looks like this:

String json = "{\"person\": {\"name\": \"John\", \"age\": 32} }";

Now we just write this String to HTTP response, from where the client reads it - to a string. The string is then deserialized to a Javascript object. Deserialization could look like this (more about JSON.parse at MDN / JSON.parse):

/* We get responseText by AJAX and it looks exactly the same that we sent from our web
 server - IT IS TEXT, the variable 'json' contents in our Java application. */
var responseText = "..."
var data = JSON.parse( responseText ); // JSON.parse is supported by modern browsers

Data looks something like this if written directly as a Javascript object:

data = {
    person: {
        name: john,
        age: 32
    }
};

So, we started in the server side from a Java-object. We serialized the Java-object to a JSON-string and sent it through the network to the client. The client deserialized the JSON-string to a Javascript-object (which can be properly dealt with in language-specific way).

Upvotes: 1

MinusFour
MinusFour

Reputation: 14423

JSON represent collections of key/value pairs. JSON Parsers (like JSON.parse, json_decode) will map this representation to an appropriate data structure that reflects these collections. JSON Serializers (like JSON.stringify, json_encode) do the inverse, they take a data structure and map it to JSON.

For example in PHP:

$obj['a'] = 'b'; //Associative Array
$obj['c'] = 'd';

Its JSON representation would be:

'{
    "a" : "b",
    "c" : "d"
}'

Keys and values of the respective data structure (in this case an associative array).

In Javascript:

var obj = {
    a : 'b',
    c : 'd'
}

This is a Javascript object, when serialized it maps to the same JSON:

'{
    "a" : "b",
    "c" : "d"
}'

JSON describes a precise string format, which is why I've marked my JSON in between ' '. A string representation of your data structures can be very useful for many things, like sending them as a message.

Upvotes: 0

marsze
marsze

Reputation: 17064

JSON is a format that allows you to represent your objects as a string / text, which can then be stored, sent etc.

Another example would be XML, which is also often used for serialization.

Example:

Object

var Person = new Object();
Person.FirstName = "Foo";
Person.LastName = "Foo";
Person.Age = 24;

JSON example serialization

    {
        Person:
        {
             FirstName: "Foo",
             LastName: "Bar",
             Age: 42
        }
    }

XML example serialization

    <Person>
        <FirstName>Foo</FirstName>
        <LastName>Bar</LastName>
        <Age>42</Age>
    </Person>

Since you asked "what is going on under the hood": Simply explained, the serialization algorithm would iterate all properties of the object and store their names and values in a string of the requested format (JSON, XML, ...)

Upvotes: 0

Related Questions