private static void UnpackOne( Unpacker unpacker ) { if ( !unpacker.Read() ) { throw new UnpackException( "Cannot unpack MesssagePack object from the stream." ); } }
protected override T[] UnpackFromCore(Unpacker unpacker) { MessagePackSerializer <T> serializer = base.OwnerContext.GetSerializer <T>(); if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } int itemsCount = UnpackHelpers.GetItemsCount(unpacker); T[] array = new T[itemsCount]; if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } T val = default(T); if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { val = serializer.UnpackFrom(unpacker); } else { using (Unpacker unpacker2 = unpacker.ReadSubtree()) { val = serializer.UnpackFrom(unpacker2); } } array[i] = val; } return(array); }
protected override List <T> UnpackFromCore(Unpacker unpacker) { MessagePackSerializer <T> serializer = base.OwnerContext.GetSerializer <T>(); if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } int itemsCount = UnpackHelpers.GetItemsCount(unpacker); List <T> list = new List <T>(); if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } T item = default(T); if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = serializer.UnpackFrom(unpacker); } else { using (Unpacker unpacker2 = unpacker.ReadSubtree()) { item = serializer.UnpackFrom(unpacker2); } } list.Add(item); } return(list); }
public void TestNestedArray() { using (var stream = new MemoryStream()) { var packer = Packer.Create(stream); packer.PackArray( new[] { new MessagePackObject(new[] { new MessagePackObject("1-1"), new MessagePackObject("1-2"), new MessagePackObject("1-3") }), new MessagePackObject(new[] { new MessagePackObject("2-1"), new MessagePackObject("2-2"), new MessagePackObject("2-3") }), new MessagePackObject(new[] { new MessagePackObject("3-1"), new MessagePackObject("3-2"), new MessagePackObject("3-3") }), } ); stream.Position = 0; var unpacker = Unpacker.Create(stream); Assert.That(unpacker.Read()); using (Unpacker subtreeReader1 = unpacker.ReadSubtree()) { Assert.That(subtreeReader1.IsArrayHeader); Assert.That(subtreeReader1.ItemsCount, Is.EqualTo(3)); for (int i = 1; subtreeReader1.Read(); i++) { using (Unpacker subtreeReader2 = subtreeReader1.ReadSubtree()) { Assert.That(subtreeReader2.IsArrayHeader); Assert.That(subtreeReader2.ItemsCount, Is.EqualTo(3)); for (int j = 1; subtreeReader2.Read(); j++) { Assert.That(subtreeReader2.LastReadData.AsString(), Is.EqualTo(i + "-" + j)); } } } } } }
protected override IO.Ably.Message UnpackFromCore(MsgPack.Unpacker unpacker) { IO.Ably.Message result = default(IO.Ably.Message); result = new IO.Ably.Message(); int itemsCount0 = default(int); itemsCount0 = MsgPack.Serialization.UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; (i < itemsCount0); i = (i + 1)) { string key = default(string); string nullable7 = default(string); nullable7 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(IO.Ably.Message), "MemberName"); if (((nullable7 == null) == false)) { key = nullable7; } else { throw MsgPack.Serialization.SerializationExceptions.NewNullIsProhibited("MemberName"); } if ((key == "timestamp")) { System.Nullable <System.DateTimeOffset> nullable15 = default(System.Nullable <System.DateTimeOffset>); if ((unpacker.Read() == false)) { throw MsgPack.Serialization.SerializationExceptions.NewMissingItem(i); } if (((unpacker.IsArrayHeader == false) && (unpacker.IsMapHeader == false))) { nullable15 = this._serializer2.UnpackFrom(unpacker); } else { MsgPack.Unpacker disposable4 = default(MsgPack.Unpacker); disposable4 = unpacker.ReadSubtree(); try { nullable15 = this._serializer2.UnpackFrom(disposable4); } finally { if (((disposable4 == null) == false)) { disposable4.Dispose(); } } } if (nullable15.HasValue) { result.Timestamp = nullable15; } } else { if ((key == "name")) { string nullable14 = default(string); nullable14 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(IO.Ably.Message), "System.String name"); if (((nullable14 == null) == false)) { result.Name = nullable14; } } else { if ((key == "id")) { string nullable13 = default(string); nullable13 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(IO.Ably.Message), "System.String id"); if (((nullable13 == null) == false)) { result.Id = nullable13; } } else { if ((key == "encoding")) { string nullable12 = default(string); nullable12 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(IO.Ably.Message), "System.String encoding"); if (((nullable12 == null) == false)) { result.Encoding = nullable12; } } else { if ((key == "data")) { object nullable10 = default(object); if ((unpacker.Read() == false)) { throw MsgPack.Serialization.SerializationExceptions.NewMissingItem(i); } if (((unpacker.IsArrayHeader == false) && (unpacker.IsMapHeader == false))) { nullable10 = this._serializer1.UnpackFrom(unpacker); } else { MsgPack.Unpacker disposable2 = default(MsgPack.Unpacker); disposable2 = unpacker.ReadSubtree(); try { nullable10 = this._serializer1.UnpackFrom(disposable2); } finally { if (((disposable2 == null) == false)) { disposable2.Dispose(); } } } if (((nullable10 == null) == false)) { if (nullable10 is MsgPack.MessagePackObject) { nullable10 = ((MessagePackObject)nullable10).ToObject(); if (nullable10 is MessagePackObject[]) { result.Data = ((MessagePackObject[])nullable10).Select(x => x.ToObject()).ToArray(); } else { result.Data = nullable10; } } else { result.Data = nullable10; } } } else { if ((key == "connectionId")) { string nullable9 = default(string); nullable9 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(IO.Ably.Message), "System.String connectionId"); if (((nullable9 == null) == false)) { result.ConnectionId = nullable9; } } else { if ((key == "clientId")) { string nullable8 = default(string); nullable8 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(IO.Ably.Message), "System.String clientId"); if (((nullable8 == null) == false)) { result.ClientId = nullable8; } } else { unpacker.Skip(); } } } } } } } } return(result); }
/// <summary> /// Unpacks the message from a MessagePack object /// This method should not be called directly, use deserialize instead. /// </summary> /// <param name="unpacker">The unpacker</param> public void UnpackFromMessage(Unpacker unpacker) { string dcs, dac, dad, dbd, dbb, day, dau, dag, dai, daj, dak, dcg; int dbc; if (!unpacker.IsMapHeader) throw SerializationExceptions.NewIsNotMapHeader(); if (UnpackHelpers.GetItemsCount(unpacker) != MapCount) throw SerializationExceptions.NewUnexpectedArrayLength(MapCount, UnpackHelpers.GetItemsCount(unpacker)); for (var i = 0; i < MapCount; i++) { string key; if (!unpacker.ReadString(out key)) throw SerializationExceptions.NewUnexpectedEndOfStream(); switch (key) { case "DCS": { if (!unpacker.ReadString(out dcs)) throw SerializationExceptions.NewMissingProperty("dcs"); Dcs = dcs; break; } case "DAC": { if (!unpacker.ReadString(out dac)) throw SerializationExceptions.NewMissingProperty("dac"); Dac = dac; break; } case "DAD": { if (!unpacker.ReadString(out dad)) throw SerializationExceptions.NewMissingProperty("dad"); Dad = dad; break; } case "DBD": { if (!unpacker.ReadString(out dbd)) throw SerializationExceptions.NewMissingProperty("dbd"); Dbd = DateTime.Parse(dbd); break; } case "DBB": { if (!unpacker.ReadString(out dbb)) throw SerializationExceptions.NewMissingProperty("dbb"); Dbb = DateTime.Parse(dbb); break; } case "DBC": { if (!unpacker.ReadInt32(out dbc)) throw SerializationExceptions.NewMissingProperty("dbc"); Dbc = (Sex) dbc; break; } case "DAY": { if (!unpacker.ReadString(out day)) throw SerializationExceptions.NewMissingProperty("day"); Day = GetEyeColor(day); break; } case "DAU": { if (!unpacker.ReadString(out dau)) throw SerializationExceptions.NewMissingProperty("dau"); Dau = new Height {AnsiFormat = dau}; break; } case "DAG": { if (!unpacker.ReadString(out dag)) throw SerializationExceptions.NewMissingProperty("dag"); Dag = dag; break; } case "DAI": { if (!unpacker.ReadString(out dai)) throw SerializationExceptions.NewMissingProperty("dai"); Dai = dai; break; } case "DAJ": { if (!unpacker.ReadString(out daj)) throw SerializationExceptions.NewMissingProperty("daj"); Daj = daj; break; } case "DAK": { if (!unpacker.ReadString(out dak)) throw SerializationExceptions.NewMissingProperty("dak"); Dak = new PostalCode {AnsiFormat = dak}; break; } case "DCG": { if (!unpacker.ReadString(out dcg)) throw SerializationExceptions.NewMissingProperty("dcg"); Dcg = dcg; break; } case "ZAA": { if (!unpacker.Read()) throw SerializationExceptions.NewMissingProperty("zaa"); var ms = new MemoryStream(unpacker.LastReadData.AsBinary()); Image = Image.FromStream(ms); break; } case "ZAB": { if (!unpacker.Read()) throw SerializationExceptions.NewMissingProperty("zab"); Fingerprint = new Fingerprint {AsIsoTemplate = unpacker.LastReadData.AsBinary()}; break; } } } }