private void RegisterSimpleCodec <T>(SimpleCodec <T> codec) { Contracts.Assert(!_loadNameToCodecCreator.ContainsKey(codec.LoadName)); Contracts.Assert(!_simpleCodecTypeMap.ContainsKey(codec.Type.RawKind)); _loadNameToCodecCreator.Add(codec.LoadName, codec.GetCodec); _simpleCodecTypeMap.Add(codec.Type.RawKind, codec); }
public void ObjectGraphCodecExampleCodecCommentSample() { // Example code begins here // var p1 = new Person("John"); p1.Children.Add(new Person("Peter")); p1.Children.Add(new Person("Mary")); // Create a sample object graph var factory = new PublicPropertyObjectGraphFactory(); var graph1 = factory.CreateObjectGraph(p1); // Encode that object graph var stream = new MemoryStream(); var codec = new SimpleCodec(); codec.EncodeObjectGraph(graph1, stream); // Print the result Console.WriteLine(Encoding.UTF8.GetString(stream.GetBuffer())); // Decode the object graph var graph2 = codec.DecodeObjectGraph(stream); // Result should be false, because we encoded only tree top nodes // from the whole object graph var result = new ObjectGraphComparer().Compare(graph1, graph2); Trace.WriteLine("{0}", result ? "Object graphs are equal!" : "Object graphs are NOT equal!"); // Example code ends here // Assert.False(result); }
public void ObjectGraphCodecExampleCodecCommentSampleRoundtrip() { var p1 = new Person("John"); p1.Children.Add(new Person("Peter")); p1.Children.Add(new Person("Mary")); var factory = new PublicPropertyObjectGraphFactory(); var graph1 = factory.CreateObjectGraph(p1); var stream = new MemoryStream(); var codec = new SimpleCodec(); codec.EncodeObjectGraph(graph1, stream); // Round trip original object graph graph1 = codec.DecodeObjectGraph(stream); var graph2 = codec.DecodeObjectGraph(stream); var result = new ObjectGraphComparer().Compare(graph1, graph2); Assert.True(result); }