What Is Serialization

What is the meaning of serialization in programming languages?

Say you have two applications that run on two different physical machines. Both of the applications need to exchange data that is commonly used by both applications. These application talk to each other to share the data with some mediums, these mediums could be a file-system, tcp or udp connections or any other suitable network protocol or may be direct in-memory data exchange. Any of these mediums would only understand data that is described in the form of a series of bits. So when one application needs to send a value 10 to another, the value 10 would be sent as its binary representation 1010 and you would also pass some information that describes 1010. This meta information will also be a series of bits that the other application can easily understand. That was easy though.

Lets take another example, wherein these two apps need to exchange a more complex, non primitive data-type. Lets say they need to exchange the objects of type Book where Book is a custom defined class in your application and both the applications have the definition of type Book.

public class Book
{
Book() { }

public long BookId { get;set; }
public string Author { get;set; }
public string Title { get;set; }
}

How will you exchange the objects of type book between the two applications? To be able to share the object between two apps, you will need to be able to convert the Book objects into binary representation. This is where serialization comes into the picture.

With the help of Serialization you can define how an object can be converted into its binary representation. The receiving application would do the reverse process, that is De-Serialization, that constructs a Book object from its binary representation.

What is data serialization?

What is serialization?

Serialization encodes objects into another format.

For example you have an array in PHP like this:

$array = array("a" => 1, "b" => 2, "c" => array("a" => 1, "b" => 2));

And then you want to store it in file or send to other application.

There are several format choices, but the idea is the same:
The array has to be encoded (or you could say "translated"), into text or bytes, that can be written to a file or sent via the network.

For example, in PHP, if you:

$data = serialize($array);

you will get this:

a:3:{s:1:"a";i:1;s:1:"b";i:2;s:1:"c";a:2:{s:1:"a";i:1;s:1:"b";i:2;}}

This is PHP's particular serializing format that PHP understands, and it works vice versa, so you are able to use it to deserialize objects.

For example, you stored a serialized array in a file, and you want it back in your code as an array:

$array = unserialize($data);

But you could choose a different serialization format, for example, JSON:

$json = json_encode($array);

will give you this:

{"a":1,"b":2,"c":{"a":1,"b":2}}

The result is not only easily saved, read by human eye, or sent via network, but is also understandable by almost every other language (JavaScript, Java, C#, C++, ...)

Conclusion

Serialization translate objects to another format, in case you want to store or share data.

Are there any situations, where you cannot do anything, but serialize it?

No. But serialization usually makes things easier.

Are JSON and PHP format the only possible formats?

No, no, no and one more time no. There are plenty of formats.

  • XML (e.g. using a schema like WSDL or XHTML)
  • Bytes, Protobuf, etc.
  • Yaml
  • ...
  • ...
  • Your own formats (you can create your own format for serialization and use it, but that is a big thing to do and is not worth it, most of the time)

Why do we use serialization?

Technically on the low-level, your serialized object will also end up as a stream of bytes on your cable or your filesystem...

So you can also think of it as a standardized and already available way of converting your objects to a stream of bytes. Storing/transferring object is a very common requirement, and it has less or little meaning to reinvent this wheel in every application.

As other have mentioned, you also know that this object->stream_of_bytes implementation is quite robust, tested, and generally architecture-independent.

This does not mean it is the only acceptable way to save or transfer an object: in some cases, you'll have to implement your own methods, for example to avoid saving unnecessary/private members (for example for security or performance reasons). But if you are in a simple case, you can make your life easier by using the serialization/deserialization of your framework, language or VM instead of having to implement it by yourself.

Hope this helps.

What is object serialization?

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.

What is a serialized object in programming?

Serialization usually refers to the process of converting an abstract datatype to a stream of bytes (You sometimes serialize to text, XML or CSV or other formats as well. The important thing is that it is a simple format that can be read/written without understanding the abstract objects that the data represents). When saving data to a file, or transmitting over a network, you can't just store a MyClass object, you're only able to store bytes. So you need to take all the data necessary to reconstruct your object, and turn that into a sequence of bytes that can be written to the destination device, and at some later point read back and deserialized, reconstructing your object.

What is serialization in Java?

Serializable is a marker interfaces that tells the JVM it can write out the state of the object to some stream (basically read all the members, and write out their state to a stream, or to disk or something). The default mechanism is a binary format. You can also use it to clone things, or keep state between invocations, send objects across the network etc.

You can let eclipse generate one for you (basically just a long random but unique ID). That means you can control when you think a class would be compatible with a serialized version, or not.

(Note: that all the non transient member variables must be of a serializable class, or you will get an error - as the JVM will recurse through the structure writing out the state of each object down to the level of writing primitives to the ObjectOutputStream).



Related Topics



Leave a reply



Submit