示例#1
0
        public override bool IsMemberDeserializable(MemberInfo member, IMemberInfo info)
        {
            var ro = AttributeHelper.GetAttribute <System.ComponentModel.ReadOnlyAttribute> (member, true);

            if (ro != null)
            {
                return(ro.IsReadOnly == false);
            }
            var s = AttributeHelper.GetAttribute <JsonSerializableAttribute> (member, true);

            if (s != null)
            {
                return(true);
            }
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && _ContractualTypes.Contains(member.DeclaringType))
            {
                return(AttributeHelper.HasAttribute <DataMemberAttribute> (member, true));
            }
            if (UseDataContractAttributes && AttributeHelper.HasAttribute <IgnoreDataMemberAttribute> (member, true))
            {
                return(false);
            }
#endif
            if (UseXmlSerializationAttributes && AttributeHelper.HasAttribute <XmlIgnoreAttribute> (member, false))
            {
                return(false);
            }
            return(base.IsMemberDeserializable(member, info));
        }
示例#2
0
        /// <summary>
        /// Gets the overridden name for an enum value. The overridden name can be set via the <see cref="JsonEnumValueAttribute"/>. If null or empty string is returned, the original name of the enum value is used.
        /// </summary>
        /// <param name="member">The enum value member.</param>
        /// <returns>The name of the enum value.</returns>
        public override string GetEnumValueName(MemberInfo member)
        {
            var a = AttributeHelper.GetAttribute <JsonEnumValueAttribute> (member, false);

            if (a != null)
            {
                return(a.Name);
            }
#if NET_40_OR_GREATER
            if (UseDataContractAttributes)
            {
                var m = AttributeHelper.GetAttribute <EnumMemberAttribute> (member, true);
                if (m != null && String.IsNullOrEmpty(m.Value) == false)
                {
                    return(m.Value);
                }
            }
#endif
            if (UseXmlSerializationAttributes)
            {
                var m = AttributeHelper.GetAttribute <XmlEnumAttribute> (member, true);
                if (m != null)
                {
                    return(m.Name);
                }
            }
            return(null);
        }
示例#3
0
        /// <summary>
        /// This method returns an <see cref="IJsonConverter"/> instance to convert item values for a field or a property which is of <see cref="IEnumerable"/> type during serialization and deserialization. If no converter is used, null can be returned. The converter can be set via <see cref="JsonItemConverterAttribute"/>.
        /// </summary>
        /// <param name="member">The <see cref="MemberInfo"/> of the field or property.</param>
        /// <returns>The converter.</returns>
        public override IJsonConverter GetMemberItemConverter(MemberInfo member)
        {
            var cv = AttributeHelper.GetAttribute <JsonItemConverterAttribute> (member, true);

            if (cv != null)
            {
                return(cv.Converter);
            }
            return(null);
        }
示例#4
0
        /// <summary>
        /// Gets the name of the collection container for customized types which implement <see cref="IEnumerable"/> and have extra members to be serialized.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> which implements <see cref="IEnumerable"/> and contains extra members to be serialized.</param>
        /// <returns>The name of the collection container. If null is returned, no container will be created.</returns>
        /// <remarks>By default, the serializer serializes types which implement <see cref="IEnumerable"/> interface as JSON arrays.
        /// Members of the type will be ignored. And so to the types which implements <see cref="IDictionary"/>.
        /// To serialize those members, return a non-empty name and the members will be serialized into a field named returned by this method.</remarks>
        public override string GetCollectionContainerName(Type type)
        {
            var n = AttributeHelper.GetAttribute <JsonCollectionAttribute> (type, true);

            if (n != null && String.IsNullOrEmpty(n.Name) == false)
            {
                return(n.Name);
            }
            return(null);
        }
