public static void UnpackCollectionTo <T>(Unpacker unpacker, MessagePackSerializer <T> serializer, IEnumerable <T> collection, Action <T> addition) { if (unpacker == null) { throw new ArgumentNullException("unpacker"); } if (serializer == null) { throw new ArgumentNullException("serializer"); } if (collection == null) { throw new ArgumentNullException("collection"); } if (addition == null) { throw new ArgumentNullException("addition"); } if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } #if !UNITY Contract.EndContractBlock(); #endif // !UNITY int count = GetItemsCount(unpacker); for (int i = 0; i < count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } T item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = serializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = serializer.UnpackFrom(subtreeUnpacker); } } addition(item); } }
protected internal sealed override KeyValuePair <TKey, TValue> UnpackFromCore(Unpacker unpacker) { if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } TKey key = unpacker.LastReadData.IsNil ? default(TKey) : this._keySerializer.UnpackFrom(unpacker); if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } TValue value = unpacker.LastReadData.IsNil ? default(TValue) : this._valueSerializer.UnpackFrom(unpacker); return(new KeyValuePair <TKey, TValue>(key, value)); }
public void Read() { Create(); Assert.AreEqual(unpacker.Read(), '0'); Assert.AreEqual(unpacker.Peek(), '1'); Assert.AreEqual(unpacker.ReadNext(), '2'); Assert.AreEqual(unpacker.ReadString(), "23456"); Assert.AreEqual(unpacker.Read(), ' '); unpacker.SkipWhiteSpace(); Assert.AreEqual(unpacker.Read(), 'H'); Assert.AreEqual(unpacker.ReadString("elo!"), "ello!"); unpacker.SkipWhiteSpace(); Assert.AreEqual(unpacker.Read(), 'W'); Debug.WriteLine($"line :{unpacker.LineCount} col :{unpacker.CollumCount}"); Assert.AreEqual(unpacker.CollumCount, 3); Assert.AreEqual(unpacker.LineCount, 2); Assert.AreEqual(unpacker.Indent, 1); }
protected internal override TestValueTypeWrapper UnpackFromCore(Unpacker unpacker) { Assert.That(unpacker.IsArrayHeader); Assert.That(unpacker.ItemsCount, Is.EqualTo(1L)); Assert.That(unpacker.Read()); return(new TestValueTypeWrapper { Value = this._serializer0.UnpackFrom(unpacker) }); }
protected internal override Complex UnpackFromCore(Unpacker unpacker) { if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var real = unpacker.LastReadData.AsDouble(); if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var imaginary = unpacker.LastReadData.AsDouble(); return(new Complex(real, imaginary)); }
protected override Vector2D UnpackFromCore(Unpacker unpacker) { var x = unpacker.LastReadData.AsDouble(); unpacker.Read(); var y = unpacker.LastReadData.AsDouble(); return(new Vector2D(x, y)); }
private void UnpackToCore(Unpacker unpacker, IDictionary <TKey, TValue> collection, int itemsCount) { for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } TKey key; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { key = this._keySerializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { key = this._keySerializer.UnpackFrom(subtreeUnpacker); } } if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } TValue value; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { value = this._valueSerializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { value = this._valueSerializer.UnpackFrom(subtreeUnpacker); } } collection.Add(key, value); } }
private void UnpackToCore(Unpacker unpacker, object collection, int itemsCount) { for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { SerializationExceptions.ThrowMissingItem(i, unpacker); } object key; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { key = this._keySerializer.UnpackFrom(unpacker); } else { using (var subtreeUnpacker = unpacker.ReadSubtree()) { key = this._keySerializer.UnpackFrom(subtreeUnpacker); } } if (!unpacker.Read()) { SerializationExceptions.ThrowMissingItem(i, (key ?? String.Empty).ToString(), unpacker); } object value; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { value = this._valueSerializer.UnpackFrom(unpacker); } else { using (var subtreeUnpacker = unpacker.ReadSubtree()) { value = this._valueSerializer.UnpackFrom(subtreeUnpacker); } } this._add.InvokePreservingExceptionType(collection, key, value); } }
private void UnpackToCore( Unpacker unpacker, TDictionary collection, int itemsCount ) { for ( int i = 0; i < itemsCount; i++ ) { if ( !unpacker.Read() ) { SerializationExceptions.ThrowMissingKey( i, unpacker ); } TKey key; if ( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) { key = this._keySerializer.UnpackFrom( unpacker ); } else { using ( var subtreeUnpacker = unpacker.ReadSubtree() ) { key = this._keySerializer.UnpackFrom( subtreeUnpacker ); } } if ( !unpacker.Read() ) { SerializationExceptions.ThrowMissingItem( i, unpacker ); } TValue value; if ( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) { value = this._valueSerializer.UnpackFrom( unpacker ); } else { using ( var subtreeUnpacker = unpacker.ReadSubtree() ) { value = this._valueSerializer.UnpackFrom( subtreeUnpacker ); } } this.AddItem( collection, key, value ); } }
public static void UnpackArrayTo <T>(Unpacker unpacker, MessagePackSerializer <T> serializer, T[] array) { if (unpacker == null) { SerializationExceptions.ThrowArgumentNullException("unpacker"); } if (serializer == null) { SerializationExceptions.ThrowArgumentNullException("serializer"); } if (array == null) { SerializationExceptions.ThrowArgumentNullException("array"); } #if ASSERT Contract.Assert(unpacker != null); Contract.Assert(serializer != null); Contract.Assert(array != null); #endif // ASSERT if (!unpacker.IsArrayHeader) { SerializationExceptions.ThrowIsNotArrayHeader(unpacker); } #if ASSERT Contract.EndContractBlock(); #endif // ASSERT int count = GetItemsCount(unpacker); for (int i = 0; i < count; i++) { if (!unpacker.Read()) { SerializationExceptions.ThrowMissingItem(i, unpacker); } T item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = serializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = serializer.UnpackFrom(subtreeUnpacker); } } array[i] = item; } }
public static void UnpackCollectionTo <TDiscarded>(Unpacker unpacker, IEnumerable collection, Func <object, TDiscarded> addition) { if (unpacker == null) { SerializationExceptions.ThrowArgumentNullException("unpacker"); } if (collection == null) { SerializationExceptions.ThrowArgumentNullException("collection"); } if (addition == null) { SerializationExceptions.ThrowArgumentNullException("addition"); } #if ASSERT Contract.Assert(unpacker != null); Contract.Assert(collection != null); Contract.Assert(addition != null); #endif // ASSERT if (!unpacker.IsArrayHeader) { SerializationExceptions.ThrowIsNotArrayHeader(unpacker); } #if ASSERT Contract.EndContractBlock(); #endif // ASSERT int count = GetItemsCount(unpacker); for (int i = 0; i < count; i++) { if (!unpacker.Read()) { SerializationExceptions.ThrowMissingItem(i, unpacker); } MessagePackObject item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = _messagePackObjectSerializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = _messagePackObjectSerializer.UnpackFrom(subtreeUnpacker); } } addition(item); } }
private static void UnpackToCore(Unpacker unpacker, int count, MessagePackObjectDictionary collection) { for (int i = 0; i < count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var key = unpacker.LastReadData; if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } collection.Add(key, unpacker.LastReadData); } }
protected internal override KeyValuePair <string, DateTimeOffset> UnpackFromCore(Unpacker unpacker) { if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var key = unpacker.LastReadData.IsNil ? null : this._keySerializer.UnpackFrom(unpacker); if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var value = unpacker.LastReadData.IsNil ? default(DateTimeOffset) : this._valueSerializer.UnpackFrom(unpacker); return(new KeyValuePair <string, DateTimeOffset>(key, value)); }
protected override Time UnpackFromCore(Unpacker unpacker) { var timeString = unpacker.LastReadData.AsString(); unpacker.Read(); string zoneId = unpacker.LastReadData.AsString(); var time = Time.StringAsTime(zoneId, timeString, Encoding); return(time); }
private static void UnpackToCore(Unpacker unpacker, List <MessagePackObject> collection, int count) { for (var i = 0; i < count; i++) { if (!unpacker.Read()) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } collection.Add(unpacker.LastReadData); } }
protected internal override T UnpackFromCore(Unpacker unpacker) { var result = Activator.CreateInstance <T>(); var unpacked = 0; var asUnpackable = result as IUnpackable; if (asUnpackable != null) { asUnpackable.UnpackFromMessage(unpacker); return(result); } if (unpacker.IsArrayHeader) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { result = this.UnpackMemberValue(result, unpacker, itemsCount, ref unpacked, i, i); } } else { #if DEBUG && !UNITY Contract.Assert(unpacker.IsMapHeader); #endif // DEBUG && !UNITY var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { string name; if (!unpacker.ReadString(out name)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (name == null) { // missing member, drain the value and discard it. if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } continue; } result = this.UnpackMemberValue(result, unpacker, itemsCount, ref unpacked, this._memberIndexes[name], i); } } return(result); }
private void ReadArrayMetadata(Unpacker metadataUnpacker, out int[] lengths, out int[] lowerBounds) { if (!metadataUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!metadataUnpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } using (var lengthsUnpacker = metadataUnpacker.ReadSubtree()) { #if !UNITY lengths = this._int32ArraySerializer.UnpackFrom(lengthsUnpacker); #else lengths = this._int32ArraySerializer.UnpackFrom(lengthsUnpacker) as int[]; #endif // !UNITY } if (!metadataUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!metadataUnpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } using (var lowerBoundsUnpacker = metadataUnpacker.ReadSubtree()) { #if !UNITY lowerBounds = this._int32ArraySerializer.UnpackFrom(lowerBoundsUnpacker); #else lowerBounds = this._int32ArraySerializer.UnpackFrom(lowerBoundsUnpacker) as int[]; #endif // !UNITY } }
private void UnpackToCore(Unpacker unpacker, Dictionary <TKey, TValue> collection, int count) { for (int i = 0; i < count; i++) { if (!unpacker.Read()) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } TKey key; if (unpacker.IsCollectionHeader) { using (var subTreeUnpacker = unpacker.ReadSubtree()) { key = this._keySerializer.UnpackFromCore(subTreeUnpacker); } } else { key = this._keySerializer.UnpackFromCore(unpacker); } if (!unpacker.Read()) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (unpacker.IsCollectionHeader) { using (var subTreeUnpacker = unpacker.ReadSubtree()) { collection.Add(key, this._valueSerializer.UnpackFromCore(subTreeUnpacker)); } } else { collection.Add(key, this._valueSerializer.UnpackFromCore(unpacker)); } } }
public static ArraySegment <T> UnpackGenericArraySegmentFrom <T>(Unpacker unpacker, MessagePackSerializer <T> itemSerializer) { T[] array = new T[unpacker.ItemsCount]; for (int i = 0; i < array.Length; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } array[i] = itemSerializer.UnpackFrom(unpacker); } return(new ArraySegment <T>(array)); }
public static object BytesToObj(Type type, byte[] data) { if (data == null || data.Length == 0) { return(null); } IMessagePackSerializer ser = serDic.GetOrAdd(type, MessagePackSerializer.Create(type)); MemoryStream stream = new MemoryStream(data); Unpacker up = Unpacker.Create(stream); up.Read(); return(ser.UnpackFrom(up)); }
protected internal override void UnpackToCore(Unpacker unpacker, Queue collection) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { SerializationExceptions.ThrowMissingItem(i, unpacker); } collection.Enqueue(unpacker.LastReadData); } }
public void UnpackFromMessage(Unpacker unpacker) { if (unpacker.Read()) { this.Session = unpacker.Unpack <byte[]>(); } if (unpacker.Read()) { this.Request = unpacker.Unpack <byte[]>(); } if (unpacker.Read()) { this.Meta = unpacker.Unpack <Dictionary <string, object> >(); } if (unpacker.Read()) { this.Result = unpacker.UnpackDynamicObject(); } if (unpacker.Read()) { this.Bindings = unpacker.Unpack <Dictionary <string, object> >(); } }
private static void UnpackToCore(Unpacker unpacker, NameValueCollection collection, int keyCount) { for (var k = 0; k < keyCount; k++) { if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var key = unpacker.LastReadData.DeserializeAsString(); if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!unpacker.IsArrayHeader) { throw new SerializationException("Invalid NameValueCollection value."); } var itemsCount = UnpackHelpers.GetItemsCount(unpacker); using (var valuesUnpacker = unpacker.ReadSubtree()) { for (var v = 0; v < itemsCount; v++) { if (!valuesUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } collection.Add(key, valuesUnpacker.LastReadData.DeserializeAsString()); } } } }
protected internal sealed override Version UnpackFromCore(Unpacker unpacker) { long num = unpacker.Data.Value.AsInt64(); int[] numArray = new int[4]; for (int i = 0; (i < num) && (i < 4); i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } numArray[i] = unpacker.Data.Value.AsInt32(); } return(new Version(numArray[0], numArray[1], numArray[2], numArray[3])); }
protected override Matrix4x4 UnpackFromCore(Unpacker unpacker) { double[] buffer = new double[16]; buffer[0] = unpacker.LastReadData.AsDouble(); for (int i = 1; i < 16; i++) { unpacker.Read(); buffer[i] = unpacker.LastReadData.AsDouble(); } var matrix = new Matrix4x4(); matrix.Values = buffer; return(matrix); }
protected internal sealed override Version UnpackFromCore(Unpacker unpacker) { long length = unpacker.Data.Value.AsInt64(); int[] array = new int[4]; for (int i = 0; i < length && i < 4; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } array[i] = unpacker.Data.Value.AsInt32(); } return(new Version(array[0], array[1], array[2], array[3])); }
public static void UnpackArrayTo <T>(Unpacker unpacker, MessagePackSerializer <T> serializer, T[] array) { #if DEBUG if (unpacker == null) { throw new ArgumentNullException("unpacker"); } if (array == null) { throw new ArgumentNullException("array"); } if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } Contract.EndContractBlock(); #endif int count = GetItemsCount(unpacker); for (int i = 0; i < count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } T item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = serializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = serializer.UnpackFrom(subtreeUnpacker); } } array[i] = item; } }
public static void UnpackCollectionTo(Unpacker unpacker, IEnumerable collection, Action <object> addition) { #if DEBUG if (unpacker == null) { throw new ArgumentNullException("unpacker"); } if (collection == null) { throw new ArgumentNullException("collection"); } if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } Contract.EndContractBlock(); #endif int count = GetItemsCount(unpacker); for (int i = 0; i < count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } MessagePackObject item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = _messagePackObjectSerializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = _messagePackObjectSerializer.UnpackFrom(subtreeUnpacker); } } addition(item); } }
protected override T UnpackFromCore(Unpacker unpacker) { IMessagePackSerializer serializer; string typeName; // Read type name and packed object if (!(unpacker.ReadString(out typeName) && unpacker.Read())) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } // Find matching serializer if (!_serializers.TryGetValue(typeName, out serializer)) { throw SerializationExceptions.NewTypeCannotDeserialize(typeof(T)); } // Unpack and return return((T)serializer.UnpackFrom(unpacker)); }
/// <summary> /// Tries read from <see cref="Unpacker"/> considering fragmented receival. /// </summary> /// <param name="unpacker">The unpacker.</param> /// <param name="underyingStream">The underying stream.</param> /// <returns><c>true</c> if data read successfully; otherwise, <c>false</c></returns> public static bool TryRead(this Unpacker unpacker, Stream underyingStream) { var position = underyingStream.Position; try { return(unpacker.Read()); } catch (InvalidMessagePackStreamException) { if (underyingStream.Position == underyingStream.Length) { // It was fragmented data, so we may be able to read them on next time. underyingStream.Position = position; return(false); } throw; } }
private void UnpackFromMap(Unpacker unpacker, ref object instance) { while (unpacker.Read()) { var memberName = GetMemberName(unpacker); int index; if (!this._indexMap.TryGetValue(memberName, out index)) { // Drains unused value. if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } // TODO: unknown member handling. continue; } // Fetches value if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (unpacker.LastReadData.IsNil) { switch (this._nilImplications[index]) { case NilImplication.Null: { this._memberSetters[index](ref instance, null); continue; } case NilImplication.MemberDefault: { continue; } case NilImplication.Prohibit: { throw SerializationExceptions.NewNullIsProhibited(this._memberNames[index]); } } } if (unpacker.IsArrayHeader || unpacker.IsMapHeader) { using (var subtreeUnpacker = unpacker.ReadSubtree()) { this.UnpackMemberInMap(subtreeUnpacker, ref instance, index); } } else { this.UnpackMemberInMap(unpacker, ref instance, index); } } }
private void UnpackFromArray(Unpacker unpacker, ref object instance) { int unpacked = 0; int itemsCount = checked((int)unpacker.ItemsCount); for (int i = 0; i < this.MemberSerializers.Length; i++) { if (unpacked == itemsCount) { // It is OK to avoid skip missing member because default NilImplication is MemberDefault so it is harmless. this.HandleNilImplication(ref instance, i); } else { if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (unpacker.LastReadData.IsNil) { this.HandleNilImplication(ref instance, i); } else { if (unpacker.IsArrayHeader || unpacker.IsMapHeader) { using (var subtreeUnpacker = unpacker.ReadSubtree()) { this.UnpackMemberInArray(subtreeUnpacker, ref instance, i); } } else { this.UnpackMemberInArray(unpacker, ref instance, i); } } unpacked++; } } }