Return to Snippet

Revision: 21274
at December 8, 2009 16:30 by jimfred


Initial Code
static void Main(string[] args)
{
   // Add to references: 
   //   jskeet-dotnet-protobufs-e53a4f8\src\ProtocolBuffers\bin\release\Google.ProtocolBuffers.dll
   // ...or equivalent.
   
   // Build data.

   ProtoBufDemo.ProtoBufDemoMsg.Builder b = new ProtoBufDemo.ProtoBufDemoMsg.Builder
   {
      Str = "my string",
      Sub = new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 1, I2 = 2, I3 = 3 }.Build(),
      I = 123,
      // SubsList = new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild[]{...}, Can't do this yet - see 
      // http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx
   };

   b.SubsList.Add(new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 1, I2 = 2, I3 = 3 }.Build());
   b.SubsList.Add(new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 11, I2 = 22, I3 = 33 }.Build());
   b.SubsList.Add(new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 111, I2 = 222, I3 = 333 }.Build());
   
   ProtoBufDemo.ProtoBufDemoMsg m1 = b.Build();

   // Done building data.

   string s1 = m1.ToString();

   // Convert to binary array.
   byte[] ba = m1.ToByteArray();

   // reconstitute.
   ProtoBufDemo.ProtoBufDemoMsg m2 = ProtoBufDemo.ProtoBufDemoMsg.ParseFrom(ba);

   string s2 = m1.ToString();

   System.Diagnostics.Debug.Assert(s1 == s2);

   return;

}


// Proto file:

// protoBufDemo.proto
// Demo usage of Jon Skeet's C# Protocol Buffers
// Build using:
// %protoc% --descriptor_set_out=protoBufDemo.protobin --proto_path=protos;.\ --include_imports .\protoBufDemo.proto
// %protogen% protoBufDemo.protobin
// where protoc=jskeet-dotnet-protobufs-e53a4f8\lib\protoc.exe
// and jskeet-dotnet-protobufs-e53a4f8\src\ProtoGen\bin\Debug\ProtoGen.exe
// The output file will be ProtoBufDemoClass.cs

package Features;
 
import "google/protobuf/csharp_options.proto";

option (google.protobuf.csharp_file_options).namespace = "ProtoBufDemo";
option (google.protobuf.csharp_file_options).umbrella_classname = "ProtoBufDemoClass";

option optimize_for = SPEED;

message  ProtoBufDemoMsg
{
   message  ProtoBufDemoChild
   {
      required int32 i1=1;
      required int32 i2=2;
      required int32 i3=3;
   }

 
   required string            str  = 1;
   required ProtoBufDemoChild sub  = 2;
   repeated ProtoBufDemoChild subs = 3;
   required int32             i    = 4;
}

Initial URL
http://code.google.com/p/protobuf-csharp-port/wiki/GettingStarted

Initial Description
This is a simple demo of Google Protocol Buffers in C# using Jon Skeet's protobuf C# port. The sample data type includes an array/list and nested types. The binary data is a smaller, faster alternative to XML or JSON. The C# built-in binary serializers created massive buffers that were 4x the size of JSON buffers.

Initial Title
Google Protocol Buffers, usage in C#, using Jon Skeet's protobuf-csharp-port

Initial Tags

                                

Initial Language
C#