protobuf-net - protocol buffers library for idiomatic .net

27
PROTOBUF-NET Larry Nung

Upload: larry-nung

Post on 20-Jan-2017

116 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: protobuf-net - Protocol Buffers library for idiomatic .NET

PROTOBUF-NETLarry Nung

Page 2: protobuf-net - Protocol Buffers library for idiomatic .NET

AGENDAIntroductionGetting startedDecorate class

ProtoContractDataContractRuntimeTypeModel

Serialize /DeSerialize DataSerialize /DeSerialize streamSerialize /DeSerialize textSerialize /DeSerialize file

Generate proto fileReferenceQ & A

2

Page 3: protobuf-net - Protocol Buffers library for idiomatic .NET

INTRODUCTION3

Page 4: protobuf-net - Protocol Buffers library for idiomatic .NET

INTRODUCTION Protocol Buffers library for idiomatic .NET Contract based serializer for .NET code

4

Page 5: protobuf-net - Protocol Buffers library for idiomatic .NET

GETTING STARTED5

Page 6: protobuf-net - Protocol Buffers library for idiomatic .NET

GETTING STARTED

Reference library

Decorate class

Serialize /DeSerialize

Data

6

Page 7: protobuf-net - Protocol Buffers library for idiomatic .NET

REFERENCE LIBRARY

7

Page 8: protobuf-net - Protocol Buffers library for idiomatic .NET

REFERENCE LIBRARY

8

Page 9: protobuf-net - Protocol Buffers library for idiomatic .NET

DECORATE CLASS[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;} }

9

Page 10: protobuf-net - Protocol Buffers library for idiomatic .NET

SERIALIZE DATAvar person = new Person { Id = 12345, Name = "Fred", Address = new Address { Line1 = "Flat 1", Line2 = "The Meadows" } }; using (var file = File.Create("person.bin")) { Serializer.Serialize(file, person); }

10

Page 11: protobuf-net - Protocol Buffers library for idiomatic .NET

DESERIALIZE DATAPerson newPerson; using (var file = File.OpenRead("person.bin"))

{ newPerson =

Serializer.Deserialize<Person>(file); }

11

Page 12: protobuf-net - Protocol Buffers library for idiomatic .NET

DECORATE CLASS12

Page 13: protobuf-net - Protocol Buffers library for idiomatic .NET

PROTOCONTRACT[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;} }

13

Page 14: protobuf-net - Protocol Buffers library for idiomatic .NET

PROTOCONTRACT[ProtoContract] [ProtoInclude(10, typeof(Male))] [ProtoInclude(11, typeof(Female))] class Person { [ProtoMember(1)] public string Name { get; set; }} [ProtoContract] class Male : Person { } [ProtoContract] class Female : Person { } 14

Page 15: protobuf-net - Protocol Buffers library for idiomatic .NET

PROTOCONTRACT[ProtoContract(SkipConstructor = true, ImplicitFields =

ImplicitFields.AllFields)] class Person { public string Name { get; set; } public Person(string name) { Name = name; } [ProtoAfterDeserialization] void Validate() { if (string.IsNullOrEmpty(Name)) throw new Exception("Empty name"); } }

15

Page 16: protobuf-net - Protocol Buffers library for idiomatic .NET

DATACONTRACT[DataContract] class Person { [DataMember(Order = 1)] public string Name { get; set; } }

16

Page 17: protobuf-net - Protocol Buffers library for idiomatic .NET

RUNTIMETYPEMODELclass Person { public string Name { get; set; } } …RuntimeTypeModel.Default .Add(typeof(Person), false); .Add(1, "Name");

17

Page 18: protobuf-net - Protocol Buffers library for idiomatic .NET

SERIALIZE /DESERIALIZE DATA18

Page 19: protobuf-net - Protocol Buffers library for idiomatic .NET

SERIALIZE /DESERIALIZE STREAMprivate static void SerializeToStream<T>(T

obj, Stream stream) { Serializer.Serialize(stream, obj); }

private static T DeSerializeFromStream<T>(Stream stream) {

return Serializer.Deserialize<T>(stream); }

19

Page 20: protobuf-net - Protocol Buffers library for idiomatic .NET

SERIALIZE /DESERIALIZE TEXTprivate static string SerializeToText<T>(T obj) { using (var ms = new MemoryStream(1024)) { SerializeToStream(obj, ms); return Convert.ToBase64String(ms.ToArray()); } }

private static T DeSerializeFromText<T>(string text) { var buffer = Convert.FromBase64String(text); using (var ms = new MemoryStream(buffer)) { return DeSerializeFromStream<T>(ms); } }

20

Page 21: protobuf-net - Protocol Buffers library for idiomatic .NET

SERIALIZE /DESERIALIZE FILEpublic virtual void SerializeToFile<T>(T obj, String file, Int32 bufferSize

= 1024) { using (var fs = File.Open(file, FileMode.Create, FileAccess.Write)) { using (var bs = new BufferedStream(fs, bufferSize)) { SerializeToStream(obj, bs); bs.Flush(); } } } public virtual T DeSerializeFromFile<T>(String file, Int32 bufferSize) { using (var fs = File.Open(file, FileMode.Open, FileAccess.Read,

FileShare.ReadWrite)) { using (var bs = new BufferedStream(fs, bufferSize)) { return DeSerializeFromStream<T>(bs); } } }

21

Page 22: protobuf-net - Protocol Buffers library for idiomatic .NET

GENERATE PROTO FILE22

Page 23: protobuf-net - Protocol Buffers library for idiomatic .NET

GENERATE PROTO FILEprivate static void

GenerateProtoFile<T>(string file) {

File.WriteAllText(file,Serializer.GetProto<T>());

}

23

Page 24: protobuf-net - Protocol Buffers library for idiomatic .NET

REFERENCE24

Page 25: protobuf-net - Protocol Buffers library for idiomatic .NET

REFERENCE NuGet Gallery | protobuf-net 2.1.0

https://www.nuget.org/packages/protobuf-net/

25

Page 26: protobuf-net - Protocol Buffers library for idiomatic .NET

Q&A26

Page 27: protobuf-net - Protocol Buffers library for idiomatic .NET

QUESTION & ANSWER

27