0

I'm looking for a Serializer to persist my classes in text format (not binary). But...

I'm already using protobuf for binary serialization. It works pretty fine. As a side note, I would have prefer not to deal with field id (index) like with protobuf.

Before closing or voting to close this question, please consider these points:

  • The specificity of this question
  • If other question really apply to my requirements and are not too old

I'm looking for a serializer with the following properties:

  • Easy to use
  • Serialize in text (readable) either Json or XML would be fine
  • Free
  • Is documented
  • Support versioning easily (obsolete field, type change, property name change, ...)
  • Uses Attribute to define items to serialize (or not serialize)
  • Does not uses an index (ID like Protobuf)
  • Be able like Protobuf, to deserialize an object directly without any constructor. Be able to instanciate an object either if the object does not have any public constructor and does not have any constructor with no arguments.
  • Does not require me to change my class or member accessibility, ie:
    • Does not need default constructor
    • Can serialize fields
    • Can skip public property (when marked to do so)

Others points not essential:

  • The speed is not important
  • Open source is a nice bonus
  • Has some examples is a nice bonus

Some examples of what I prefer to not use:

  • Microsoft XMLSerializer and JsonSerializer does require default constructor.
  • I have hard time using Microsoft-DataContractSerializer, an easier solution would be welcome.
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • You might be able to customize Json.NET to do what you need. Look at [this answer](https://stackoverflow.com/a/41871975) to [How does JSON deserialization in C# work](https://stackoverflow.com/q/41870132), you will see there is an option for `FormatterServices.GetUninitializedObject` to be called. You can also force serialization of fields not properties, see [JSON.Net: Force serialization of all private fields and all fields in sub-classes](https://stackoverflow.com/q/24106986). – dbc May 13 '20 at 16:48
  • But in general recommendation requests are off topic here. – dbc May 13 '20 at 16:50
  • The Net Xml Serializer is very easy to use provided the data is in classes. You just need a couple of instructions to serialize the parent node and serialize will automatically serialize all the descendant classes and properties. – jdweng May 13 '20 at 16:52
  • @jdweng, the Microsoft XmlSerializer request a default constructor and cannot serialize fields. It forces you to modifiy your class to fits its too specific requirements. That's precisely what I want to avoid. – Eric Ouellet May 13 '20 at 17:06
  • @dbc, I'm not sure about your links. The first link is about Newtonsoft.Json where it seems to use Constructor with parameters at last. But I can't see how it is possible and also it seems that it can instanciate the object without using a constructor like Protobuf does. That's what I want. I will specify that in my question in order to be clearer. – Eric Ouellet May 13 '20 at 17:12
  • @dbc, Just to clarify and fix my mistake in my previous comment. Newtonsoft.Json does not seems to be able to instanciate an object without constructor. – Eric Ouellet May 13 '20 at 17:21
  • I've used Xml Serializer thousands of times. To use default serialization you do not need to modify any classes and will serialize all public properties. – jdweng May 13 '20 at 17:54
  • https://stackoverflow.com/questions/8254503/how-to-pass-arguments-to-a-non-default-constructor – Alexei Levenkov May 13 '20 at 18:02
  • @jdweng, my classes already exists. I have some with no default constructor and some properties that are only public read. I do not want to modify (adapt) my classes for serialization. I design my classes in order to make sure that they will be used the proper way. By adding a default constructor or by turning a property read/write when its proper design is to be read only only to satisfy a serializer appears to me silly. I makes good effort to do good class design, I think that I should be able to serialize without having to modify my actual classes. – Eric Ouellet May 14 '20 at 02:24
  • @AlexeiLevenkov, Thanks! But i'm really looking for a serializer that will be able to create instance directly without having to have a default constructor. I do not want to create twisted ways to create my object using a constructor with parameters knowing that there is a very more simple way to do it. In last resort, I know I can use Microsoft c# DataContractSerializer. – Eric Ouellet May 14 '20 at 02:27
  • You are correct. The default constructors already exist so no changes in your code is required. – jdweng May 14 '20 at 08:09

0 Answers0