Posted By

jimfred on 12/08/09


Tagged


Versions (?)

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


 / Published in: C#
 

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

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.

  1. static void Main(string[] args)
  2. {
  3. // Add to references:
  4. // jskeet-dotnet-protobufs-e53a4f8\src\ProtocolBuffers\bin\release\Google.ProtocolBuffers.dll
  5. // ...or equivalent.
  6.  
  7. // Build data.
  8.  
  9. ProtoBufDemo.ProtoBufDemoMsg.Builder b = new ProtoBufDemo.ProtoBufDemoMsg.Builder
  10. {
  11. Str = "my string",
  12. Sub = new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 1, I2 = 2, I3 = 3 }.Build(),
  13. I = 123,
  14. // SubsList = new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild[]{...}, Can't do this yet - see
  15. // http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx
  16. };
  17.  
  18. b.SubsList.Add(new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 1, I2 = 2, I3 = 3 }.Build());
  19. b.SubsList.Add(new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 11, I2 = 22, I3 = 33 }.Build());
  20. b.SubsList.Add(new ProtoBufDemo.ProtoBufDemoMsg.Types.ProtoBufDemoChild.Builder { I1 = 111, I2 = 222, I3 = 333 }.Build());
  21.  
  22. ProtoBufDemo.ProtoBufDemoMsg m1 = b.Build();
  23.  
  24. // Done building data.
  25.  
  26. string s1 = m1.ToString();
  27.  
  28. // Convert to binary array.
  29. byte[] ba = m1.ToByteArray();
  30.  
  31. // reconstitute.
  32. ProtoBufDemo.ProtoBufDemoMsg m2 = ProtoBufDemo.ProtoBufDemoMsg.ParseFrom(ba);
  33.  
  34. string s2 = m1.ToString();
  35.  
  36. System.Diagnostics.Debug.Assert(s1 == s2);
  37.  
  38. return;
  39.  
  40. }
  41.  
  42.  
  43. // Proto file:
  44.  
  45. // protoBufDemo.proto
  46. // Demo usage of Jon Skeet's C# Protocol Buffers
  47. // Build using:
  48. // %protoc% --descriptor_set_out=protoBufDemo.protobin --proto_path=protos;.\ --include_imports .\protoBufDemo.proto
  49. // %protogen% protoBufDemo.protobin
  50. // where protoc=jskeet-dotnet-protobufs-e53a4f8\lib\protoc.exe
  51. // and jskeet-dotnet-protobufs-e53a4f8\src\ProtoGen\bin\Debug\ProtoGen.exe
  52. // The output file will be ProtoBufDemoClass.cs
  53.  
  54. package Features;
  55.  
  56. import "google/protobuf/csharp_options.proto";
  57.  
  58. option (google.protobuf.csharp_file_options).namespace = "ProtoBufDemo";
  59. option (google.protobuf.csharp_file_options).umbrella_classname = "ProtoBufDemoClass";
  60.  
  61. option optimize_for = SPEED;
  62.  
  63. message ProtoBufDemoMsg
  64. {
  65. message ProtoBufDemoChild
  66. {
  67. required int32 i1=1;
  68. required int32 i2=2;
  69. required int32 i3=3;
  70. }
  71.  
  72.  
  73. required string str = 1;
  74. required ProtoBufDemoChild sub = 2;
  75. repeated ProtoBufDemoChild subs = 3;
  76. required int32 i = 4;
  77. }

Report this snippet  

You need to login to post a comment.