tomba / netserializer

Fast(est?) .Net Serializer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serializer for Custom Array not generated...

jogibear9988 opened this issue · comments

I've a class with a Array:

class LcInformationResponse {
public LcInformations[] Infos {get;set;}
}

for this class, no correct serializer/deserializer is build!

I could do my own serializer for it, but then i have to use GetTypeIdAndSerializer, and also the 2 delegates have to be public!

Ok, it was my fault that the serializer was not created.

It works now, but it would be nice if I can access those properties so a class like this is possible:

class ArraySerializer : IStaticTypeSerializer
{
public bool Handles(Type type)
{
if (type.IsArray)
{
if (type.GetElementType().Namespace.StartsWith("BlaBla"))
{
return true;
}
}
return false;
}

    public IEnumerable<Type> GetSubtypes(Type type)
    {
        yield break;
    }

    public MethodInfo GetStaticWriter(Type type)
    {
        return typeof(ArraySerializer).GetMethod("Serialize", BindingFlags.Static | BindingFlags.Public);
    }

    public MethodInfo GetStaticReader(Type type)
    {
        return typeof(ArraySerializer).GetMethod("Deserialize", BindingFlags.Static | BindingFlags.Public);
    }

    public static void Serialize(Serializer serializer, Stream stream, object ob)
    {
        if (ob == null)
        {
            Primitives.WritePrimitive(stream, (int) 0);
            return;
        }

        SerializeDelegate<object> del;

        var array = (Array) ob;
        Primitives.WritePrimitive(stream, array.Length + 1);
        uint id = serializer.GetTypeIdAndSerializer(ob.GetType(), out del);
        Primitives.WritePrimitive(stream, id);


        for (int n = 0; n < array.Length; n++)
        {
            var obj = array.GetValue(n);
            if (obj != null)
            {
                id = serializer.GetTypeIdAndSerializer(obj.GetType(), out del);
                Primitives.WritePrimitive(stream, id);
                del(serializer, stream, obj);
            }
            else
            {
                Primitives.WritePrimitive(stream, (uint) 0);
            }
        } 
    }

    public static void Deserialize(Serializer serializer, Stream stream, out object ob)
    {
        int l1;
        uint l2;
        uint l3;

        Primitives.ReadPrimitive(stream, out l1);

        if (l1 == 0)
        {
            ob = null;
            return;
        }

        Primitives.ReadPrimitive(stream, out l2);
        var type = serializer.GetDeserializeTypeFromId(l2);
        var array = Array.CreateInstance(type.GetElementType(), l1 - 1);
        ob = array;
        object obj;
        for (int i = 0; i < l1 - 1; i++)
        {
            Primitives.ReadPrimitive(stream, out l3);
            if (l3 != 0)
            {
                var del = serializer.GetDeserializeTrampolineFromId(l3);
                del(serializer, stream, out obj);
                array.SetValue(obj, i);
            }
        }    
    }
}

Sorry, I don't follow. What's the purpose of this class? What properties you need to access?

i will create a pull req

You see it here: #36