I obviously am doing something wrong, but I cannot figure it out and not find the documentation.
I am experimenting with proto-buf for .NET by Marc Gravell and trying to serialize and deserialize objects. When an object contains a string that is "too long" (did not try to determine the size threshold, but it is several hundred bytes), the string does not deserialize correctly for me.
This is my code:
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text; using ProtoBuf; namespace ConsoleApplication1 { public class Program { [ProtoContract] public class test { [ProtoMember(1)] public int i; [ProtoMember(2)] public string s1; [ProtoMember(3)] public string s2; [ProtoMember(4)] public char[] arrchars; [ProtoMember(5)] public Dictionary<int, string> Dict = new Dictionary<int, string>(); } static void Main(string[] args) { test var1 = new test(); var1.i = 10; var1.s1 = "Hello"; var1.arrchars = new char[] {'A', 'B', 'C'}; var1.Dict.Add(10, "ten"); var1.Dict.Add(5, "five"); var1.s2 = new String('X', 520); string s = PBSerializer.Serialize(typeof (test), var1); test var2 = null; PBSerializer.Deserialize(s, out var2); } public static class PBSerializer { public static string Serialize(Type objType, object obj) { MemoryStream stream = new MemoryStream(); ProtoBuf.Serializer.Serialize(stream, obj);
var2.s2 is not identical to var1.s2 - it has an extra character at the beginning of the line and truncates most of the end of the line. If, however, I change the length of var1.s2 to a small number (say 52 instead of 520 characters), my problem disappears, but I need to be able to serialize long strings. I assume this is due to what I am doing wrong with setting PrefixStyle (?) Or maybe I am not using the correct encoding (?). However, trial and error did not help me figure it out.
I am using .NET 3.5 and have tried it with versions 444 and 450 with the same result.
Thanks.
source share