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?
26
[Serializable]
public class Pessoa
{
public string Nome { get; set; }
public string Cpf { get; set; }
}
21
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)
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.
var objeto = new { nome = "Nome", valor = "Valor" };
return Json(objeto, JsonRequestBehavior.AllowGet);
{ 'nome': 'Nome', 'valor': 'Valor' }
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);
<?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>
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.
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.
It should not even be "avoidable". The concept is super important for data exchanges between modules and applications.
Browser other questions tagged c# .net serialization
You are not signed in. Login or sign up in order to post.
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.
– Laerte
Okay, I’ll put it to you.
– Leonel Sanches da Silva
Very good @Gypsy, the only problem is that I went to test the example returned me this error: Only public types can be processed.
– Laerte
@NULL Missing a
public
somewhere in your code.– Leonel Sanches da Silva