Пример #1
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;
                }
                foreach (var item in AttributeHelper.GetAttributes <XmlElementAttribute> (member, true))
                {
                    if (String.IsNullOrEmpty(item.ElementName))
                    {
                        continue;
                    }
                    if (item.DataType == 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);
                    }
                }
                var an = AttributeHelper.GetAttribute <XmlAttributeAttribute> (member, true);
                if (an != null)
                {
                    tn.DefaultName = an.AttributeName;
                }
            }
            return(tn);
        }
Пример #2
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);
        }
Пример #3
0
 /// <summary>
 /// Gets whether the type is always deserializable. The value can be set via <see cref="JsonSerializableAttribute"/>.
 /// </summary>
 /// <param name="type">The type to be deserialized.</param>
 /// <returns>Whether the type can be deserialized even if it is a non-public type.</returns>
 public override bool IsAlwaysDeserializable(Type type)
 {
     return(AttributeHelper.HasAttribute <JsonSerializableAttribute> (type, false));
 }