0
votes

I want to serialize using protobuf-net to string and deserialize back to object. It works fine if I serialize to file like .bin However the code below throws an exception while deserializing. Any ideas?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using ProtoBuf;

namespace Proto
{
class Program
{
    public long MemUsed = 0;
    NetDataContractSerializer ser = new NetDataContractSerializer();

    static void Main(string[] args)
    {
        var person = new Person {
            Id = 12345, Name = "Fred",
            Address = new Address 
            {
                Line1 = "Flat 1",
                Line2 = "The Meadows"
            }
        };
        Console.WriteLine("Serializing .....");
        var result = Serialize<Person>(person);
        Console.WriteLine(" Serialization Complete .....");
        Console.WriteLine("Press key to Deserialize ....");
        Console.ReadKey();
        var person2 = new Person();
        DeSerialize<Person>(ref person2,result);
        Console.WriteLine(person2.Name);
        Console.ReadLine();
    }

    public static string Serialize<T>(T myObj)
    {
        string retVal = "";            
        using (MemoryStream memStream = new MemoryStream())
        {
            Serializer.Serialize<T>(memStream, myObj);
            memStream.Position = 0;
            retVal = new StreamReader(memStream).ReadToEnd();
        }
        return (retVal);          
    }

    public static void DeSerialize<T>(ref T myObj, string xmlString)
    {
        byte[] byteArray = Encoding.ASCII.GetBytes(xmlString); 
        MemoryStream stream = new MemoryStream(byteArray,0,byteArray.Length);
        stream.Capacity = Convert.ToInt32(stream.Length);
        myObj = Serializer.Deserialize<T>(stream);
        stream.Close();       
    }
}

[ProtoContract]  
class Person {
    [ProtoMember(1)] 
    public int Id {get;set;}
    [ProtoMember(2)]
    public string Name { get; set; }
    [ProtoMember(3)]
    public Address Address {get;set;}
}     
[ProtoContract] 
class Address {  
    [ProtoMember(1)] 
    public string Line1 {get;set;}
    [ProtoMember(2)]
    public string Line2 {get;set;}
}
}
2
The parameter "xmlString" is particularly misleading - this is certainly not XML :p - Marc Gravell

2 Answers

4
votes

You are using a TextReader to process a non-text binary chunk. This will not work; essentially you have corrupted the binary by trying to UTF decode something that wasn't UTF, then re-encoded the (nonsensical) string you obtained.

Either return binary (for example a byte[]), or if it must be string: use base-64 via Convert.ToBase64String etc.

See also: http://marcgravell.blogspot.com/2010/03/binary-data-and-strings.html

0
votes

I posted a similar question with protobuf-net here and the answer shows you how to do the base64 converting.

protobuf-net Serialize To String and Store in Database Then De Serialize