NewValueTypeCannotBeNull() public static method

This is intended to MsgPack for CLI internal use. Do not use this type from application directly. Returns new exception to notify that value type cannot be null on deserialization.
public static NewValueTypeCannotBeNull ( Type type ) : Exception
type System.Type The target type.
return System.Exception
        /// <summary>
        ///		Deserialize object with specified <see cref="Unpacker"/>.
        /// </summary>
        /// <param name="unpacker"><see cref="Unpacker"/> which unpacks values of resulting object tree.</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="unpacker"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="SerializationException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="MessageTypeException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="InvalidMessagePackStreamException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="NotSupportedException">
        ///		<typeparamref name="T"/> is abstract type.
        /// </exception>
        public T UnpackFrom(Unpacker unpacker)
        {
            // TODO: Hot-Path-Optimization
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (!unpacker.Data.HasValue)
            {
                throw SerializationExceptions.NewEmptyOrUnstartedUnpacker();
            }

            if (unpacker.Data.GetValueOrDefault().IsNil)
            {
                if (_isNullable)
                {
                    // null
                    return(default(T));
                }
                else
                {
                    throw SerializationExceptions.NewValueTypeCannotBeNull(typeof(T));
                }
            }

            return(this.UnpackFromCore(unpacker));
        }
Ejemplo n.º 2
0
        void IMessagePackSerializer.PackTo(Packer packer, object objectTree)
        {
            // TODO: Hot-Path-Optimization
            if (packer == null)
            {
                throw new ArgumentNullException("packer");
            }

            if (objectTree == null)
            {
                if (typeof(T).GetIsValueType())
                {
                    if (!(typeof(T).GetIsGenericType() && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>)))
                    {
                        throw SerializationExceptions.NewValueTypeCannotBeNull(typeof(T));
                    }
                }

                packer.PackNull();
                return;
            }
            else
            {
                if (!(objectTree is T))
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "'{0}' is not compatible for '{1}'.", objectTree.GetType(), typeof(T)), "objectTree");
                }
            }

            this.PackToCore(packer, ( T )objectTree);
        }
        /// <summary>
        ///		Deserialize object with specified <see cref="Unpacker"/>.
        /// </summary>
        /// <param name="unpacker"><see cref="Unpacker"/> which unpacks values of resulting object tree.</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="unpacker"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="SerializationException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="MessageTypeException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="InvalidMessagePackStreamException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="NotSupportedException">
        ///		<typeparamref name="T"/> is abstract type.
        /// </exception>
        public object UnpackFrom(Unpacker unpacker)
        {
            // TODO: Hot-Path-Optimization
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (unpacker.LastReadData.IsNil)
            {
                if (_isNullable)
                {
                    // null
                    if (_type.IsValueType)
                    {
                        return(Activator.CreateInstance(_type));
                    }
                    else
                    {
                        return(null);
                    };
                }
                else
                {
                    throw SerializationExceptions.NewValueTypeCannotBeNull(_type);
                }
            }

            return(this.UnpackFromCore(unpacker));
        }
Ejemplo n.º 4
0
 public static T ConvertWithEnsuringNotNull <T>(object boxed, string name, Type targetType)
 {
     if ((typeof(T).GetIsValueType() && (boxed == null)) && (Nullable.GetUnderlyingType(typeof(T)) == null))
     {
         throw SerializationExceptions.NewValueTypeCannotBeNull(name, typeof(T), targetType);
     }
     return((T)boxed);
 }
