[This is preliminary documentation and is subject to change.]
Encapsulates the custom code for serializing and deserializing objects of
type TElement.
Namespace: Microsoft.Research.Naiad.SerializationAssembly: Microsoft.Research.Naiad (in Microsoft.Research.Naiad.dll) Version: 0.5.0.0 (0.5.0.0)
public interface CustomSerialization<TElement>
Type Parameters
- TElement
- The type of element to be serialized or deserialized.
The CustomSerializationTElement type exposes the following members.
| Name | Description |
---|
| Deserialize |
Deserializes into value the next element from the given
buffer.
|
| TrySerialize |
Attempts to serialize the given value into the given
buffer with limit bytes remaining.
|
Top
Implementations of this interface should have a no-argument constructor,
because the serialization code generator will attempt to instantiate serializers using the no-argument
constructor. A type constraint on
RegisterCustomSerializationTElement, TSerializer
ensures that this is the case for all registered serializers.
public unsafe class IntSerializer : CustomSerialization<int>
{
public unsafe int TrySerialize(int value, byte* buffer, int limit)
{
if (limit < 4)
return -1;
*(int*)buffer = value;
return 4;
}
public unsafe int Deserialize(out int value, byte* buffer, int limit)
{
value = *(int*)buffer;
return 4;
}
}
// Generated serialization code:
byte* currentPosition = ...;
int bytesRemaining = ...;
int toSerialize = ...;
IntSerializer serializer = new IntSerializer();
int bytesWritten = serializer.TrySerialize(value, currentPosition, bytesRemaining);
if (bytesWritten <= 0)
return false;
else
currentPosition += bytesWritten;
// Generated deserialization code:
byte* currentPosition = ...;
int bytesRemaining = ...;
int toDeserialize;
IntSerializer deserializer = new IntSerializer();
int bytesRead = deserializer.Deserialize(out toDeserialize, currentPosition, bytesRemaining);
currentPosition += bytesRead;
bytesRemaining -= bytesRead;
Reference