2

I'm currently doing a POC to prove that Protobuf-net is faster than the native C# serializer. But I'm experiencing a bug? on the serialize response.

Here is the Error

nativeResult

<?xml version="1.0"?>
<userInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <age>22</age>
  <education>Cavite State University</education>
  <name>Ronald Estacion</name>
  <address>
    <city>Cavite</city>
    <country>PH</country>
    <street>Torjak Street</street>
    <subd>Yari ka ngayon</subd>
  </address>
</userInformation>

protoresult

Proto

Here is my code.

var nativeSerializer = new XmlSerializer(typeof(UserInformation));
var nativeStream = new MemoryStream();
var protoStream = new MemoryStream();
nativeSerializer.Serialize(nativeStream, CreateDetaulUserInformation());
var nativeResult = Encoding.UTF8.GetString(nativeStream.ToArray());
Serializer.Serialize(protoStream, CreateDetaulUserInformation());
var protoResult = Encoding.UTF8.GetString(protoStream.ToArray());

Here is the UserInformation object.

//[ProtoContract]
[XmlType("address")]
public class Address
{
    //[ProtoMember(1)]
    [XmlElement("city", Order = 1)]
    public string City { get; set; }

    //[ProtoMember(2)]
    [XmlElement("country", Order = 2)]
    public string Country { get; set; }

    //[ProtoMember(3)]
    [XmlElement("street", Order = 3)]
    public string Street { get; set; }

    //[ProtoMember(4)]
    [XmlElement("subd", Order = 4)]
    public string Subdivision { get; set; }
}

//[ProtoContract]
[XmlType("userInformation")]
public class UserInformation
{
    //[ProtoMember(1)]
    [XmlElement("age", Order = 1)]
    public int Age { get; set; }

    //[ProtoMember(2)]
    [XmlElement("education", Order = 2)]
    public string Education { get; set; }

    //[ProtoMember(3)]
    [XmlElement("name", Order = 3)]
    public string Name { get; set; }

    //[ProtoMember(4)]
    [XmlElement("address", Order = 4)]
    public Address Address { get; set; }
}

NOTE: I've also tried using ProtoContract as the attribute but it has the same result. Protobuf-net supports XmlType aswell so i commented those attributes.

DevEstacion
  • 1,947
  • 1
  • 14
  • 28
  • What is the encoding for your XML file? – jhenninger Apr 01 '15 at 03:49
  • What you call the "native C# serializer" is the NET XML Serializer. The output is XML (text). ProtoBuf is a *binary* serializer. You are comparing apples and tomatoes. The unprintable leading chars are the serialized binary representation of 22 (age). Everything is working as it is supposed to. Compare the output of the NET BinaryFormatter to ProtoBuf for a more similar comparison. – Ňɏssa Pøngjǣrdenlarp Apr 01 '15 at 04:38
  • @Plutonix how do i achieve an xml string output? – DevEstacion Apr 01 '15 at 05:28
  • @DevEstacion base-64 can be transferred inside xml - note that doing so introduces unnecessary overheads, though – Marc Gravell Apr 01 '15 at 15:57

1 Answers1

2

Here's your error:

var protoResult = Encoding.UTF8.GetString(protoStream.ToArray());

protobuf is a binary protocol; it is not text, and is specifically not UTF8. What you are doing is attempting to decode a UTF-8 buffer into a string when the input data is not a UTF-8 buffer.

If you want to turn a raw byte[] into a string, then Convert.ToBase64String is your friend. But if you can avoid doing that: it is better to treat it as binary end-to-end if possible.

As a very minor thing: there's no need to call ToArray() here - that forces an extra array copy. If possible, use GetBuffer() combined with .Length (the underlying buffer is oversized); i.e.

string base64 = Convert.ToBase64String(
    protoStream.GetBuffer(), 0, (int)protoStream.Length);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The problem is that we have a legacy webservice, it accepts Xml request and Xml response, so I was finding a way to make a faster serialization/deserialization of the xmls – DevEstacion Apr 02 '15 at 01:04
  • @DevEstacion protobuf-net is *not* an xml serializer; it talks "protobuf" – Marc Gravell Apr 02 '15 at 06:59
  • ♦ as i thought, i was just wondering if i could use it's speed in deserialization and serialization as xml. Thanks. – DevEstacion Apr 02 '15 at 07:01