Skip to content

ITransportSerializer Example

hyperletter edited this page Sep 10, 2012 · 1 revision

To use TypedSocket you need to supply Hyperletter with a serializer. There is no built in serializer because we dont want to have any external dependencies (the built in .net serializers wants to make a mess in your code with the required [Serializble] attribute).

Here is a example built on top of the great open source project JSON.NET:

public class JsonTransportSerializer : ITransportSerializer {
    public byte[] Serialize(object obj) {
        return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
    }

    public T Deserialize<T>(byte[] value) {
        return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(value));
    }

    public T Deserialize<T>(byte[] value, Type concreteType) {
        return (T) JsonConvert.DeserializeObject(Encoding.UTF8.GetString(value), concreteType);
    }
}
Clone this wiki locally