2

It is possible to serialize only a few properties of the class? ex:

public class Client
{
   [ProtoMember(1)]
   public int Id { get; set; } 

   [ProtoMember(2)]
   public string Name { get; set; }

   public int Age { get; set; } 

   [ProtoMember(3)]
   public string Guid { get; set; } 
}

Thus, I get the value of "Id" incorrect. The other properties are correct. If I fill it with "[ProtoMember]" all the properties, the value of "id" is correct. Why?


Actually the error is caused by other reasons you may be able to help me.

I convert String to Stream to perform tasks. In time to reverse this conversion I have error in the value of Id


var cli = new Client
{ Id = 222, Guid = "52369-fe5r6-74e2g-j1i4e", Age = 29, Name = "José"};

//Serialize
var ms = new MemoryStream();
Serializer.Serialize(ms, cli);
ms.Position = 0;
var reader = new StreamReader(ms);
var strStream = reader.ReadToEnd();

//Deserialize
var ms2 = new MemoryStream(Encoding.UTF8.GetBytes(strStream));


var obj = Serializer.Deserialize<Client>(ms2);

Thus, any value above 127, is converted to a different Int. Ex: 3104751

My conversion is wrong?

Obs: I'm sorry the poor English

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
José
  • 23
  • 3
  • 1
    (I'm the author of protobuf-net) the class as written is fine (as long as you add `[ProtoContract]`, as suggested by alfonso). Please clarify what you mean by "the value of "Id" incorrect" - what do you see? – Marc Gravell Aug 18 '11 at 05:42
  • (added an answer re your edit) – Marc Gravell Aug 19 '11 at 06:33

3 Answers3

2

It is. Just make sure to include the [ProtoContract] attribute for the class and use the exact same contract when serializing and deserializing.

alf
  • 18,372
  • 10
  • 61
  • 92
  • José, without seeing your code it will be impossible to help you. Paste some of your code here so people can see what you're trying to do. If you question isn't related to this one you should create another one to avoid confusion. – alf Aug 18 '11 at 19:54
2

With your edit, the error is obvious; you are using Encoding to process arbitrary data into a string. Don't worry, I see this a lot (hence this post).

The intention of a text encoding is:

string     =>      byte[]     =>     string
        (encode)           (decode)

the important thing here is that the byte[] has a specific important defined by the encoding. What you want is base-64:

byte[]     =>      string     =>     byte[]
        (encode)           (decode)

which you do via:

byte[] raw = ms.ToArray();
string s = Convert.ToBase64String(raw);

and then:

byte[] bytes = Convert.FromBase64String(s);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

The problem is because of the encoding. Why are you reading the stream and then creating another one? Something like this should work:

var cli = new Client{ Id = 222, Guid = "52369-fe5r6-74e2g-j1i4e", Age = 29, Name = "José"};

//Serialize
var ms = new MemoryStream();
Serializer.Serialize(ms, cli);
ms.Position = 0;

//Deserialize
var obj = Serializer.Deserialize<Client>(ms);
alf
  • 18,372
  • 10
  • 61
  • 92
  • Because I convert the Stream to String, String use that in some processes, and then convert back pro object. – José Aug 18 '11 at 20:27
  • You should consider using a Stream and not converting it to a string. They are complete different concepts. However, I think you could solve your problem by indicating the UTF8 codification in the StreamReader constructor: new StreamReader(ms, System.Text.Encoding.UTF8) – alf Aug 18 '11 at 20:36
  • I tested this code and the error persists. I need to convert to string because I do some operations involving the database and other business rules. – José Aug 18 '11 at 20:42
  • Here's a guy who needs to do exactly the same thing as you. Look at the answer. It seems you should use Convert.ToBase64String: http://stackoverflow.com/questions/6905585/protobuf-net-serialize-to-string-and-store-in-database-then-de-serialize – alf Aug 18 '11 at 20:54