ffyhlkain
ffyhlkain

Reputation: 57

Protobuf-net is not serializing? Output nearly plain text

I'm a beginner with protobuf and I'm having a problem I hope you can help me with:

I tried out [the example][1] of the protobuf-net page and got it running. I extended it, that not only one person is stored, but another data class which holds a list of 10000 persons. Then I serialized that data class and had a look into the output. I expected to see some binary stuff and was surprised to see nearly everything as plain text:

 π`Fred
Flat 1The Meadows
 π`Fred
Flat 1The Meadows
 π`Fred
Flat 1The Meadows
 π`Fred
Flat 1The Meadows
 π`Fred
...

My code is pretty simple:

namespace SNSClient.Assets.Scripts.GamePlay.Testing
{
    [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;}
    }

    [ProtoContract]
    class MyProtoDataHolder
    {
        [ProtoMember(1)]
        public List<Person> persons { get; set; }
    }
}

For serialization:

List<Person> dataList = new List<Person>();
for (int i = 0; i < 100000; i++)
{
    var person = new Person {
    id = 12345, Name = "Fred",
    Address = new Address {
    Line1 = "Flat 1",
    Line2 = "The Meadows"
    }
    };

    dataList.Add(person);
}
var data = new MyProtoDataHolder() {persons = dataList};

using (var file = File.Create(Application.dataPath + "/dataList.bin"))
{
    Serializer.Serialize(file, data);
}

I'm expecting that I'm missing something, 'cause the size of the output file is not what I would expect.

Thanks for help!

Upvotes: 2

Views: 490

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064284

Your data is dominated by text. The protobuf wire format encodes text as UTF-8, so it makes sense that:

  1. the files are almost readable (and contain the original text values clearly visible to most text editors)
  2. the file size isn't much different to the original content

Basically: looks fine to me. If you want it compressed, run it through something like GZipStream or DeflateStream too (in both directions, obviously). As a side note: if your real data will have lots of duplicate data, protobuf-net has some opt-in flags to allow things like object and string re-use (storing just a token, not the entire string each time) - however, it will be awkward to read that into other protocol buffers libraries (i.e. not protobuf-net), as this functionality is not part of the core specification.

Upvotes: 3

Related Questions