示例#5
0
        /// <summary>
        /// This method is called to get the <see cref="IJsonConverter"/> for the type. If no converter, null should be returned. The converter can be set via <see cref="JsonConverterAttribute"/>.
        /// </summary>
        /// <param name="type">The type to be checked for <see cref="IJsonConverter"/>.</param>
        /// <returns>The interceptor.</returns>
        public override IJsonConverter GetConverter(Type type)
        {
            var ia = AttributeHelper.GetAttribute <JsonConverterAttribute> (type, true);

            if (ia != null)
            {
                return(ia.Converter);
            }
            return(null);
        }
示例#6
0
        /// <summary>
        /// Gets the type alias which can be used to determine derived types for abstract types, interface types or the <see cref="object" /> type during deserialization.
        /// </summary>
        /// <param name="type">The type to be serialized.</param>
        /// <returns>
        /// The alias denotes the type.
        /// </returns>
        public override string GetTypeAlias(Type type)
        {
            var a = AttributeHelper.GetAttribute <JsonTypeAliasAttribute> (type, false);

            if (a != null)
            {
                return(a.Name);
            }
            return(null);
        }
示例#7
0
        internal static bool IsAnonymous(this Type type)
        {
            if (type.IsGenericType == false || (type.Attributes & TypeAttributes.NotPublic) != TypeAttributes.NotPublic)
            {
                return(false);
            }
            var n = type.Name;

            return((n.StartsWith("<>", StringComparison.Ordinal) || n.StartsWith("VB$", StringComparison.Ordinal)) &&
                   n.Contains("AnonymousType") &&
                   AttributeHelper.GetAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>(type, false) != null);
        }
示例#8
0
        /// <summary>
        /// Returns the <see cref="IJsonInterceptor"/> for given type. If no interceptor, null should be returned. The interceptor can be set via <see cref="JsonInterceptorAttribute"/>.
        /// </summary>
        /// <param name="type">The type to be checked.</param>
        /// <returns>The interceptor.</returns>
        public override IJsonInterceptor GetInterceptor(Type type)
        {
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && AttributeHelper.HasAttribute <DataContractAttribute> (type, false))
            {
                _ContractualTypes.Add(type);
            }
#endif
            var ia = AttributeHelper.GetAttribute <JsonInterceptorAttribute> (type, true);
            if (ia != null)
            {
                return(ia.Interceptor);
            }
            return(null);
        }
示例#9
0
        /// <summary>
        /// This method returns a series of values that will not be serialized for a field or a property. When the value of the member matches those values, it will not be serialized. If all values can be serialized, null should be returned. The value can be set via <see cref="System.ComponentModel.DefaultValueAttribute"/>.
        /// </summary>
        /// <param name="member">The <see cref="MemberInfo"/> of the field or property.</param>
        /// <returns>The values which are not serialized for <paramref name="member"/>.</returns>
        public override IEnumerable GetNonSerializedValues(MemberInfo member)
        {
            var a = AttributeHelper.GetAttribute <System.ComponentModel.DefaultValueAttribute> (member, true);
            var n = AttributeHelper.GetAttributes <JsonNonSerializedValueAttribute> (member, true);

            if (a == null && n.Length == 0)
            {
                return(null);
            }
            var v = new List <object> ();

            if (a != null)
            {
                v.Add(a.Value);
            }
            foreach (var item in n)
            {
                v.Add(item.Value);
            }
            return(v);
        }
示例#10
0
        public override bool?IsMemberSerializable(MemberInfo member, IMemberInfo info)
        {
            var ic = AttributeHelper.GetAttribute <JsonIncludeAttribute> (member, true);

            if (ic != null)
            {
                return(ic.Include ? true : false);
            }
            if (AttributeHelper.HasAttribute <JsonSerializableAttribute> (member, true))
            {
                return(true);
            }
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && _ContractualTypes.Contains(member.DeclaringType))
            {
                return(AttributeHelper.HasAttribute <DataMemberAttribute> (member, true));
            }
            if (UseDataContractAttributes && AttributeHelper.HasAttribute <IgnoreDataMemberAttribute> (member, true))
            {
                return(false);
            }
