protected internal override T UnpackFromCore(Unpacker unpacker) { if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } var buffer = new TItem[UnpackHelpers.GetItemsCount(unpacker)]; using (var subTreeUnpacker = unpacker.ReadSubtree()) { for (int i = 0; i < buffer.Length; i++) { if (!subTreeUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } buffer[i] = this._itemSerializer.UnpackFrom(subTreeUnpacker); } } return(factory(buffer)); }
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()); } } } }
public static T Decode <T>(Unpacker unpacker, Func <Unpacker, Type> typeFinder, Func <Type, Unpacker, T> unpacking) { if (!unpacker.IsArrayHeader || UnpackHelpers.GetItemsCount(unpacker) != 2) { throw SerializationExceptions.NewUnknownTypeEmbedding(); } using (var subTreeUnpacker = unpacker.ReadSubtree()) { if (!subTreeUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } var type = typeFinder(subTreeUnpacker); if (!subTreeUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } return(unpacking(type, subTreeUnpacker)); } }
protected internal override DictionaryEntry UnpackFromCore(Unpacker unpacker) { if (unpacker.IsArrayHeader) { MessagePackObject key; MessagePackObject value; if (!unpacker.ReadObject(out key)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!unpacker.ReadObject(out value)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } return(new DictionaryEntry(key, value)); } else { // Previous DictionaryEntry serializer accidentally pack it as map... MessagePackObject key = default(MessagePackObject); MessagePackObject value = default(MessagePackObject); bool isKeyFound = false; bool isValueFound = false; string propertyName; while ((!isKeyFound || !isValueFound) && unpacker.ReadString(out propertyName)) { switch (propertyName) { case "Key": { if (!unpacker.ReadObject(out key)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } isKeyFound = true; break; } case "Value": { if (!unpacker.ReadObject(out value)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } isValueFound = true; break; } } } if (!isKeyFound) { throw SerializationExceptions.NewMissingProperty("Key"); } if (!isValueFound) { throw SerializationExceptions.NewMissingProperty("Value"); } return(new DictionaryEntry(key, value)); } }
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; } 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); } } return(result); }
protected internal override object UnpackFromCore(Unpacker unpacker) #endif // !UNITY { if (!unpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } if (UnpackHelpers.GetItemsCount(unpacker) != 2) { throw new SerializationException("Multidimensional array must be encoded as 2 element array."); } using (var wholeUnpacker = unpacker.ReadSubtree()) { if (!wholeUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } MessagePackExtendedTypeObject metadata; try { metadata = wholeUnpacker.LastReadData.AsMessagePackExtendedTypeObject(); } catch (InvalidOperationException ex) { throw new SerializationException("Multidimensional array must be encoded as ext type.", ex); } if (metadata.TypeCode != MultidimensionalArray) { throw new SerializationException( String.Format( CultureInfo.CurrentCulture, "Multidimensional array must be encoded as ext type {0}.", MultidimensionalArray ) ); } int[] lengths, lowerBounds; using (var arrayMetadata = new MemoryStream(metadata.Body)) using (var metadataUnpacker = Unpacker.Create(arrayMetadata)) { if (!metadataUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!metadataUnpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } if (UnpackHelpers.GetItemsCount(metadataUnpacker) != 2) { throw new SerializationException("Multidimensional metadata array must be encoded as 2 element array."); } this.ReadArrayMetadata(metadataUnpacker, out lengths, out lowerBounds); } #if SILVERLIGHT // Simulate lowerbounds because Array.Initialize() in Silverlight does not support lowerbounds. for (var i = 0; i < lowerBounds.Length; i++) { lengths[i] += lowerBounds[i]; } #endif // SILVERLIGHT if (!wholeUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!wholeUnpacker.IsArrayHeader) { throw SerializationExceptions.NewIsNotArrayHeader(); } using (var arrayUnpacker = wholeUnpacker.ReadSubtree()) { var result = Array.CreateInstance( #if !UNITY typeof(TItem), #else this._itemType, #endif // !UNITY #if !SILVERLIGHT lengths, lowerBounds #else lengths #endif // !SILVERLIGHT ); var totalLength = UnpackHelpers.GetItemsCount(arrayUnpacker); if (totalLength > 0) { ForEach( result, totalLength, lowerBounds, lengths, indices => { // ReSharper disable AccessToDisposedClosure if (!arrayUnpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } result.SetValue( this._itemSerializer.UnpackFrom(arrayUnpacker), indices ); // ReSharper restore AccessToDisposedClosure } ); } #if !UNITY return(( TArray )( object )result); #else return(result); #endif // !UNITY } } }
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[])); } }
/// <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; } } } }
private void UnpackFromMap(Unpacker unpacker, ref T 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.Data.Value.IsNil) { switch (this._nilImplications[index]) { case NilImplication.Null: { if (this._memberSetters[index] == null) { throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(this._memberNames[index]); } 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); } } }