|
| 1 | +#region -- License Terms -- |
| 2 | +// |
| 3 | +// MessagePack for CLI |
| 4 | +// |
| 5 | +// Copyright (C) 2010-2012 FUJIWARA, Yusuke |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +#endregion -- License Terms -- |
| 20 | + |
| 21 | +using System; |
| 22 | +using System.Diagnostics; |
| 23 | +using System.IO; |
| 24 | + |
| 25 | +using MsgPack; |
| 26 | +using MsgPack.Serialization; |
| 27 | +using NUnit.Framework; // For running checking |
| 28 | + |
| 29 | +namespace Samples |
| 30 | +{ |
| 31 | + /// <summary> |
| 32 | + /// A sample code for explore MessagePackObject. |
| 33 | + /// </summary> |
| 34 | + [TestFixture] |
| 35 | + public class HandlingDynamicObjectSample |
| 36 | + { |
| 37 | + [Test] |
| 38 | + public void SerializeThenDeserialize() |
| 39 | + { |
| 40 | + // They are object for just description. |
| 41 | + var targetObject = |
| 42 | + new PhotoEntry // See Sample01_BasicUsage.cs |
| 43 | + { |
| 44 | + Id = 123, |
| 45 | + Title = "My photo", |
| 46 | + Date = DateTime.Now, |
| 47 | + Image = new byte[] { 1, 2, 3, 4 }, |
| 48 | + Comment = "This is test object to be serialize/deserialize using MsgPack." |
| 49 | + }; |
| 50 | + targetObject.Tags.Add( "Sample" ); |
| 51 | + targetObject.Tags.Add( "Excellent" ); |
| 52 | + var stream = new MemoryStream(); |
| 53 | + |
| 54 | + // Set using Map instead of Array to serialize complex object. See Sample03 for details. |
| 55 | + SerializationContext.Default.SerializationMethod = SerializationMethod.Map; |
| 56 | + |
| 57 | + // 1. Create serializer instance. |
| 58 | + var serializer = SerializationContext.Default.GetSerializer<PhotoEntry>(); |
| 59 | + |
| 60 | + // 2. Serialize object to the specified stream. |
| 61 | + serializer.Pack( stream, targetObject ); |
| 62 | + |
| 63 | + // Set position to head of the stream to demonstrate deserialization. |
| 64 | + stream.Position = 0; |
| 65 | + |
| 66 | + // 3. Unpack MessagePackObject to get raw representation. |
| 67 | + var rawObject = Unpacking.UnpackObject( stream ); |
| 68 | + // You can read MPO tree via Unpacker |
| 69 | + // var unpacker = Unpacker.Create( stream ); |
| 70 | + |
| 71 | + // Check its type |
| 72 | + Debug.WriteLine( "Is array? {0}", rawObject.IsArray ); // IsList is alias |
| 73 | + Debug.WriteLine( "Is map? {0}", rawObject.IsMap ); // IsDictionary is alias |
| 74 | + Debug.WriteLine( "Type: {0}", rawObject.UnderlyingType ); |
| 75 | + |
| 76 | + // Gets serialized fields. |
| 77 | + // Note: When the object was serialized as array instead of map, use index instead. |
| 78 | + var asDictionary = rawObject.AsDictionary(); |
| 79 | + Debug.WriteLine( "Id : {0}({1})", asDictionary[ "Id" ], asDictionary[ "Id" ].UnderlyingType ); |
| 80 | + // String is encoded as utf-8 by default. |
| 81 | + Debug.WriteLine( "Title : {0}({1})", asDictionary[ "Title" ], asDictionary[ "Title" ].UnderlyingType ); |
| 82 | + // Non-primitive is serialized as complex type or encoded primitive type. |
| 83 | + // DateTimeOffset is encoded as array[2]{ticks,offset} |
| 84 | + Debug.WriteLine( "Date : {0}({1})", asDictionary[ "Date" ], asDictionary[ "Date" ].UnderlyingType ); |
| 85 | + // byte[] is byte[], as you know. |
| 86 | + Debug.WriteLine( "Image : {0}({1})", asDictionary[ "Image" ], asDictionary[ "Image" ].UnderlyingType ); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments