Serialisation proposal - API

Skip to end of metadata
Go to start of metadata

Just banging down some initial ideas for the API, based on a prototype.  Using C# versions.

Self-serialising objects:

public interface IFudgeSerializable
{
    void Serialize(FudgeMsg msg, IFudgeSerializationContext context);
    void Deserialize(FudgeMsg msg, int dataVersion, IFudgeDeserializationContext context);
}

Surrogate for when an object can't (de)serialise itself - e.g. it's owned by someone else, or it's immutable:

public interface IFudgeSerializationSurrogate
{
    void Serialize(object obj, FudgeMsg msg, IFudgeSerializationContext context);
    object Deserialize(FudgeMsg msg, int dataVersion, IFudgeDeserializationContext context);
}

Context used to handle serialising sub-objects (and maybe hold some other useful stuff later):

public interface IFudgeSerializationContext
{
    FudgeMsg AsSubMsg(object obj);
    int AsRef(object obj);
}

Similar for deserialisation:

public interface IFudgeDeserializationContext
{
    T FromMsg<T>(FudgeMsg msg) where T : class;
    T FromRef<T>(int? refId) where T : class;
    T FromField<T>(IFudgeField field) where T : class;
    void Register(object obj);
}

With IFudgeDeserializationContext, FromField would normally be used as it handles both nested and referenced objects.  Register is needed when you have a surrogate - just after construction the object must be registered with the framework in case it is referenced in one of its child objects.

Example self-serialising class:

public class Tick : IFudgeSerializable
{
    public string Ticker { get; set; }
    public double Bid { get; set; }
    public double Offer { get; set; }

    #region IFudgeSerializable Members

    public void Serialize(FudgeMsg msg, IFudgeSerializationContext context)
    {
        msg.Add("ticker", Ticker);
        msg.Add("bid", Bid);
        msg.Add("offer", Offer);
    }

    public void Deserialize(FudgeMsg msg, int dataVersion, IFudgeDeserializationContext context)
    {
        Ticker = msg.GetString("ticker");
        Bid = msg.GetDouble("bid") ?? 0.0;
        Offer = msg.GetDouble("offer") ?? 0.0;
    }

    #endregion
}
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.