Shuttle.Core.Serialization
PM> Install-Package Shuttle.Core.Serialization
An implementation of the ISerializer
interface is used to serialize objects into a Stream
.
The DefaultSerializer
makes use of the standard .NET xml serialization functionality.
Methods
Serialize
Stream Serialize(object message);
Task<Stream> SerializeAsync(object message);
Returns the message object
as a Stream
.
Deserialize
object Deserialize(Type type, Stream stream);
Task<object> DeserializeAsync(Type type, Stream stream);
Deserializes the Stream
into an object
of the given type.
ISerializerRootType
The ISerializerRootType
interface is an optional interface that serializer implementations can use that allows the developer to specify explicit object types contained within a root type.
The DefaultSerializer
implements this interface and it is recommended that you explicitly register types with the same name, but in different namespaes, that will be serialized within the same root type to avoid any conflicts later down the line.
For instance, the following two types will cause issues when used in the root Complex
type as they both serialize to the same name and the .Net serializer cannot seem to distinguish the difference:
namespace Serializer.v1
{
public class MovedEvent
{
public string Where { get; set; }
}
}
namespace Serializer.v2
{
public class MovedEvent
{
public string Where { get; set; }
}
}
namespace Serializer
{
public class Complex
{
public v1.MovedEvent { get; set; }
public v2.MovedEvent { get; set; }
}
}
By explicitly specifying the types the DefaultSerializer
will add a namespace that will cause the types to be correctly identified.
AddSerializerType
void AddSerializerType(Type root, Type contained);
Specify the contained
tpe that is used within te root
type somewhere.