#endif
            if (UseXmlSerializationAttributes && AttributeHelper.HasAttribute <XmlIgnoreAttribute> (member, false))
            {
                return(false);
            }
            if (IgnoreAttributes != null && IgnoreAttributes.Count > 0)
            {
                foreach (var item in IgnoreAttributes)
                {
                    if (member.IsDefined(item, false))
                    {
                        return(false);
                    }
                }
            }
            return(base.IsMemberSerializable(member, info));
        }
示例#11
0
        /// <summary>
        /// This method is called to determine whether the values of the given <see cref="Enum"/> type should be serialized as its numeric form rather than literal form. The override can be set via the <see cref="JsonEnumFormatAttribute"/>.
        /// </summary>
        /// <param name="type">An <see cref="Enum"/> value type.</param>
        /// <returns>If the type should be serialized numerically, returns true, otherwise, false.</returns>
        public override EnumValueFormat GetEnumValueFormat(Type type)
        {
            var a = AttributeHelper.GetAttribute <JsonEnumFormatAttribute> (type, false);

            return(a != null ? a.Format : EnumValueFormat.Default);
        }
示例#12
0
        /// <summary>
        /// This method returns possible names for corresponding types of a field or a property. This enables polymorphic serialization and deserialization for abstract classes, interfaces, or object types, with predetermined concrete types. If polymorphic serialization is not used, null or an empty <see cref="SerializedNames"/> could be returned. The names can be set via <see cref="JsonFieldAttribute"/>.
        /// </summary>
        /// <param name="member">The <see cref="MemberInfo"/> of the field or property.</param>
        /// <returns>The dictionary contains types and their corresponding names.</returns>
        /// <exception cref="InvalidCastException">The <see cref="JsonFieldAttribute.DataType"/> type does not derive from the member type.</exception>
        public override SerializedNames GetSerializedNames(MemberInfo member)
        {
            var tn = new SerializedNames();
            var jf = AttributeHelper.GetAttributes <JsonFieldAttribute> (member, true);
            var f  = member as FieldInfo;
            var p  = member as PropertyInfo;
            var t  = p != null ? p.PropertyType : f.FieldType;

            foreach (var item in jf)
            {
                if (String.IsNullOrEmpty(item.Name))
                {
                    continue;
                }
                if (item.DataType == null)
                {
                    tn.DefaultName = item.Name;
                }
                else
                {
                    if (t.IsAssignableFrom(item.DataType) == false)
                    {
                        throw new InvalidCastException("The override type (" + item.DataType.FullName + ") does not derive from the member type (" + t.FullName + ")");
                    }
                    tn.Add(item.DataType, item.Name);
                }
            }
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && jf.Length == 0)
            {
                var m = AttributeHelper.GetAttribute <DataMemberAttribute> (member, true);
                if (m != null && String.IsNullOrEmpty(m.Name) == false)
                {
                    tn.DefaultName = m.Name;
                }
            }
#endif
            if (UseXmlSerializationAttributes)
            {
                var ar = AttributeHelper.GetAttribute <XmlArrayAttribute> (member, true);
                if (ar != null)
                {
                    tn.DefaultName = ar.ElementName;
                    return(tn);
                }
                var an = AttributeHelper.GetAttribute <XmlAttributeAttribute> (member, true);
                if (an != null)
                {
                    tn.DefaultName = an.AttributeName;
                    return(tn);
                }
                if (typeof(IEnumerable).IsAssignableFrom(t))
                {
                    return(tn);
                }
                foreach (var item in AttributeHelper.GetAttributes <XmlElementAttribute> (member, true))
                {
                    if (String.IsNullOrEmpty(item.ElementName))
                    {
                        continue;
                    }
                    if (item.Type == null)
                    {
                        tn.DefaultName = item.ElementName;
                    }
                    else
                    {
                        if (t.IsAssignableFrom(item.Type) == false)
                        {
                            throw new InvalidCastException("The override type (" + item.Type.FullName + ") does not derive from the member type (" + t.FullName + ")");
                        }
                        tn.Add(item.Type, item.ElementName);
                    }
                }
            }
            return(tn);
        }