示例#1
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);
        }
示例#2
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));
 }
示例#3
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);
        }
示例#4
0
        internal ReflectionCache(Type type)
        {
            Type                 = type;
            TypeName             = type.FullName;
            AssemblyName         = type.AssemblyQualifiedName;
            CircularReferencable = type.IsValueType == false || type != typeof(string);
            IsAbstract           = type.IsUndetermined();
            IsAnonymous          = type.IsAnonymous();

            JsonDataType      = Reflection.GetJsonDataType(type);
            SerializeMethod   = JsonSerializer.GetWriteJsonMethod(type);
            DeserializeMethod = JsonDeserializer.GetReadJsonMethod(type);

            if (JsonDataType == JsonDataType.Enum)
            {
                IsFlaggedEnum = AttributeHelper.HasAttribute <FlagsAttribute> (type, false);
                return;
            }

            if (type.IsPointer)
            {
                throw new JsonSerializationException("Can not serialize pointer type: " + type.AssemblyQualifiedName);
            }
            if (type.IsArray)
            {
                ArgumentTypes = new Type[] { type.GetElementType() };
                CommonType    = type.GetArrayRank() == 1 ? ComplexType.Array : ComplexType.MultiDimensionalArray;
            }
            else
            {
                var t = type;
                if (t.IsGenericType == false)
                {
                    while ((t = t.BaseType) != null)
                    {
                        if (t.IsGenericType)
                        {
                            break;
                        }
                    }
                }
                if (t != null)
                {
                    ArgumentTypes = t.GetGenericArguments();
                    var gt = t.GetGenericTypeDefinition();
                    if (gt.Equals(typeof(Dictionary <,>)))
                    {
                        CommonType = ComplexType.Dictionary;
                    }
                    else if (gt.Equals(typeof(List <>)))
                    {
                        CommonType = ComplexType.List;
                    }
                    else if (gt.Equals(typeof(Nullable <>)))
                    {
                        CommonType      = ComplexType.Nullable;
                        SerializeMethod = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]);
                    }
                }
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                if (typeof(Array).IsAssignableFrom(type) == false)
                {
                    AppendItem = Reflection.CreateWrapperMethod <AddCollectionItem> (Reflection.FindMethod(type, "Add", new Type[] { null }));
                }
                if (ArgumentTypes != null && ArgumentTypes.Length == 1)
                {
                    ItemSerializer   = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]);
                    ItemDeserializer = JsonDeserializer.GetReadJsonMethod(ArgumentTypes[0]);
                }
            }
            if (ArgumentTypes != null)
            {
                ArgumentReflections = new ReflectionCache[ArgumentTypes.Length];
            }
            if (CommonType != ComplexType.Array &&
                CommonType != ComplexType.MultiDimensionalArray &&
                CommonType != ComplexType.Nullable)
            {
                if (type.IsPubliclyAccessible() == false)
                {
                    ConstructorInfo |= ConstructorTypes.NonPublic;
                }
                if (type.IsClass || type.IsValueType)
                {
                    try {
                        Constructor = Reflection.CreateConstructorMethod(type, type.IsVisible == false || typeof(DatasetSchema).Equals(type));
                    }
                    catch (Exception ex) {
                        throw new JsonSerializationException("Error occurred when creating constructor method for type " + type.AssemblyQualifiedName, ex);
                    }
                    if (Constructor != null && Constructor.Method.IsPublic == false)
                    {
                        ConstructorInfo |= ConstructorTypes.NonPublic;
                    }
                    if (Constructor == null)
                    {
                        var c = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        if (c != null && c.Length > 0)
                        {
                            ConstructorInfo |= ConstructorTypes.Parametric;
                        }
                    }

                    Members = Reflection.GetMembers(type);
                }
            }
            //if (typeof (IEnumerable).IsAssignableFrom (type)) {
            //	return;
            //}
            //if (JsonDataType != JsonDataType.Undefined) {
            //	return;
            //}
        }