Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most flexible serialization for .NET objects, yet simple to implement?

I would like to serialize and deserialize objects without having to worry about the entire class graph.

Flexibility is key. I would like to be able to serialize any object passed to me without complete attributes needed throughout the entire object graph.

That means that Binary Serialization is not an option as it only works with the other .NET Platforms. I would also like something readable by a person, and thus decipherable by a management program and other interpreters.

I've found problems using the DataContract, JSON, and XML Serializers.

  • Most of these errors seem to center around Serialization of Lists/Dictionaries (i.e. XML Serializable Generic Dictionary).
  • "Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."

Please base your answers on actual experiences and not theory or reading of an article.

like image 657
mrbradleyt Avatar asked Sep 20 '08 00:09

mrbradleyt


People also ask

How many types of serialization are there in C#?

Basic and custom serialization Binary and XML serialization can be performed in two ways, basic and custom. Basic serialization uses . NET to automatically serialize the object.

What is serialization in .NET with example?

Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred. .

What is the serialization in .NET how is it affecting to .NET programming?

Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache.

What are the types of serialization available in NET?

There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization. Binary serialization is the process where you convert your . NET objects into byte stream.


2 Answers

Have you considered serializing to JSON instead of XML?

Json.NET has a really powerful and flexible serializer that has no problems with Hashtables/generic dictionaries and doesn't require any particular attributes. I know because I wrote it :)

It gives you heaps of control through various options on the serializer and it allows you to override how a type is serialized by creating a JsonConverter for it.

In my opinion JSON is more human readable than XML and Json.NET gives the option to write nicely formatted JSON.

Finally the project is open source so you can step into the code and make modifications if you need to.

like image 143
James Newton-King Avatar answered Sep 24 '22 04:09

James Newton-King


If I recall it works something like this with a property:

[XmlArray("Foo")] [XmlArrayItem("Bar")] public List<BarClass> FooBars { get; set; } 

If you serialized this you'd get something like:

<Foo>     <Bar />     <Bar /> </Foo> 

Of course, I should probably defer to the experts. Here's more info from MS: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayitemattribute.aspx

Let me know if that works out for you.

like image 32
Ian Suttle Avatar answered Sep 23 '22 04:09

Ian Suttle