How to serialize Sorted Set to Json in c#? - c#

I have a SortedSet object which contains the objects of my class MyClass. However, when I use JsonConvert.Serialize() method, then the output is empty list, i.e. the value of MyClass are not serialized. With simple Lists there is no problems. What is the reason?

Related

C# deserialization from base type fails trying to convert to derived type

I have two classes, one being derived from another.
Before creating a Derived class, I serialized a List of Base class objects to a binary file and now I try to deserialize them back, however even if I try assigning the deserialization result to an object or not assigning it to anything at all, I get an exception: "Cannot convert type Base to type Derived" at the line that calls the Deserialize method. If I rename the Derived class to Base, however, it does deserialize, leaving the Derived properties that aren't present in Base as null.
Can I and if I can, how can I have the BinaryFormatter not try to convert the Base class to Derived during deserialization?
I found the answer myself.
The Base class had a list of class A objects, and each of them were referencing the Base class at the moment I serialized the Base object list. At the moment I tried deserializing it, A objects were referencing Derived objects in code, but in the file they still referenced Base, which caused the conversion error. So the problem was in that I versioned my Base objects up to Derived, but forgot to version the A objects.

Deserialize XML to a list of objects that share the same base class

I'm using c#. I have a base class, we'll call BaseClass. I have three other classes that derive from BaseClass. I have a List<BaseClass> that contains different instances of the derived classes. I serialize this list to XML using the XmlSerializer and it looks fine.
However, when I try to deserialize the XML back to the objects, I'm getting XML exception errors thrown pointing to the first object.
How can I deserialize this list of objects? When I serialize it, I pass the possible types, do I need to set this when I deserialize so the deserialize method knows what objects it could possibly have to create? Is there another way? Is it possible?
You will need to use the XmlInclude attribute to specify all the subclasses of the base class. See How to XML serialize child class with its base class or http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute(v=vs.110).aspx

Serialize List<Func<T>>

I have a class which contains a List<Func<T>>. The class is serializable and will be transferred over WCF. Now, I have problems with contained List<Func<T>>. What can I do that this list will also be serializable?
How would you expect a Func<T> to be serialized? It's a pointer to a function - such a thing cannot be serialized.

Adding a method to an object that is serialized, but it doesnt' need to be serialized

I have an object that is currently serialized, and I want to do add a method to this class. The method shouldn't be serialized as it doesn't hold any state or anything, but it will modify the classes data but this doesn't need to be serialized (it will parse the value of a serializable string property).
How can I add this method? Or should I have this method on another utility class instead?
Methods aren't serialized. Only properties and fields are serialized. And Properties are only eligible for serialization if using DataContractSerializer (BinaryFormatter will only serialize Properties' underlying fields).
Adding this method won't be a problem, methods aren't serialized.
If you have access to the class then as I understand it serialization deals with properties and fields. Methods don't need to be and don't get serialized. They process the data
Quick answer: Most cases shouldn't affect unserialize an object.
Only fields are serialized. Methods and properties arent (properties are methods). The XmlSerializer is an exception; it only looks at public properties, and those must be public get / set if you are to deserialize them.

Serialize/deserialize objects - order of fields matters?

Is it possible that DataContractSerializer wrongly deserializes an object if the fields are not in the "correct" (whatever that means) order?
The classes that I try to serialize/deserialize do not have order-attributes placed on fields/properties. Yet one of my fields always gets deserialized as null even though it has a non-null value (it actually contains a list of strings).
When I moved two XML elements in serialized file around to match the order in another XML example (for which deserialization worked without problems) everything started to work.
This makes no sense to me but maybe someone knows better. ;)
To be eligible for correct serialization / serialization by the DataContractSerializer, all of the following must be true.
The class that must be serialized must have SerializableAttribute or DataContractAttribute set;
The properties and fields of the class that must be serialized require the DataMemberAttribute set;
The datatype of the serializable property or field must be serializable (i.e., have a public ctor and inherit ISerializable);
The class that must be serialized must implement IExtensibleDataObject;
Note: serializable fields can be either public or private.
Members must be in alphabetical order or you should use the Order-property of the DataMemberAttribute.
So, the order of the declaration does matter. If members are not in alphabetical order, they are skipped. Look up this answer at StackOverflow for an example, perhaps it applies to your case.

Categories