What happens when I annotate a class with serializable? Can I serialize them into json or XML? How is this done?
A:
This allows the class to be serialized with the available serializers, like xmlserializer and the binary serializer. However, more annotations are probably required to output it in the desired xml layout (or JSON)
WardB
2010-10-17 09:49:51
`XmlSerializer` does not care about `SerializableAttribute`.
Matti Virkkunen
2010-10-17 09:51:01
Yep, you are right. It's only the binary serializer.
WardB
2010-10-17 10:04:53
+2
A:
The SerializableAttribute is just a marker attribute that does nothing.
When using it, you are claiming that every component of your class can be serialized - in order to actually serialize the class you need to use a serializer such as the XmlSerializer or DataContractSerializer.
To use the DataContractSerializer, you need to decorate your classes and class members with different attributes (DateMemberAttribute, for example). It give you better control over the output XML than the XmlSerializer does and can also output JSON.
Oded
2010-10-17 09:51:26
It's not a marker interface, because it's not an interface. It's an attribute.
Matti Virkkunen
2010-10-17 09:54:08
I think he's referring to the general term where it's a run time marker.
Preet Sangha
2010-10-17 10:02:59