protected internal override void UnpackToCore(Unpacker unpacker, Queue <TItem> collection) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } TItem item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = this._itemSerializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = this._itemSerializer.UnpackFrom(subtreeUnpacker); } } collection.Enqueue(item); } }
private static void UnpackToCore(Unpacker unpacker, IDictionary collection, int itemsCount) { for (int i = 0; i < itemsCount; i++) { MessagePackObject key; try { key = unpacker.ReadItemData(); } catch (InvalidMessagePackStreamException) { throw SerializationExceptions.NewMissingItem(i); } MessagePackObject value; try { value = unpacker.ReadItemData(); } catch (InvalidMessagePackStreamException) { throw SerializationExceptions.NewMissingItem(i); } collection.Add(key, value); } }
protected internal override T UnpackFromCore(Unpacker unpacker) { if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } var itemsCount = UnpackHelpers.GetItemsCount(unpacker); if (itemsCount != this._itemSerializers.Count) { throw SerializationExceptions.NewTupleCardinarityIsNotMatch(this._itemSerializers.Count, itemsCount); } var unpackedItems = new List <object>(this._itemSerializers.Count); for (var i = 0; i < this._itemSerializers.Count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } unpackedItems.Add(this._itemSerializers[i].UnpackFrom(unpacker)); } return(this.CreateTuple(unpackedItems)); }
internal void UnpackToCore(Unpacker unpacker, object collection, int itemsCount) { for (var i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } object item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = this._itemSerializer.UnpackFrom(unpacker); } else { using (var subtreeUnpacker = unpacker.ReadSubtree()) { item = this._itemSerializer.UnpackFrom(subtreeUnpacker); } } this.AddItem(collection, item); } }
protected override Quaternion UnpackFromCore(Unpacker unpacker) { //IL_008b: Unknown result type (might be due to invalid IL or missing references) if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } int itemsCount = UnpackHelpers.GetItemsCount(unpacker); if (itemsCount != 4) { throw SerializationExceptions.NewIsNotArrayHeader(); } if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } if (!unpacker.ReadSingle(out float result)) { throw SerializationExceptions.NewMissingItem(0); } if (!unpacker.ReadSingle(out float result2)) { throw SerializationExceptions.NewMissingItem(1); } if (!unpacker.ReadSingle(out float result3)) { throw SerializationExceptions.NewMissingItem(2); } if (!unpacker.ReadSingle(out float result4)) { throw SerializationExceptions.NewMissingItem(2); } return(new Quaternion(result, result2, result3, result4)); }
private void UnpackToCore(Unpacker unpacker, IList collection, int count) #endif // !UNITY { for (int i = 0; i < count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } #if !UNITY T item; #else object item; #endif // !UNITY if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = this._itemSerializer.UnpackFrom(unpacker); } else { using (var subtreeUnpacker = unpacker.ReadSubtree()) { item = this._itemSerializer.UnpackFrom(subtreeUnpacker); } } collection[i] = item; } }
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); }
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)); }
protected internal override void UnpackToCore(Unpacker unpacker, Queue collection) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } collection.Enqueue(unpacker.LastReadData); } }
private void UnpackToCore(Unpacker unpacker, TDictionary 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 (var 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 (var subtreeUnpacker = unpacker.ReadSubtree()) { value = this._valueSerializer.UnpackFrom(subtreeUnpacker); } } #if (!UNITY && !XAMIOS) || AOT_CHECK collection.Add(key, value); #else // .constraind call for TDictionary.Add causes AOT error. // So use cast and invoke as normal call (it might cause boxing, but most collection should be reference type). (collection as IDictionary <TKey, TValue>).Add(key, value); #endif // ( !UNITY && !XAMIOS ) || AOT_CHECK } }
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 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])); }
protected void UnpackToCore(Unpacker unpacker, T collection, int itemsCount) { for (int i = 0; i < itemsCount; i++) { MessagePackObject item; try { item = unpacker.ReadItemData(); } catch (InvalidMessagePackStreamException) { throw SerializationExceptions.NewMissingItem(i); } this.AddItem(collection, item); } }
private void UnpackToCore(Unpacker unpacker, object collection, int itemsCount) { for (int i = 0; i < itemsCount; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } 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()) { throw SerializationExceptions.NewMissingItem(i); } 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, 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); } }
public static object UnpackGenericArraySegmentFrom(Unpacker unpacker, Type elementType, IMessagePackSingleObjectSerializer itemSerializer) { Array array = Array.CreateInstance(elementType, unpacker.ItemsCount); for (int i = 0; i < array.Length; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } array.SetValue(itemSerializer.UnpackFrom(unpacker), i); } return (ReflectionExtensions.CreateInstancePreservingExceptionType( typeof(ArraySegment <>).MakeGenericType(elementType), array )); }
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); }
private object UnpackMemberValue(object objectGraph, Unpacker unpacker, int itemsCount, ref int unpacked, int index, int unpackerOffset) { object nullable = null; var setter = index < this._setters.Length ? this._setters[index] : null; if (unpacked < itemsCount) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(unpackerOffset); } if (!unpacker.LastReadData.IsNil) { if (setter != null || this._constructorParameters != null) { nullable = this.UnpackSingleValue(unpacker, index); } else if (this._getters[index] != null) // null getter supposes undeclared member (should be treated as nil) { this.UnpackAndAddCollectionItem(objectGraph, unpacker, index); } } } if (this._constructorParameters != null) { #if DEBUG && !UNITY Contract.Assert(objectGraph is object[], "objectGraph is object[]"); #endif // !UNITY int argumentIndex; if (this._constructorArgumentIndexes.TryGetValue(index, out argumentIndex)) { if (nullable == null) { ReflectionNilImplicationHandler.Instance.OnUnpacked( new ReflectionSerializerNilImplicationHandlerOnUnpackedParameter( this._memberInfos[index].GetMemberValueType(), // ReSharper disable once PossibleNullReferenceException value => (objectGraph as object[])[argumentIndex] = nullable, this._contracts[index].Name, this._memberInfos[index].DeclaringType ), this._contracts[index].NilImplication )(null); } else { (objectGraph as object[])[argumentIndex] = nullable; } } } else if (setter != null) { if (nullable == null) { ReflectionNilImplicationHandler.Instance.OnUnpacked( new ReflectionSerializerNilImplicationHandlerOnUnpackedParameter( this._memberInfos[index].GetMemberValueType(), value => setter(objectGraph, nullable), this._contracts[index].Name, this._memberInfos[index].DeclaringType ), this._contracts[index].NilImplication )(null); } else { setter(objectGraph, nullable); } } unpacked++; return(objectGraph); }
protected SequenceReflectionMessagePackSerializer(Type type, SerializationContext context, CollectionTraits traits) : base(type, (context ?? SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions) { Contract.Assert(type.IsArray || typeof(IEnumerable).IsAssignableFrom(type), type + " is not array nor IEnumerable"); this._traits = traits; this._elementSerializer = context.GetSerializer(traits.ElementType); this._getCount = ReflectionSerializerLogics.CreateGetCount(type, traits); //var packerParameter = Expression.Parameter(typeof(Packer), "packer"); //var objectTreeParameter = Expression.Parameter(typeof(T), "objectTree"); //var elementSerializerParameter = Expression.Parameter(typeof(IMessagePackSerializer), "elementSerializer"); this._packToCore = (Packer packer, object objectTree, IMessagePackSerializer elementSerializer) => { var length = this._getCount(objectTree); packer.PackArrayHeader(length); foreach (var item in (IEnumerable)objectTree) { elementSerializer.PackTo(packer, item); } }; /* * for ( int i = 0; i < count; i++ ) * { * if ( !unpacker.Read() ) * { * throw SerializationExceptions.NewMissingItem( i ); * } * * T item; * if ( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * item = this.ElementSerializer.UnpackFrom( unpacker ); * } * else * { * using ( Unpacker subtreeUnpacker = unpacker.ReadSubtree() ) * { * item = this.ElementSerializer.UnpackFrom( subtreeUnpacker ); * } * } * * instance[ i ] = item; -- OR -- instance.Add( item ); * } */ // FIXME: use UnpackHelper if (type.IsArray) { var arrayUnpackerMethod = _UnpackHelpers.UnpackArrayTo_1.MakeGenericMethod(traits.ElementType); this._unpackToCore = (Unpacker unpacker, object instance, IMessagePackSerializer elementSerializer) => { arrayUnpackerMethod.Invoke(null, new object[] { unpacker, elementSerializer, instance }); }; } else { this._unpackToCore = (Unpacker unpacker, object instance, IMessagePackSerializer elementSerializer) => { var count = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < count; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } object item; if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { item = elementSerializer.UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { item = elementSerializer.UnpackFrom(subtreeUnpacker); } } traits.AddMethod.Invoke(instance, new object[] { item }); } }; } }
protected internal override T UnpackFromCore(Unpacker unpacker) { object result = this._constructorParameters == null ? ReflectionExtensions.CreateInstancePreservingExceptionType(typeof(T)) : this._constructorParameters.Select(p => p.GetHasDefaultValue() ? p.DefaultValue : p.ParameterType.GetIsValueType() ? ReflectionExtensions.CreateInstancePreservingExceptionType(p.ParameterType) : null ).ToArray(); var unpacked = 0; var asUnpackable = result as IUnpackable; if (asUnpackable != null) { asUnpackable.UnpackFromMessage(unpacker); return(( T )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, "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; } int index; if (!this._memberIndexes.TryGetValue(name, out index)) { // key does not exist in the object, skip the associated value if (unpacker.Skip() == null) { throw SerializationExceptions.NewMissingItem(i); } continue; } result = this.UnpackMemberValue(result, unpacker, itemsCount, ref unpacked, index, i); } } if (this._constructorParameters == null) { return(( T )result); } else { return(ReflectionExtensions.CreateInstancePreservingExceptionType <T>(typeof(T), result as object[])); } }
private T UnpackMemberValue(T objectGraph, Unpacker unpacker, int itemsCount, ref int unpacked, int index, int unpackerOffset) { object nullable = null; var setter = this._setters[index]; if (unpacked < itemsCount) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(unpackerOffset); } if (!unpacker.LastReadData.IsNil) { if (setter != null) { if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader) { nullable = this._serializers[index].UnpackFrom(unpacker); } else { using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { nullable = this._serializers[index].UnpackFrom(subtreeUnpacker); } } } else if (this._getters[index] != null) // null getter supposes undeclared member (should be treated as nil) { var collection = this._getters[index](objectGraph); if (collection == null) { throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(this._contracts[index].Name); } using (Unpacker subtreeUnpacker = unpacker.ReadSubtree()) { this._serializers[index].UnpackTo(subtreeUnpacker, collection); } } } } if (setter != null) { if (nullable == null) { ReflectionNilImplicationHandler.Instance.OnUnpacked( new ReflectionSerializerNilImplicationHandlerOnUnpackedParameter( this._memberInfos[index].GetMemberValueType(), value => SetMemverValue(objectGraph, setter, value), this._contracts[index].Name, this._memberInfos[index].DeclaringType ), this._contracts[index].NilImplication )(null); } else { objectGraph = SetMemverValue(objectGraph, setter, nullable); } } unpacked++; return(objectGraph); }