What is serialization? When to use? How to implement in C#?

Asked

Viewed 9,155 times

26

[Serializable]
public class Pessoa
{
    public string Nome { get; set; }
    public string Cpf { get; set; }
}
  • There’s only one kind of serialization?
  • What alternatives not to need serialize an object?

2 answers

21


Serialization: What is?

In computer science, in the context of data storage and transmission, serialization is the process of saving or transliterating one object on another on a storage medium (such as a computer file or memory buffer) or transmitting it over a network connection, either in binary form or in text format such as XML or JSON. This byte series can be used to recreate an object with the same internal state as the original.

Source: http://en.wikipedia.org/wiki/Serialization (adapted)

There is only one type of serialization?

No. There are several types of serialization, the difference being the final data representation format. For example, using the method Json, library Newtonsoft.Json, you will get a JSON serialization. Using the object System.Xml.Serialization.XmlSerializer, you can convert (or serialize) an object in XML format.

In JSON

var objeto = new { nome = "Nome", valor = "Valor" };
return Json(objeto, JsonRequestBehavior.AllowGet);

Upshot

{ 'nome': 'Nome', 'valor': 'Valor' }

In XML

I’ll use your class:

[Serializable]
public class Pessoa
{
    public string Nome { get; set; }
    public string Cpf { get; set; }
}

Use:

var pessoa = new Pessoa { Nome = "Nome", Cpf = "123.456.789-01" };
XmlSerializer x = new XmlSerializer(pessoa.GetType());
x.Serialize(Console.Out, pessoa);

Upshot

<?xml version="1.0" encoding="IBM437"?>
 <pessoa xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
 .org/2001/XMLSchema">
    <Nome>Nome</Nome>
    <Cpf>123.456.789-01</Cpf>
</pessoa>

How to implement in C#?

Serialization is already implemented in C# for different formats. Serialization should be seen first as a concept, and then as a language resource.

You can even implement your serialization to the format you want, but the ideal is to use components ready to avoid extra work.

  • 1

    Gypsy, if possible could you create a small example of Serialization/deserialization? So that I understand better the concept as well as the way to implement in a project.

  • Okay, I’ll put it to you.

  • Very good @Gypsy, the only problem is that I went to test the example returned me this error: Only public types can be processed.

  • 1

    @NULL Missing a public somewhere in your code.

14

Only complementing the answer already given by @Ciganomorrisonmendez.

Sometimes it is simply not possible to avoid serialization:

  • transmit an object over the network

  • transmit an object between Appdomains

  • if you want to save an object to disk

In all these cases, a reference to the live object simply doesn’t make sense.

  • 6

    It should not even be "avoidable". The concept is super important for data exchanges between modules and applications.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.