I would like to serialize a tree to a human-readable text format (the less storage space required, the better). I would like to achieve this without implementing parameterless constructors and public setters as required by the XmlSerializer class. What would be the standard way(s) to achieve this?
My model looks something like this:
public interface ISpecification<T>
{ //some stuff here }
// The tree will have some nodes like this
public abstract class CompositeSpecificationBase<T>
{
public ISpecification<T> Left { get; private set; }
public ISpecification<T> Right { get; private set; }
protected CompositeSpecificationBase(ISpecification<T> left, ISpecification<T> right)
{
Left = left;
Right = right;
}
}
public class AndSpecification<T> : CompositeSpecificationBase<T>, ISpecification<T>
{
public AndSpecification(ISpecification<T> left, ISpecification<T> right)
: base(left, right)
{}
// some stuff here
}
// At least one more implementation of CompositeSpecificationBase...
// There will be many leaf types like this
public class ExampleSpec : ISpecification<SomeEntity>
{
public int SomeProperty {get; private set;}
public ExampleSpec (int someProperty)
{
SomeProperty = someProperty;
}
}
I would suggest Json, for example using the Json.NET serializer: http://json.codeplex.com/