Ejemplo n.º 5
0
 /// <summary>
 ///		Unpacks the nil value.
 /// </summary>
 /// <returns>
 ///		A valid value of <typeparamref name="T"/> which represents 'null' state.
 /// </returns>
 /// <remarks>
 ///		<para>
 ///			This method is invoked from <see cref="UnpackFrom"/> method when the current <see cref="Unpacker.LastReadData"/> is <see cref="MessagePackObject.IsNil">nil</see>.
 ///		</para>
 ///		<para>
 ///			The implementation of this class returns <c>null</c> for nullable types (that is, all reference types and <see cref="Nullable{T}"/>); otherwise, throws <see cref="SerializationException"/>.
 ///		</para>
 ///		<para>
 ///			Custom serializers can override this method to provide custom nil representation. For example, built-in <see cref="DBNull"/> serializer overrides this method to return <see cref="DBNull.Value"/> instead of <c>null</c>.
 ///		</para>
 /// </remarks>
 protected internal virtual T UnpackNil()
 {
     if (_isNullable)
     {
         // null
         return(default(T));
     }
     else
     {
         throw SerializationExceptions.NewValueTypeCannotBeNull(typeof(T));
     }
 }
 public T UnpackFrom(Unpacker unpacker)
 {
     if (unpacker == null)
     {
         throw new ArgumentNullException("unpacker");
     }
     if (!unpacker.Data.HasValue)
     {
         throw SerializationExceptions.NewEmptyOrUnstartedUnpacker();
     }
     if (unpacker.Data.GetValueOrDefault().IsNil)
     {
         if (!MessagePackSerializer <T> ._isNullable)
         {
             throw SerializationExceptions.NewValueTypeCannotBeNull(typeof(T));
         }
         return(default(T));
     }
     return(this.UnpackFromCore(unpacker));
 }
Ejemplo n.º 7
0
        private static void VerifyNilImplication(Type type, IEnumerable <SerializingMember> entries)
        {
            foreach (var serializingMember in entries)
            {
                if (serializingMember.Contract.NilImplication == NilImplication.Null)
                {
                    var itemType = serializingMember.Member.GetMemberValueType();

                    if (itemType != typeof(MessagePackObject) &&
                        itemType.GetIsValueType() &&
                        Nullable.GetUnderlyingType(itemType) == null)
                    {
                        throw SerializationExceptions.NewValueTypeCannotBeNull(serializingMember.Member.ToString(), itemType, type);
                    }

                    bool         isReadOnly;
                    FieldInfo    asField;
                    PropertyInfo asProperty;
                    if ((asField = serializingMember.Member as FieldInfo) != null)
                    {
                        isReadOnly = asField.IsInitOnly;
                    }
                    else
                    {
                        asProperty = serializingMember.Member as PropertyInfo;
// 20150616 applibot modify
//#if DEBUG && !UNITY_IPHONE && !UNITY_ANDROID
#if DEBUG && !UNITY_IPHONE && !UNITY_ANDROID && !UNITY
                        Contract.Assert(asProperty != null, serializingMember.Member.ToString());
#endif
                        isReadOnly = asProperty.GetSetMethod() == null;
                    }

                    if (isReadOnly)
                    {
                        throw SerializationExceptions.NewNullIsProhibited(serializingMember.Member.ToString());
                    }
                }
            }
        }
        /// <summary>
        ///		Deserializes object with specified <see cref="Unpacker"/>.
        /// </summary>
        /// <param name="unpacker"><see cref="Unpacker"/> which unpacks values of resulting object tree.</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="unpacker"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="SerializationException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="MessageTypeException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="InvalidMessagePackStreamException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        public object UnpackFrom(Unpacker unpacker)
        {
            // TODO: Hot-Path-Optimization
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (unpacker.LastReadData.IsNil)
            {
                if (this._isNullable)
                {
                    // null
                    return(null);
                }
                else
                {
                    throw SerializationExceptions.NewValueTypeCannotBeNull(this._targetType);
                }
            }

            return(this.UnpackFromCore(unpacker));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///		Deserializes object with specified <see cref="Unpacker"/>.
        /// </summary>
        /// <param name="unpacker"><see cref="Unpacker"/> which unpacks values of resulting object tree.</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="unpacker"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="SerializationException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="MessageTypeException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="InvalidMessagePackStreamException">
        ///		Failed to deserialize object due to invalid unpacker state, stream content, or so.
        /// </exception>
        /// <exception cref="NotSupportedException">
        ///		<typeparamref name="T"/> is abstract type.
        /// </exception>
        public T UnpackFrom(Unpacker unpacker)
        {
            // TODO: Hot-Path-Optimization
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (unpacker.LastReadData.IsNil)
            {
                if (_isNullable)
                {
                    // null
                    return(default(T));
                }
                // ReSharper disable once RedundantIfElseBlock
                else
                {
                    throw SerializationExceptions.NewValueTypeCannotBeNull(typeof(T));
                }
            }

            return(this.UnpackFromCore(unpacker));
        }