mathnet / mathnet-spatial

Math.NET Spatial
http://spatial.mathdotnet.com
MIT License
378 stars 133 forks source link

External Serialization? #5

Closed cdrnet closed 10 years ago

cdrnet commented 10 years ago

Currently types like Point2D implement IXmlSerializable and also include helper fields like SerializeAsElements.

I tend to prefer external serialization approaches, i.e. an external serializer that can format specific objects or collections of them into XML, binary or other formats. Fields like SerializeAsElements would no longer be needed. To my understanding there already is a partially external serializer to a custom text format.

JohanLarsson commented 10 years ago

I don't understand what you mean by 'external serialization'

cdrnet commented 10 years ago

With internal serialization a type is responsible for its serialization (ISerializable, IXmlSerializable; .Net 1), with external serialization you typically have some static class or object that converts between different representations (e.g. ProtoBuf, Json, System.Runtime.Serialization, .Net 4). External serializers sometimes use data contract attributes to delegate the control back to the type.

The problem with serialization is that this is usually very application specific. You mentioned you have some legacy requirements regarding attributes vs elements, other users may have different requirements. Then we have parse logic that works well with the tests. In short, serialization cannot really be a responsibility of the type but of the application and use case.

In Math.NET Numerics we use external serialization exclusively, e.g. to convert between matrices and CSV, MatrixMarket or MATLAB binary streams/files.

A disadvantage of a pure external approach is that it does not integrate well with internal serializers but requires some manual work, e.g. when including points in a complex object tree that is required to be XML-serializable. I take it this is one of your scenarios?

In your particular application, do I understand it correctly that you require the same type to be serialized in either element or attribute style on case by case basis? Is this consistent within a serialized tree, or are there mixed cases where both happens within the same tree that is to be serialized?

JohanLarsson commented 10 years ago
cdrnet commented 10 years ago

Great! Personally I prefer attribute style as it is more compact and closer to SVG but I'll let you decide.

Ok, let's keep the types IXmlSerializable and the static Parse methods.

JohanLarsson commented 10 years ago

I removed the flags, probably forgot some. I kept ReadXml handling both element and attribute style.