internal static byte ReadHeader(Type t, BinaryReader reader, NilImplication nilImplication, out object result) { result = null; try { byte v = reader.ReadByte(); if (v == MsgPackConstants.Formats.NIL) { if (nilImplication == NilImplication.MemberDefault) { if (t.IsValueType) { result = Activator.CreateInstance(t); } } else if (nilImplication == NilImplication.Prohibit) { throw new ApplicationException(nullProhibitedExceptionMessage); } } return(v); } catch { return(0); } }
public TAction OnPacking(TPackingParameter parameter, NilImplication nilImplication) { switch (nilImplication) { case NilImplication.Prohibit: { TCondition condition = null; if (parameter.ItemType == typeof(MessagePackObject)) { condition = this.OnPackingMessagePackObject(parameter); } else if (!parameter.ItemType.GetIsValueType()) { condition = this.OnPackingReferenceTypeObject(parameter); } else if (Nullable.GetUnderlyingType(parameter.ItemType) != null) { condition = this.OnPackingNullableValueTypeObject(parameter); } if (condition != null) { return(this.OnPackingCore(parameter, condition)); } break; } } return(null); }
internal static object ReadMsgPackString(BinaryReader reader, NilImplication nilImplication, byte header = 0) { object result = null; byte v = header == 0 ? ReadHeader(typeof(string), reader, nilImplication, out result) : header; if (v != MsgPackConstants.Formats.NIL) { int length = 0; if (v >= MsgPackConstants.FixedString.MIN && v <= MsgPackConstants.FixedString.MAX) { length = v - MsgPackConstants.FixedString.MIN; } else if (v == MsgPackConstants.Formats.STR_8) { length = reader.ReadByte(); } else if (v == MsgPackConstants.Formats.STR_16) { length = (reader.ReadByte() << 8) + reader.ReadByte(); } else if (v == MsgPackConstants.Formats.STR_32) { length = (reader.ReadByte() << 24) + (reader.ReadByte() << 16) + (reader.ReadByte() << 8) + reader.ReadByte(); } byte[] stringBuffer = reader.ReadBytes(length); result = UTF8Encoding.UTF8.GetString(stringBuffer); } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerGenerationContext{TConstruct}"/> class. /// </summary> /// <param name="context">The serialization context.</param> protected SerializerGenerationContext(SerializationContext context) { this.SerializationContext = context; this.CollectionItemNilImplication = NilImplication.Null; this.DictionaryKeyNilImplication = NilImplication.Prohibit; this.TupleItemNilImplication = NilImplication.Null; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> public DataMemberContract(MemberInfo member) { Contract.Requires(member != null); this._name = member.Name; this._nilImplication = Serialization.NilImplication.MemberDefault; this._id = UnspecifiedId; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> public DataMemberContract(MemberInfo member) { Contract.Assert(member != null); this._name = member.Name; this._nilImplication = NilImplication.MemberDefault; this._id = UnspecifiedId; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct from <see cref="DataMemberAttribute"/>. /// </summary> /// <param name="member">The target member.</param> /// <param name="attribute">The data contract member attribute. This value can be <c>null</c>.</param> public DataMemberContract(MemberInfo member, DataMemberAttribute attribute) { Contract.Requires(member != null); Contract.Requires(attribute != null); this._name = String.IsNullOrEmpty(attribute.Name) ? member.Name : attribute.Name; this._nilImplication = Serialization.NilImplication.MemberDefault; this._id = attribute.Order; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> public DataMemberContract(MemberInfo member) { #if !UNITY Contract.Requires(member != null); #endif // !UNITY this._name = member.Name; this._nilImplication = NilImplication.MemberDefault; this._id = UnspecifiedId; }
internal static object ReadMsgPackBoolean(BinaryReader reader, NilImplication nilImplication) { object result; byte v = ReadHeader(typeof(bool), reader, nilImplication, out result); if (v != MsgPackConstants.Formats.NIL) { result = v == MsgPackConstants.Bool.TRUE; } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> /// <param name="name">The name of member.</param> /// <param name="nilImplication">The implication of the nil value for the member.</param> /// <param name="id">The ID of the member. This value cannot be negative and must be unique in the type.</param> public DataMemberContract(MemberInfo member, string name, NilImplication nilImplication, int?id) { Contract.Assert(member != null); if (id < 0) { throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "The member ID cannot be negative. The member is '{0}' in the '{1}' type.", member.Name, member.DeclaringType)); } this._name = String.IsNullOrEmpty(name) ? member.Name : name; this._nilImplication = nilImplication; this._id = id ?? UnspecifiedId; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct from <see cref="MessagePackMemberAttribute"/>. /// </summary> /// <param name="member">The target member.</param> /// <param name="attribute">The MessagePack member attribute. This value can be <c>null</c>.</param> public DataMemberContract(MemberInfo member, MessagePackMemberAttribute attribute) { Contract.Requires(member != null); Contract.Requires(attribute != null); if (attribute.Id < 0) { throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "The member ID cannot be negative. The member is '{0}' in the '{1}' type.", member.Name, member.DeclaringType)); } this._name = member.Name; this._nilImplication = attribute.NilImplication; this._id = attribute.Id; }
internal static object ReadMsgPackULong(BinaryReader reader, NilImplication nilImplication, byte header = 0) { object result = null; byte v = header == 0 ? ReadHeader(typeof(ulong), reader, nilImplication, out result) : header; if (v != MsgPackConstants.Formats.NIL) { if (v != MsgPackConstants.Formats.UINT_64) { throw new ApplicationException("Serialized data doesn't match type being deserialized to"); } result = reader.ReadUInt64(); } return(result); }
internal static object DeserializeObject(object o, BinaryReader reader, NilImplication nilImplication = NilImplication.MemberDefault) { var list = o as IList; if (list != null) { return(MsgPackIO.DeserializeCollection(list, reader) ? null : o); } var dictionary = o as IDictionary; if (dictionary != null) { return(MsgPackIO.DeserializeCollection(dictionary, reader) ? null : o); } return(GetSerializer(o.GetType()).Deserialize(o, reader)); }
public TAction OnUnpacked(TUnpackedParameter parameter, NilImplication nilImplication) { /* * #if MEMBER_DEFAULT * // nop * #elif PROHIBIT * throw SerializationExceptiuons.NullIsProhibited(...); * #elif VALUE_TYPE * throw SerializationExceptiuons.ValueTypeCannotbeNull(...); * #else * SET_VALUE(item); * #endif */ // is nilable natually? var isNativelyNullable = parameter.ItemType == typeof(MessagePackObject) || !parameter.ItemType.GetIsValueType() || Nullable.GetUnderlyingType(parameter.ItemType) != null; // Nil Implication switch (nilImplication) { case NilImplication.MemberDefault: { // Nothing to return(OnNopOnUnpacked(parameter)); } case NilImplication.Prohibit: { return(OnThrowNullIsProhibitedExceptionOnUnpacked(parameter)); } case NilImplication.Null: { return(!isNativelyNullable?this.OnThrowValueTypeCannotBeNull3OnUnpacked(parameter) : parameter.Store); } default: { throw new SerializationException( String.Format(CultureInfo.CurrentCulture, "Unknown NilImplication value '{0}'.", ( int )nilImplication) ); } } }
internal static object DeserializeObjectType(Type type, BinaryReader reader, NilImplication nilImplication = NilImplication.MemberDefault) { if (type.IsPrimitive || type == typeof(string) || IsSerializableGenericCollection(type)) { return(MsgPackIO.DeserializeValue(type, reader, nilImplication)); } ConstructorInfo constructorInfo = type.GetConstructor(Type.EmptyTypes); if (constructorInfo == null) { throw new ApplicationException("Can't deserialize Type [" + type + "] in MsgPackSerializer because it has no default constructor"); } object result = constructorInfo.Invoke(SerializableProperty.EmptyObjArgs); return(GetSerializer(type).Deserialize(result, reader)); }
internal SerializableProperty(PropertyInfo propInfo, int sequence = 0, NilImplication?nilImplication = null) { PropInfo = propInfo; Name = propInfo.Name; _nilImplication = nilImplication ?? NilImplication.MemberDefault; Sequence = sequence; ValueType = propInfo.PropertyType; Type underlyingType = Nullable.GetUnderlyingType(propInfo.PropertyType); if (underlyingType != null) { ValueType = underlyingType; if (nilImplication.HasValue == false) { _nilImplication = NilImplication.Null; } } }
internal static object ReadMsgPackDouble(BinaryReader reader, NilImplication nilImplication, byte header = 0) { object result = null; byte v = header == 0 ? ReadHeader(typeof(double), reader, nilImplication, out result) : header; if (v != MsgPackConstants.Formats.NIL) { if (v != MsgPackConstants.Formats.FLOAT_64) { throw new ApplicationException("Serialized data doesn't match type being deserialized to"); } byte[] data = reader.ReadBytes(8); if (BitConverter.IsLittleEndian) { Array.Reverse(data); } result = BitConverter.ToDouble(data, 0); } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> public DataMemberContract( MemberInfo member ) { Contract.Requires( member != null ); this._name = member.Name; this._nilImplication = Serialization.NilImplication.MemberDefault; this._id = UnspecifiedId; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct from <see cref="MessagePackMemberAttribute"/>. /// </summary> /// <param name="member">The target member.</param> /// <param name="attribute">The MessagePack member attribute.</param> public DataMemberContract( MemberInfo member, MessagePackMemberAttribute attribute ) { #if !UNITY Contract.Requires( member != null ); Contract.Requires( attribute != null ); #endif // !UNITY if ( attribute.Id < 0 ) { throw new SerializationException( String.Format( CultureInfo.CurrentCulture, "The member ID cannot be negative. The member is '{0}' in the '{1}' type.", member.Name, member.DeclaringType ) ); } this._name = String.IsNullOrEmpty( attribute.Name ) ? member.Name : attribute.Name; this._nilImplication = attribute.NilImplication; this._id = attribute.Id; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> /// <param name="name">The name of member.</param> /// <param name="nilImplication">The implication of the nil value for the member.</param> /// <param name="id">The ID of the member. This value cannot be negative and must be unique in the type.</param> public DataMemberContract( MemberInfo member, string name, NilImplication nilImplication, int? id ) { #if !UNITY Contract.Requires( member != null ); #endif // !UNITY if ( id < 0 ) { throw new SerializationException( String.Format( CultureInfo.CurrentCulture, "The member ID cannot be negative. The member is '{0}' in the '{1}' type.", member.Name, member.DeclaringType ) ); } this._name = String.IsNullOrEmpty( name ) ? member.Name : name; this._nilImplication = nilImplication; this._id = id ?? UnspecifiedId; }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> public DataMemberContract( MemberInfo member ) { #if !UNITY Contract.Requires( member != null ); #endif // !UNITY this._name = member.Name; this._nilImplication = NilImplication.MemberDefault; this._id = UnspecifiedId; }
public DataMemberContract() { this._name = null; this._nilImplication = NilImplication.MemberDefault; this._id = UnspecifiedId; }
/// <summary> /// Emits the serializing value instructions. /// </summary> /// <param name="emitter">The emitter.</param> /// <param name="il">The il generator.</param> /// <param name="packerArgumentIndex">Index of the packer argument.</param> /// <param name="valueType">Type of the current member value.</param> /// <param name="memberName">Name of the current member.</param> /// <param name="nilImplication">The nil implication of the current member.</param> /// <param name="loadValueEmitter">The delegate which emits case specific value loading instructions.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitSerializeValue(SerializerEmitter emitter, TracingILGenerator il, int packerArgumentIndex, Type valueType, string memberName, NilImplication nilImplication, Action <TracingILGenerator> loadValueEmitter, LocalVariableHolder localHolder) { Contract.Requires(emitter != null); Contract.Requires(il != null); Contract.Requires(packerArgumentIndex >= 0); Contract.Requires(valueType != null); Contract.Requires(loadValueEmitter != null); /* * var serializingValue = LOAD_VALUE; * NULL_PROHIBIT_HANDLING * GET_SERIALIZER.PackTo( packer, serializingValue ); */ var value = localHolder.GetSerializingValue(valueType); loadValueEmitter(il); il.EmitAnyStloc(value); if (memberName != null && nilImplication == NilImplication.Prohibit) { /* * if( serializingValue == null )( * { * throw SerializationExceptions.NewNullIsProhibited(); * } */ if (!valueType.IsValueType) { il.EmitAnyLdloc(value); var endIf = il.DefineLabel("END_IF"); il.EmitBrtrue_S(endIf); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(endIf); } else if (Nullable.GetUnderlyingType(valueType) != null) { il.EmitAnyLdloca(value); il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(Nullable.GetUnderlyingType(valueType)).GetProperty("HasValue")); var endIf = il.DefineLabel("END_IF"); il.EmitBrtrue_S(endIf); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(endIf); } } var serializerGetter = emitter.RegisterSerializer(valueType); serializerGetter(il, 0); il.EmitAnyLdarg(packerArgumentIndex); il.EmitAnyLdloc(value); il.EmitAnyCall(typeof(MessagePackSerializer <>).MakeGenericType(valueType).GetMethod("PackTo")); }
internal static object ReadMsgPackInt(BinaryReader reader, NilImplication nilImplication, byte header = 0) { object result = null; byte v = header == 0 ? ReadHeader(typeof(long), reader, nilImplication, out result) : header; if (v != MsgPackConstants.Formats.NIL) { if (v <= MsgPackConstants.FixedInteger.POSITIVE_MAX) { result = v; } else if (v >= MsgPackConstants.FixedInteger.NEGATIVE_MIN) { result = -(v - MsgPackConstants.FixedInteger.NEGATIVE_MIN); } else if (v == MsgPackConstants.Formats.UINT_8) { result = reader.ReadByte(); } else if (v == MsgPackConstants.Formats.UINT_16) { result = (reader.ReadByte() << 8) + reader.ReadByte(); } else if (v == MsgPackConstants.Formats.UINT_32) { result = (uint)(reader.ReadByte() << 24) + (uint)(reader.ReadByte() << 16) + (uint)(reader.ReadByte() << 8) + (uint)reader.ReadByte(); } else if (v == MsgPackConstants.Formats.UINT_64) { result = (ulong)(reader.ReadByte() << 56) + (ulong)(reader.ReadByte() << 48) + (ulong)(reader.ReadByte() << 40) + (ulong)(reader.ReadByte() << 32) + (ulong)(reader.ReadByte() << 24) + (ulong)(reader.ReadByte() << 16) + (ulong)(reader.ReadByte() << 8) + (ulong)reader.ReadByte(); } else if (v == MsgPackConstants.Formats.INT_8) { result = reader.ReadSByte(); } else if (v == MsgPackConstants.Formats.INT_16) { byte[] data = reader.ReadBytes(2); if (BitConverter.IsLittleEndian) { Array.Reverse(data); } result = BitConverter.ToInt16(data, 0); } else if (v == MsgPackConstants.Formats.INT_32) { byte[] data = reader.ReadBytes(4); if (BitConverter.IsLittleEndian) { Array.Reverse(data); } result = BitConverter.ToInt32(data, 0); } else if (v == MsgPackConstants.Formats.INT_64) { byte[] data = reader.ReadBytes(8); if (BitConverter.IsLittleEndian) { Array.Reverse(data); } result = BitConverter.ToInt64(data, 0); } else { throw new ApplicationException("Serialized data doesn't match type being deserialized to"); } } return(result); }
/// <summary> /// Emits the nil implication. /// </summary> /// <param name="il">The il generator.</param> /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param> /// <param name="memberName">Name of the deserializing member.</param> /// <param name="nilImplication">The nil implication.</param> /// <param name="endOfDeserialization">The label to the end of deserialization.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitNilImplication( TracingILGenerator il, int unpackerArgumentIndex, string memberName, NilImplication nilImplication, Label endOfDeserialization, LocalVariableHolder localHolder ) { switch (nilImplication) { case NilImplication.MemberDefault: { // TODO: This should be empty for extra items. /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.Data); var data = localHolder.UnpackedData; il.EmitAnyStloc(data); il.EmitAnyLdloca(data); il.EmitGetProperty(Metadata._Nullable <MessagePackObject> .Value); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(dataValue); il.EmitAnyLdloca(dataValue); il.EmitGetProperty(Metadata._MessagePackObject.IsNil); il.EmitBrtrue(endOfDeserialization); break; } case NilImplication.Prohibit: { /* * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } */ il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.Data); var data = localHolder.UnpackedData; il.EmitAnyStloc(data); il.EmitAnyLdloca(data); il.EmitGetProperty(Metadata._Nullable <MessagePackObject> .Value); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(dataValue); il.EmitAnyLdloca(dataValue); il.EmitGetProperty(Metadata._MessagePackObject.IsNil); var endIf0 = il.DefineLabel("END_IF0"); il.EmitBrfalse_S(endIf0); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(endIf0); break; } } }
internal static object DeserializeValue(Type type, BinaryReader reader, NilImplication nilImplication) { object result = null; bool isRichType = false; if (type == typeof(string)) { result = ReadMsgPackString(reader, nilImplication); } else if (type == typeof(int) || type == typeof(uint) || type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort) || type == typeof(long) || type == typeof(ulong) || type == typeof(int?) || type == typeof(uint?) || type == typeof(byte?) || type == typeof(sbyte?) || type == typeof(short?) || type == typeof(ushort?) || type == typeof(long?) || type == typeof(ulong?)) { result = ReadMsgPackInt(reader, nilImplication); } else if (type == typeof(char)) { result = ReadMsgPackInt(reader, nilImplication); } else if (type == typeof(float)) { result = ReadMsgPackFloat(reader, nilImplication); } else if (type == typeof(double)) { result = ReadMsgPackDouble(reader, nilImplication); } else if (type == typeof(Boolean) || type == typeof(bool)) { result = ReadMsgPackBoolean(reader, nilImplication); } else if (type == typeof(DateTime)) { object boxedVal = ReadMsgPackInt(reader, nilImplication); if (boxedVal == null) { result = null; } else { long unixEpochTicks = (long)boxedVal; result = ToDateTime(unixEpochTicks); } } else if (type == typeof(TimeSpan)) { object boxedVal = ReadMsgPackInt(reader, nilImplication); if (boxedVal == null) { result = null; } else { int unixEpochTicks = (int)boxedVal; result = ToTimeSpan(unixEpochTicks); } } else if (type.IsEnum) { object boxedVal = ReadMsgPackString(reader, nilImplication); if (boxedVal == null) { result = null; } else { string enumVal = (string)boxedVal; if (enumVal == "") { result = null; } else { result = Enum.Parse(type, enumVal); } } } else if (type.IsArray) { int numElements = MsgPackIO.ReadNumArrayElements(reader); if (numElements == -1) { result = null; } else { result = Activator.CreateInstance(type, new object[] { numElements }); MsgPackIO.DeserializeArray((Array)result, numElements, reader); } } else if (type == typeof(System.Object)) { byte header = reader.ReadByte(); if (header == MsgPackConstants.Formats.NIL) { result = null; } else if (header == MsgPackConstants.Bool.TRUE) { result = true; } else if (header == MsgPackConstants.Bool.FALSE) { result = false; } else if (header == MsgPackConstants.Formats.FLOAT_64) { result = ReadMsgPackDouble(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.FLOAT_32) { result = ReadMsgPackFloat(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.INTEGER_16) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.INTEGER_32) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.INTEGER_64) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.INTEGER_8) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.STRING_8) { result = ReadMsgPackString(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.STRING_16) { result = ReadMsgPackString(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.STRING_32) { result = ReadMsgPackString(reader, nilImplication, header); } else if (header >= MsgPackConstants.FixedString.MIN && header <= MsgPackConstants.FixedString.MAX) { result = ReadMsgPackString(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_8) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_16) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_32) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_64) { result = ReadMsgPackInt(reader, nilImplication, header); } else if (header >= MsgPackConstants.FixedInteger.POSITIVE_MIN && header <= MsgPackConstants.FixedInteger.POSITIVE_MAX) { if (header == 0) { result = 0; } else { result = ReadMsgPackInt(reader, nilImplication, header); } } else if (header >= MsgPackConstants.FixedInteger.NEGATIVE_MIN && header <= MsgPackConstants.FixedInteger.NEGATIVE_MAX) { result = ReadMsgPackInt(reader, nilImplication, header); } else if ((header >= MsgPackConstants.FixedMap.MIN && header <= MsgPackConstants.FixedMap.MAX) || header == MsgPackConstants.Formats.MAP_16 || header == MsgPackConstants.Formats.MAP_32) { result = new Dictionary <string, object>(); MsgPackIO.DeserializeCollection((Dictionary <string, object>)result, reader, header); } else { isRichType = true; } } else { isRichType = true; } if (isRichType) { ConstructorInfo constructorInfo = type.GetConstructor(Type.EmptyTypes); if (constructorInfo == null) { throw new ApplicationException("Can't deserialize Type [" + type + "] because it has no default constructor"); } result = constructorInfo.Invoke(SerializableProperty.EmptyObjArgs); result = MsgPackSerializer.DeserializeObject(result, reader, nilImplication); } return(result); }
/// <summary> /// Emits the deserialize collection value. /// </summary> /// <param name="emitter">The emitter.</param> /// <param name="il">The il generator.</param> /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param> /// <param name="target">The target collection variable.</param> /// <param name="member">The deserializing member metadata which holds the collection.</param> /// <param name="memberType">Type of the deserializing member.</param> /// <param name="nilImplication">The nil implication.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitDeserializeCollectionValue(SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder target, MemberInfo member, Type memberType, NilImplication nilImplication, LocalVariableHolder localHolder) { Contract.Requires(emitter != null); Contract.Requires(il != null); Contract.Requires(unpackerArgumentIndex >= 0); Contract.Requires(target != null); Contract.Requires(member != null); Contract.Requires(memberType != null); Contract.Requires(localHolder != null); var endOfDeserialization = il.DefineLabel("END_OF_DESERIALIZATION"); EmitGeneralRead(il, unpackerArgumentIndex); switch (nilImplication) { case NilImplication.MemberDefault: { /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.Data); var data = localHolder.UnpackedData; il.EmitAnyStloc(data); il.EmitAnyLdloca(data); il.EmitGetProperty(Metadata._Nullable <MessagePackObject> .Value); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(dataValue); il.EmitAnyLdloca(dataValue); il.EmitGetProperty(Metadata._MessagePackObject.IsNil); il.EmitBrtrue(endOfDeserialization); break; } case NilImplication.Null: case NilImplication.Prohibit: { /* * // for Prohibit * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } * * // for Null, and * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewReadOnlyMemberItemsMustNotBeNullMethod( "..." ); * } */ il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.Data); var data = localHolder.UnpackedData; il.EmitAnyStloc(data); il.EmitAnyLdloca(data); il.EmitGetProperty(Metadata._Nullable <MessagePackObject> .Value); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(dataValue); il.EmitAnyLdloca(dataValue); il.EmitGetProperty(Metadata._MessagePackObject.IsNil); var endIf0 = il.DefineLabel("END_IF0"); il.EmitBrfalse_S(endIf0); il.EmitLdstr(member.Name); if (nilImplication == NilImplication.Prohibit) { il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); } else { // Because result member is readonly collection, so the member will not be null if packed value was nil. il.EmitAnyCall(SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNullMethod); } il.EmitThrow(); il.MarkLabel(endIf0); break; } } /* * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * throw new SerializatonException( "Cannot deserialize..." ); * } * * using( var subtreeUnpacker = unpacker.ReadSubtree() ) * { * GET_SERIALIZER.UnpackTo( unpacker, result ) ) * } * * END_OF_DESERIALIZATION: */ var endIf = il.DefineLabel("THEN"); var serializerGetter = emitter.RegisterSerializer(memberType); il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.IsArrayHeader); il.EmitBrtrue_S(endIf); il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.IsMapHeader); il.EmitBrtrue_S(endIf); // else il.EmitLdstr(member.Name); il.EmitAnyCall(SerializationExceptions.NewStreamDoesNotContainCollectionForMemberMethod); il.EmitThrow(); // then var subtreeUnpacker = localHolder.SubtreeUnpacker; il.MarkLabel(endIf); EmitUnpackerBeginReadSubtree(il, unpackerArgumentIndex, subtreeUnpacker); serializerGetter(il, 0); il.EmitAnyLdloc(subtreeUnpacker); il.EmitAnyLdloc(target); Emittion.EmitLoadValue(il, member); il.EmitAnyCall(typeof(MessagePackSerializer <>).MakeGenericType(memberType).GetMethod("UnpackTo", new[] { typeof(Unpacker), memberType })); EmitUnpackerEndReadSubtree(il, subtreeUnpacker); il.MarkLabel(endOfDeserialization); }
public static void EmitDeserializeCollectionValue(SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder target, MemberInfo member, Type memberType, NilImplication nilImplication, LocalVariableHolder localHolder) { LocalBuilder unpackedData; LocalBuilder unpackedDataValue; Label label2; Label label3; Contract.Requires(emitter != null); Contract.Requires(il != null); Contract.Requires(unpackerArgumentIndex >= 0); Contract.Requires(target != null); Contract.Requires(member != null); Contract.Requires(memberType != null); Contract.Requires(localHolder != null); Label label = il.DefineLabel("END_OF_DESERIALIZATION"); EmitGeneralRead(il, unpackerArgumentIndex); switch (nilImplication) { case NilImplication.MemberDefault: il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.Data); unpackedData = localHolder.UnpackedData; il.EmitAnyStloc(unpackedData); il.EmitAnyLdloca(unpackedData); il.EmitGetProperty(_Nullable <MessagePackObject> .Value); unpackedDataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(unpackedDataValue); il.EmitAnyLdloca(unpackedDataValue); il.EmitGetProperty(_MessagePackObject.IsNil); il.EmitBrtrue(label); goto Label_01B5; case NilImplication.Null: case NilImplication.Prohibit: il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.Data); unpackedData = localHolder.UnpackedData; il.EmitAnyStloc(unpackedData); il.EmitAnyLdloca(unpackedData); il.EmitGetProperty(_Nullable <MessagePackObject> .Value); unpackedDataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(unpackedDataValue); il.EmitAnyLdloca(unpackedDataValue); il.EmitGetProperty(_MessagePackObject.IsNil); label2 = il.DefineLabel("END_IF0"); il.EmitBrfalse_S(label2); il.EmitLdstr(member.Name); if (nilImplication != NilImplication.Prohibit) { il.EmitAnyCall(SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNullMethod); break; } il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); break; default: goto Label_01B5; } il.EmitThrow(); il.MarkLabel(label2); Label_01B5: label3 = il.DefineLabel("THEN"); Action <TracingILGenerator, int> action = emitter.RegisterSerializer(memberType); il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.IsArrayHeader); il.EmitBrtrue_S(label3); il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.IsMapHeader); il.EmitBrtrue_S(label3); il.EmitLdstr(member.Name); il.EmitAnyCall(SerializationExceptions.NewStreamDoesNotContainCollectionForMemberMethod); il.EmitThrow(); LocalBuilder subtreeUnpacker = localHolder.SubtreeUnpacker; il.MarkLabel(label3); EmitUnpackerBeginReadSubtree(il, unpackerArgumentIndex, subtreeUnpacker); action(il, 0); il.EmitAnyLdloc(subtreeUnpacker); il.EmitAnyLdloc(target); EmitLoadValue(il, member); il.EmitAnyCall(typeof(MessagePackSerializer <>).MakeGenericType(new Type[] { memberType }).GetMethod("UnpackTo", new Type[] { typeof(Unpacker), memberType })); EmitUnpackerEndReadSubtree(il, subtreeUnpacker); il.MarkLabel(label); }
public static void EmitDeserializeValue( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder value, NilImplication nilImplication, Action<TracingILGenerator, int> customUnpackerReading ) { Contract.Requires( emitter != null ); Contract.Requires( il != null ); Contract.Requires( unpackerArgumentIndex >= 0 ); Contract.Requires( value != null ); if ( customUnpackerReading != null ) { customUnpackerReading( il, unpackerArgumentIndex ); } var endOfDeserialization = il.DefineLabel( "END_OF_DESERIALIZATION" ); /* * * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * valueN = GET_SERIALIZER.UnpackFrom( unpacker ); * } * else * { * using( var subtreeUnpacker = unpacker.ReadSubtree ) * { * valueN = GET_SERIALIZER.UnpackFrom( unpacker ); * } * } * * isValueNUnpacked = true; * END_OF_DESERIALIZATION: */ var then = il.DefineLabel( "THEN" ); var endIf = il.DefineLabel( "END_IF" ); var serializerGetter = emitter.RegisterSerializer( value.LocalType ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); il.EmitBrtrue_S( then ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsMapHeader ); il.EmitBrtrue_S( then ); // else serializerGetter( il, 0 ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( value.LocalType ).GetMethod( "UnpackFrom" ) ); il.EmitAnyStloc( value ); il.EmitBr_S( endIf ); // then var subtreeUnpacker = il.DeclareLocal( typeof( Unpacker ), "subtreeUnpacker" ); il.MarkLabel( then ); EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker ); serializerGetter( il, 0 ); il.EmitAnyLdloc( subtreeUnpacker ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( value.LocalType ).GetMethod( "UnpackFrom" ) ); il.EmitAnyStloc( value ); EmitUnpackerEndReadSubtree( il, subtreeUnpacker ); il.MarkLabel( endIf ); il.MarkLabel( endOfDeserialization ); }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct from <see cref="DataMemberAttribute"/>. /// </summary> /// <param name="member">The target member.</param> /// <param name="attribute">The data contract member attribute. This value can be <c>null</c>.</param> public DataMemberContract( MemberInfo member, DataMemberAttribute attribute ) { Contract.Requires( member != null ); Contract.Requires( attribute != null ); this._name = String.IsNullOrEmpty( attribute.Name ) ? member.Name : attribute.Name; this._nilImplication = Serialization.NilImplication.MemberDefault; this._id = attribute.Order; }
/// <summary> /// Emits the deserialize collection value. /// </summary> /// <param name="emitter">The emitter.</param> /// <param name="il">The il generator.</param> /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param> /// <param name="target">The target collection variable.</param> /// <param name="member">The deserializing member metadata which holds the collection.</param> /// <param name="memberType">Type of the deserializing member.</param> /// <param name="nilImplication">The nil implication.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitDeserializeCollectionValue( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder target, MemberInfo member, Type memberType, NilImplication nilImplication, LocalVariableHolder localHolder ) { Contract.Requires( emitter != null ); Contract.Requires( il != null ); Contract.Requires( unpackerArgumentIndex >= 0 ); Contract.Requires( target != null ); Contract.Requires( member != null ); Contract.Requires( memberType != null ); Contract.Requires( localHolder != null ); var endOfDeserialization = il.DefineLabel( "END_OF_DESERIALIZATION" ); EmitGeneralRead( il, unpackerArgumentIndex ); switch ( nilImplication ) { case NilImplication.MemberDefault: { /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); il.EmitBrtrue( endOfDeserialization ); break; } case NilImplication.Null: case NilImplication.Prohibit: { /* * // for Prohibit * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } * * // for Null, and * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewReadOnlyMemberItemsMustNotBeNullMethod( "..." ); * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); var endIf0 = il.DefineLabel( "END_IF0" ); il.EmitBrfalse_S( endIf0 ); il.EmitLdstr( member.Name ); if ( nilImplication == NilImplication.Prohibit ) { il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); } else { // Because result member is readonly collection, so the member will not be null if packed value was nil. il.EmitAnyCall( SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNullMethod ); } il.EmitThrow(); il.MarkLabel( endIf0 ); break; } } /* * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * throw new SerializatonException( "Cannot deserialize..." ); * } * * using( var subtreeUnpacker = unpacker.ReadSubtree() ) * { * GET_SERIALIZER.UnpackTo( unpacker, result ) ) * } * * END_OF_DESERIALIZATION: */ var endIf = il.DefineLabel( "THEN" ); var serializerGetter = emitter.RegisterSerializer( memberType ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); il.EmitBrtrue_S( endIf ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsMapHeader ); il.EmitBrtrue_S( endIf ); // else il.EmitLdstr( member.Name ); il.EmitAnyCall( SerializationExceptions.NewStreamDoesNotContainCollectionForMemberMethod ); il.EmitThrow(); // then var subtreeUnpacker = localHolder.SubtreeUnpacker; il.MarkLabel( endIf ); EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker ); serializerGetter( il, 0 ); il.EmitAnyLdloc( subtreeUnpacker ); il.EmitAnyLdloc( target ); Emittion.EmitLoadValue( il, member ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( memberType ).GetMethod( "UnpackTo", new[] { typeof( Unpacker ), memberType } ) ); EmitUnpackerEndReadSubtree( il, subtreeUnpacker ); il.MarkLabel( endOfDeserialization ); }
/// <summary> /// Emits the serializing value instructions. /// </summary> /// <param name="emitter">The emitter.</param> /// <param name="il">The il generator.</param> /// <param name="packerArgumentIndex">Index of the packer argument.</param> /// <param name="valueType">Type of the current member value.</param> /// <param name="memberName">Name of the current member.</param> /// <param name="nilImplication">The nil implication of the current member.</param> /// <param name="loadValueEmitter">The delegate which emits case specific value loading instructions.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitSerializeValue( SerializerEmitter emitter, TracingILGenerator il, int packerArgumentIndex, Type valueType, string memberName, NilImplication nilImplication, Action<TracingILGenerator> loadValueEmitter, LocalVariableHolder localHolder ) { Contract.Requires( emitter != null ); Contract.Requires( il != null ); Contract.Requires( packerArgumentIndex >= 0 ); Contract.Requires( valueType != null ); Contract.Requires( loadValueEmitter != null ); /* * var serializingValue = LOAD_VALUE; * NULL_PROHIBIT_HANDLING * GET_SERIALIZER.PackTo( packer, serializingValue ); */ var value = localHolder.GetSerializingValue( valueType ); loadValueEmitter( il ); il.EmitAnyStloc( value ); if ( memberName != null && nilImplication == NilImplication.Prohibit ) { /* * if( serializingValue == null )( * { * throw SerializationExceptions.NewNullIsProhibited(); * } */ if ( !valueType.IsValueType ) { il.EmitAnyLdloc( value ); var endIf = il.DefineLabel( "END_IF" ); il.EmitBrtrue_S( endIf ); il.EmitLdstr( memberName ); il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); il.EmitThrow(); il.MarkLabel( endIf ); } else if ( Nullable.GetUnderlyingType( valueType ) != null ) { il.EmitAnyLdloca( value ); il.EmitGetProperty( typeof( Nullable<> ).MakeGenericType( Nullable.GetUnderlyingType( valueType ) ).GetProperty( "HasValue" ) ); var endIf = il.DefineLabel( "END_IF" ); il.EmitBrtrue_S( endIf ); il.EmitLdstr( memberName ); il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); il.EmitThrow(); il.MarkLabel( endIf ); } } var serializerGetter = emitter.RegisterSerializer( valueType ); serializerGetter( il, 0 ); il.EmitAnyLdarg( packerArgumentIndex ); il.EmitAnyLdloc( value ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( valueType ).GetMethod( "PackTo" ) ); }
/// <summary> /// Emits the nil implication. /// </summary> /// <param name="il">The il generator.</param> /// <param name="unpackerArgumentIndex">Index of the unpacker argument.</param> /// <param name="memberName">Name of the deserializing member.</param> /// <param name="nilImplication">The nil implication.</param> /// <param name="endOfDeserialization">The label to the end of deserialization.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitNilImplication( TracingILGenerator il, int unpackerArgumentIndex, string memberName, NilImplication nilImplication, Label endOfDeserialization, LocalVariableHolder localHolder ) { switch ( nilImplication ) { case NilImplication.MemberDefault: { // TODO: This should be empty for extra items. /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); il.EmitBrtrue( endOfDeserialization ); break; } case NilImplication.Prohibit: { /* * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = localHolder.UnpackedData; il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); var endIf0 = il.DefineLabel( "END_IF0" ); il.EmitBrfalse_S( endIf0 ); il.EmitLdstr( memberName ); il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); il.EmitThrow(); il.MarkLabel( endIf0 ); break; } } }
/// <summary> /// Initializes a new instance of the <see cref="DataMemberContract"/> struct. /// </summary> /// <param name="member">The target member.</param> public DataMemberContract( MemberInfo member ) { Contract.Assert( member != null ); this._name = member.Name; this._nilImplication = NilImplication.MemberDefault; this._id = UnspecifiedId; }
public static void EmitNilImplication(TracingILGenerator il, int unpackerArgumentIndex, string memberName, NilImplication nilImplication, Label endOfDeserialization, LocalVariableHolder localHolder) { LocalBuilder unpackedData; LocalBuilder unpackedDataValue; switch (nilImplication) { case NilImplication.MemberDefault: il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.Data); unpackedData = localHolder.UnpackedData; il.EmitAnyStloc(unpackedData); il.EmitAnyLdloca(unpackedData); il.EmitGetProperty(_Nullable <MessagePackObject> .Value); unpackedDataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(unpackedDataValue); il.EmitAnyLdloca(unpackedDataValue); il.EmitGetProperty(_MessagePackObject.IsNil); il.EmitBrtrue(endOfDeserialization); break; case NilImplication.Prohibit: { il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.Data); unpackedData = localHolder.UnpackedData; il.EmitAnyStloc(unpackedData); il.EmitAnyLdloca(unpackedData); il.EmitGetProperty(_Nullable <MessagePackObject> .Value); unpackedDataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(unpackedDataValue); il.EmitAnyLdloca(unpackedDataValue); il.EmitGetProperty(_MessagePackObject.IsNil); Label target = il.DefineLabel("END_IF0"); il.EmitBrfalse_S(target); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(target); break; } } }
public static void EmitSerializeValue(SerializerEmitter emitter, TracingILGenerator il, int packerArgumentIndex, Type valueType, string memberName, NilImplication nilImplication, Action <TracingILGenerator> loadValueEmitter, LocalVariableHolder localHolder) { Contract.Requires(emitter != null); Contract.Requires(il != null); Contract.Requires(packerArgumentIndex >= 0); Contract.Requires(valueType != null); Contract.Requires(loadValueEmitter != null); LocalBuilder serializingValue = localHolder.GetSerializingValue(valueType); loadValueEmitter(il); il.EmitAnyStloc(serializingValue); if ((memberName != null) && (nilImplication == NilImplication.Prohibit)) { Label label; if (!valueType.IsValueType) { il.EmitAnyLdloc(serializingValue); label = il.DefineLabel("END_IF"); il.EmitBrtrue_S(label); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(label); } else if (Nullable.GetUnderlyingType(valueType) != null) { il.EmitAnyLdloca(serializingValue); il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(new Type[] { Nullable.GetUnderlyingType(valueType) }).GetProperty("HasValue")); label = il.DefineLabel("END_IF"); il.EmitBrtrue_S(label); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(label); } } emitter.RegisterSerializer(valueType)(il, 0); il.EmitAnyLdarg(packerArgumentIndex); il.EmitAnyLdloc(serializingValue); il.EmitAnyCall(typeof(MessagePackSerializer <>).MakeGenericType(new Type[] { valueType }).GetMethod("PackTo")); }
public static void EmitDeserializeValue( SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder value, LocalBuilder isUnpacked, string memberName, NilImplication nilImplication, Action<TracingILGenerator, int> customUnpackerReading ) { Contract.Requires( emitter != null ); Contract.Requires( il != null ); Contract.Requires( unpackerArgumentIndex >= 0 ); Contract.Requires( value != null ); if ( customUnpackerReading != null ) { customUnpackerReading( il, unpackerArgumentIndex ); } var endOfDeserialization = il.DefineLabel( "END_OF_DESERIALIZATION" ); if ( memberName != null ) { switch ( nilImplication ) { case NilImplication.MemberDefault: { /* * if( unpacker.Data.Value.IsNil ) * { * // Skip current. * goto END_OF_DESERIALIZATION; * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = il.DeclareLocal( typeof( MessagePackObject? ), "data" ); il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = il.DeclareLocal( typeof( MessagePackObject ), "dataValue" ); il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); il.EmitBrtrue( endOfDeserialization ); break; } case NilImplication.Prohibit: { /* * if( unpacker.Data.Value.IsNil ) * { * throw SerializationEceptions.NewProhibitNullException( "..." ); * } */ il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.Data ); var data = il.DeclareLocal( typeof( MessagePackObject? ), "data" ); il.EmitAnyStloc( data ); il.EmitAnyLdloca( data ); il.EmitGetProperty( Metadata._Nullable<MessagePackObject>.Value ); var dataValue = il.DeclareLocal( typeof( MessagePackObject ), "dataValue" ); il.EmitAnyStloc( dataValue ); il.EmitAnyLdloca( dataValue ); il.EmitGetProperty( Metadata._MessagePackObject.IsNil ); var endIf0 = il.DefineLabel( "END_IF0" ); il.EmitBrfalse_S( endIf0 ); il.EmitLdstr( memberName ); il.EmitAnyCall( SerializationExceptions.NewNullIsProhibitedMethod ); il.EmitThrow(); il.MarkLabel( endIf0 ); break; } } } /* * * if( !unpacker.IsArrayHeader && !unpacker.IsMapHeader ) * { * valueN = GET_SERIALIZER.UnpackFrom( unpacker ); * } * else * { * using( var subtreeUnpacker = unpacker.ReadSubtree ) * { * valueN = GET_SERIALIZER.UnpackFrom( unpacker ); * } * } * * isValueNUnpacked = true; * END_OF_DESERIALIZATION: */ var then = il.DefineLabel( "THEN" ); var endIf = il.DefineLabel( "END_IF" ); var serializerGetter = emitter.RegisterSerializer( value.LocalType ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsArrayHeader ); il.EmitBrtrue_S( then ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitGetProperty( Metadata._Unpacker.IsMapHeader ); il.EmitBrtrue_S( then ); // else serializerGetter( il, 0 ); il.EmitAnyLdarg( unpackerArgumentIndex ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( value.LocalType ).GetMethod( "UnpackFrom" ) ); il.EmitAnyStloc( value ); il.EmitBr_S( endIf ); // then var subtreeUnpacker = il.DeclareLocal( typeof( Unpacker ), "subtreeUnpacker" ); il.MarkLabel( then ); EmitUnpackerBeginReadSubtree( il, unpackerArgumentIndex, subtreeUnpacker ); serializerGetter( il, 0 ); il.EmitAnyLdloc( subtreeUnpacker ); il.EmitAnyCall( typeof( MessagePackSerializer<> ).MakeGenericType( value.LocalType ).GetMethod( "UnpackFrom" ) ); il.EmitAnyStloc( value ); EmitUnpackerEndReadSubtree( il, subtreeUnpacker ); il.MarkLabel( endIf ); if ( isUnpacked != null ) { il.EmitLdc_I4_1(); il.EmitAnyStloc( isUnpacked ); } il.MarkLabel( endOfDeserialization ); }