BT

Thanks to a Utility Library by Erik Bergstedt, Protobuf for .NET Just Got Easier

by Jonathan Allen on Mar 07, 2016 |

Protobuf is a wire format created by Google. Formally known as Protocol Buffers, it is a compact binary format that discards readability and extensibility for size and performance. In order to deserialize a Protobuf message, the client and server must agree on the specific fields in the message ahead of time. (Contrast this with XML or JSON, where the client can examine the message structure at runtime.)

In the .NET version of Protobuf, attributes are used to tag properties with “id” numbers. These are, in turn, used to construct the binary arrays that contain the message. Here is an example,

[ProtoContract]
class Address
{
    [ProtoMember(1)]
   
public string Line1 { get; set; }
    [ProtoMember(2)]
    public string Line2 { get; set; }
}

Using Protobuf in C# can be somewhat tedious, as you normally have to manipulate streams. But with Erik Bergstedt’s Protobuffer library, serialization and deserialization has been reduced to simple one-liners.

var serialize = _simpleSerializer.ToByteArray(GetPerson());
Person deserialize = _simpleDeserializer.FromByteArray<Person>(serialize);

You may be wondering why not add these to the Protobuf directly. Erik wites,

Because this library is opinionated, and hides some functionality which can be undesirable in some situations. There is a sacrifice to pay for simplicity.

For example I am opinionated on how to create the streams, and also the support for GZIP is outside the scope of the original protobuf.

We must be careful to differentiate between bloat and features. If my things were part of the original protobuf, I'd argue it would tangent bloat and not feature.

Protobuffer is offered as open source under the MIT License. Protobuf itself is offered by Google for a variety of platforms and programming languages including C++, Java, JavaScript, Python, and Ruby.

Rate this Article

Relevance
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
General Feedback
Bugs
Advertising
Editorial
Marketing
InfoQ.com and all content copyright © 2006-2016 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT

We notice you’re using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.