Deserialize List

Go To StackoverFlow.com

3

I am rx this error on Deserialization:

Invalid wire-type; this usually means you have over-written a file without truncating or setting the length; see Using Protobuf-net, I suddenly got an exception about an unknown wire-type

That only mentions File truncation, but I am creating a new file

 Stopwatch sw = new Stopwatch();
            List<SomeClass> items = CreateSomeClass();
            sw.Start();
            using (var file = File.Create(fileName))
            {
                var model = CreateModel();
                model.Serialize(file, items);
                file.SetLength(file.Position);
            }
            sw.Stop();
            logger.Debug("Saving/serialization to {0} took {1} m/s", fileName, sw.ElapsedMilliseconds);
            sw.Reset();
            logger.Debug("Starting deserialzation...");
            sw.Start();
            using (var returnStream = new FileStream(fileName, FileMode.Open))
            {
                var model = CreateModel();
                var deserialized = model.Deserialize(returnStream, null, typeof(SomeClass));
            }
            logger.Debug("Retrieving/deserialization of {0} took {1} m/s", fileName, sw.ElapsedMilliseconds);

 public static TypeModel CreateModel()
    {
        RuntimeTypeModel model = TypeModel.Create();

        model.Add(typeof(SomeClass), false)
            .Add(1, "SomeClassId")
            .Add(2, "FEnum")
            .Add(3, "AEnum")
            .Add(4, "Thing")
            .Add(5, "FirstAmount")
            .Add(6, "SecondAmount")
            .Add(7, "SomeDate");
        TypeModel compiled = model.Compile();

        return compiled;
    }

 public enum FirstEnum
{ 
    First = 0,
    Second,
    Third
}
public enum AnotherEnum
{ 
    AE1 = 0,
    AE2,
    AE3
}
[Serializable()]
public class SomeClass
{
    public int SomeClassId { get; set; }
    public FirstEnum FEnum { get; set; }
    public AnotherEnum AEnum { get; set; }
    string thing;
    public string Thing
    {
        get{return thing;}
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentNullException("Thing");

            thing = value;
        }
    }
    public decimal FirstAmount { get; set; }
    public decimal SecondAmount { get; set; }
    public decimal ThirdAmount { get { return FirstAmount - SecondAmount; } }
    public DateTime? SomeDate { get; set; }
}

I am new to Protobuf-net so is there anything obvious I am doing wrong/missing?

2012-04-05 16:44
by Eric


2

You are serializing it as a list, and deserializing it asa single item. This is a problem. Either use DeserializeItems, or: instead of

typeof(SomeClass)

Pass

typeof(List<SomeClass>)

The DeserializeItems is probably slightly faster (for various reasons, it has to do extra work when Deserialize is called with a list type as the operand).

2012-04-05 20:55
by Marc Gravell
that was it thank - Eric 2012-04-05 21:43
Wait, does this mean

[ProtoMember(10)] public List<Unit> fUnits;

won’t work? I’m getting that error deserializing the object containing this field - David Dunham 2014-07-03 21:34

@DavidDunham no, it doesn't mean that at all; unrelate - Marc Gravell 2014-07-03 22:27


1

Given that the error seems to indicate that the deserialization reader wants to read additional byte(s), try running your code without the file.SetLength(file.Position), which should not be needed (the file stream knows its length).

2012-04-05 20:01
by Philipp Schmid
Ads