Does C # have native serialization?

9

In PHP there is a language serialization feature. I mean: I'm not talking about using JSON or XML , but I'm talking about a serialization in a language-specific format.

For example, if I want to serialize a array or a objeto , I can easily do this.

serialize(['a' => 1, 'b' => 2])

The result will be:

'a:2:{s:1:"a";i:1;s:1:"b";i:2;}'

If I want to deserialize this data, I can invoke the unserialize function to convert this data to a valid value in PHP.

See:

unserialize('a:2:{s:1:"a";i:1;s:1:"b";i:2;}')

The result will be:

[
  "a" => 1,
  "b" => 2,
]

In conclusion, this is a specific format, where the language itself will understand which data needs to be converted.

Is there something similar in C #? Is there any way to serialize native language data?

Note : Just reinforcing: I'm not talking about JSON or XML, but in a native way, as demonstrated in the PHP example.

    
asked by anonymous 02.01.2017 / 14:57

2 answers

10

Yes, it is possible, .Net provides this possibility through reflection. But do not worry, you do not have to do anything much, just say what you want to be serializable. Just use the Serializable attribute that the framework knows What you gotta do. And if you need some members not to participate in serialization you can use NonSerializable . p>

You can achieve serialization in different formats as needed, either text or binary ( example ) that is considered more or less obsolete, due to the difficulties it imposes. It takes some formatting. If you want to know if there is a format invented by .NET, only binary, JSON is usually used for text.

Obviously some cases need something more custom and standard serialization does not work. Then the solution is to implement the ISerializable interface with the logic that be more suitable for what you need.

You do not always have to ask to serialize or deserialize, the framework can handle this for you in cases where it knows it's what you need. Obviously it will not do it in non-serializable types.

There are some external libraries that are more performative and many people give preference to the native. One of them is NewtonSoft Json.NET , the most used nowadays (which is used by some frameworks ). Another is protobuf.NET created by an SE employee.

Documentation .

I do not really know the serialization of PHP, but it should work in a very similar way.

    
02.01.2017 / 15:49
-1

Microsoft has adopted Newtonsoft.NET, a third-party package adopted as Official, as the guy responsible for doing serializations within the .NET environment.

Its use is extremely simple, just first install the Json.NET package in your project via NuGet Packager Manager or via Packager Manager Console :

  

PM > Install-Package Newtonsoft.Json

Then, to serialize:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

To deserialize:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys

Or using LINQ:

JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));

JObject o = new JObject();
o["MyArray"] = array;

string json = o.ToString();
// {
//   "MyArray": [
//     "Manual text",
//     "2000-05-23T00:00:00"
//   ]
// }

To learn more, check out the official Json.NET page.

    
03.01.2017 / 09:04