コード例 #1
0
        /// <summary>
        /// Gets whether a field or a property is deserializable. If false is returned, the member will be excluded from deserialization. By default, writable fields or properties are deserializable. The value can be set via <see cref="System.ComponentModel.ReadOnlyAttribute"/>.
        /// </summary>
        /// <param name="member">The member to be serialized.</param>
        /// <param name="info">Reflection information for the member.</param>
        /// <returns>True is returned if the member is serializable, otherwise, false.</returns>
        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
ファイル: Reflection.cs プロジェクト: qyezzard/PowerJSON
        internal static MemberCache[] GetMembers(Type type)
        {
            var pl = type.GetProperties(__ReflectionFlags);
            var fl = type.GetFields(__ReflectionFlags);
            var c  = new List <MemberCache> (pl.Length + fl.Length);

            foreach (var m in pl)
            {
                if (m.GetIndexParameters().Length > 0)                   // Property is an indexer
                {
                    continue;
                }
                c.Add(new MemberCache(m));
            }
            foreach (var m in fl)
            {
                if (m.IsLiteral == false)
                {
                    c.Add(new MemberCache(m));
                }
            }
            c = c.FindAll(m => { return((m.HasPublicGetter || m.HasPublicSetter) || AttributeHelper.HasAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute> (m.MemberInfo, true) == false); });
            var r = new MemberCache[c.Count];

            c.CopyTo(r, 0);
            return(r);
        }
コード例 #3
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);
        }
コード例 #4
0
        /// <summary>
        /// Returns whether the specific member is serializable. This value can be set via <see cref="JsonIncludeAttribute"/> and <see cref="IgnoreAttributes"/>.
        /// If true is returned, the member will always get serialized.
        /// If false is returned, the member will be excluded from serialization.
        /// If null is returned, the serialization of the member will be determined by the settings in <see cref="JSONParameters"/>.
        /// </summary>
        /// <param name="member">The member to be serialized.</param>
        /// <param name="info">Reflection information for the member.</param>
        /// <returns>True is returned if the member is serializable, otherwise, false.</returns>
        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));
        }
コード例 #5
0
ファイル: ReflectionCache.cs プロジェクト: qyezzard/PowerJSON
        internal ReflectionCache(Type type)
        {
            Type         = type;
            TypeName     = type.FullName;
            AssemblyName = type.AssemblyQualifiedName;

            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.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[1] {
                        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)
            {
                var t = type;
                if (type.IsNested == false && type.IsPublic == false)
                {
                    ConstructorInfo |= ConstructorTypes.NonPublic;
                }
                else
                {
                    while (t != null && t.IsNested)
                    {
                        if (t.IsNestedPublic == false)
                        {
                            ConstructorInfo |= ConstructorTypes.NonPublic;
                        }
                        t = t.DeclaringType;
                    }
                }
                if (type.IsClass || type.IsValueType)
                {
                    Constructor = Reflection.CreateConstructorMethod(type, type.IsVisible == false || typeof(DatasetSchema).Equals(type));
                    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;
            //}
        }
コード例 #6
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));
 }