Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonContainerContract"/> class.
        /// </summary>
        /// <param name="underlyingType">The underlying type for the contract.</param>
        internal JsonContainerContract(Type underlyingType)
            : base(underlyingType)
        {
            JsonContainerAttribute jsonContainerAttribute = JsonTypeReflector.GetJsonContainerAttribute(underlyingType);

            if (jsonContainerAttribute != null)
            {
                if (jsonContainerAttribute.ItemConverterType != null)
                {
                    ItemConverter = JsonConverterAttribute.CreateJsonConverterInstance(jsonContainerAttribute.ItemConverterType);
                }

                ItemIsReference           = jsonContainerAttribute._itemIsReference;
                ItemReferenceLoopHandling = jsonContainerAttribute._itemReferenceLoopHandling;
                ItemTypeNameHandling      = jsonContainerAttribute._itemTypeNameHandling;
            }
        }
Ejemplo n.º 2
0
        public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
        {
            Type converterType = GetJsonConverterType(attributeProvider);

            if (converterType != null)
            {
                JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType);

                if (!memberConverter.CanConvert(targetConvertedType))
                {
                    throw new JsonSerializationException("JsonConverter {0} on {1} is not compatible with member type {2}.".FormatWith(CultureInfo.InvariantCulture, memberConverter.GetType().Name, attributeProvider, targetConvertedType.Name));
                }

                return(memberConverter);
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
        {
            object provider = null;

#if !(NETFX_CORE || PORTABLE)
            provider = attributeProvider as MemberInfo;
#else
            provider = attributeProvider.UnderlyingObject;
#endif

            Type converterType = GetJsonConverterType(attributeProvider);

            if (converterType != null)
            {
                JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType);

                return(memberConverter);
            }

            return(null);
        }
        public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
        {
            Type jsonConverterType = JsonTypeReflector.GetJsonConverterType(attributeProvider);

            if (jsonConverterType == null)
            {
                return(null);
            }
            JsonConverter jsonConverter = JsonConverterAttribute.CreateJsonConverterInstance(jsonConverterType);

            if (!jsonConverter.CanConvert(targetConvertedType))
            {
                throw new JsonSerializationException("JsonConverter {0} on {1} is not compatible with member type {2}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                {
                    jsonConverter.GetType().get_Name(),
                    attributeProvider,
                    targetConvertedType.get_Name()
                }));
            }
            return(jsonConverter);
        }
        private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute)
        {
            hasExplicitAttribute = false;

#if !PocketPC && !NET20
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);

            MemberInfo memberInfo = null;
#if !(NETFX_CORE || PORTABLE)
            memberInfo = attributeProvider as MemberInfo;
#else
            memberInfo = attributeProvider.UnderlyingObject as MemberInfo;
#endif

            DataMemberAttribute dataMemberAttribute;
            if (dataContractAttribute != null && memberInfo != null)
            {
                dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)memberInfo);
            }
            else
            {
                dataMemberAttribute = null;
            }
#endif

            JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);
            if (propertyAttribute != null)
            {
                hasExplicitAttribute = true;
            }

            string mappedName;
            if (propertyAttribute != null && propertyAttribute.PropertyName != null)
            {
                mappedName = propertyAttribute.PropertyName;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
            {
                mappedName = dataMemberAttribute.Name;
            }
#endif
            else
            {
                mappedName = name;
            }

            property.PropertyName   = ResolvePropertyName(mappedName);
            property.UnderlyingName = name;

            if (propertyAttribute != null)
            {
                property._required = propertyAttribute._required;
                property.Order     = propertyAttribute._order;
            }
#if !PocketPC && !NET20
            else if (dataMemberAttribute != null)
            {
                property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
                property.Order     = (dataMemberAttribute.Order != -1) ? (int?)dataMemberAttribute.Order : null;
            }
#endif

            bool hasJsonIgnoreAttribute =
                JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
                || JsonTypeReflector.GetAttribute <NonSerializedAttribute>(attributeProvider) != null
#endif
            ;

            if (memberSerialization != MemberSerialization.OptIn)
            {
                // ignored if it has JsonIgnore or NonSerialized attributes
                property.Ignored = hasJsonIgnoreAttribute;
            }
            else
            {
                // ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes
                property.Ignored =
                    hasJsonIgnoreAttribute ||
                    (propertyAttribute == null
#if !PocketPC && !NET20
                     && dataMemberAttribute == null
#endif
                    );
            }

            // resolve converter for property
            // the class type might have a converter but the property converter takes presidence
            property.Converter       = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);

            DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider);
            property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;

            property.NullValueHandling      = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
            property.DefaultValueHandling   = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
            property.ReferenceLoopHandling  = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
            property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
            property.TypeNameHandling       = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
            property.IsReference            = (propertyAttribute != null) ? propertyAttribute._isReference : null;

            property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null;
            property.ItemConverter   =
                (propertyAttribute != null && propertyAttribute.ItemConverterType != null)
          ? JsonConverterAttribute.CreateJsonConverterInstance(propertyAttribute.ItemConverterType)
          : null;
            property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null;
            property.ItemTypeNameHandling      = (propertyAttribute != null) ? propertyAttribute._itemTypeNameHandling : null;

            allowNonPublicAccess = false;
            if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (propertyAttribute != null)
            {
                allowNonPublicAccess = true;
            }
            if (memberSerialization == MemberSerialization.Fields)
            {
                allowNonPublicAccess = true;
            }

#if !PocketPC && !NET20
            if (dataMemberAttribute != null)
            {
                allowNonPublicAccess = true;
                hasExplicitAttribute = true;
            }
#endif
        }
Ejemplo n.º 6
0
        private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
        {
            JsonPropertyAttribute attribute1 = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);

            if (attribute1 != null)
            {
                property.HasMemberAttribute = true;
            }
            string propertyName = attribute1 == null || attribute1.PropertyName == null ? name : attribute1.PropertyName;

            property.PropertyName   = this.ResolvePropertyName(propertyName);
            property.UnderlyingName = name;
            bool flag1 = false;

            if (attribute1 != null)
            {
                property._required            = attribute1._required;
                property.Order                = attribute1._order;
                property.DefaultValueHandling = attribute1._defaultValueHandling;
                flag1 = true;
            }
            bool flag2 = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null || JsonTypeReflector.GetAttribute <NonSerializedAttribute>(attributeProvider) != null;

            if (memberSerialization != MemberSerialization.OptIn)
            {
                bool flag3 = false;
                property.Ignored = flag2 || flag3;
            }
            else
            {
                property.Ignored = flag2 || !flag1;
            }
            property.Converter       = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            DefaultValueAttribute attribute2 = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider);

            if (attribute2 != null)
            {
                property.DefaultValue = attribute2.Value;
            }
            property.NullValueHandling         = attribute1 != null ? attribute1._nullValueHandling : new NullValueHandling?();
            property.ReferenceLoopHandling     = attribute1 != null ? attribute1._referenceLoopHandling : new ReferenceLoopHandling?();
            property.ObjectCreationHandling    = attribute1 != null ? attribute1._objectCreationHandling : new ObjectCreationHandling?();
            property.TypeNameHandling          = attribute1 != null ? attribute1._typeNameHandling : new TypeNameHandling?();
            property.IsReference               = attribute1 != null ? attribute1._isReference : new bool?();
            property.ItemIsReference           = attribute1 != null ? attribute1._itemIsReference : new bool?();
            property.ItemConverter             = attribute1 == null || attribute1.ItemConverterType == null ? (JsonConverter)null : JsonConverterAttribute.CreateJsonConverterInstance(attribute1.ItemConverterType);
            property.ItemReferenceLoopHandling = attribute1 != null ? attribute1._itemReferenceLoopHandling : new ReferenceLoopHandling?();
            property.ItemTypeNameHandling      = attribute1 != null ? attribute1._itemTypeNameHandling : new TypeNameHandling?();
            allowNonPublicAccess               = false;
            if ((this.DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (attribute1 != null)
            {
                allowNonPublicAccess = true;
            }
            if (memberSerialization != MemberSerialization.Fields)
            {
                return;
            }
            allowNonPublicAccess = true;
        }
Ejemplo n.º 7
0
        private void SetPropertySettingsFromAttributes(JsonProperty property, object attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
        {
            DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
            MemberInfo            memberInfo            = attributeProvider as MemberInfo;
            DataMemberAttribute   dataMemberAttribute;

            if (dataContractAttribute != null && memberInfo != null)
            {
                dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute(memberInfo);
            }
            else
            {
                dataMemberAttribute = null;
            }
            JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);

            if (attribute != null)
            {
                property.HasMemberAttribute = true;
            }
            string propertyName;

            if (attribute != null && attribute.PropertyName != null)
            {
                propertyName = attribute.PropertyName;
            }
            else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
            {
                propertyName = dataMemberAttribute.Name;
            }
            else
            {
                propertyName = name;
            }
            property.PropertyName   = this.ResolvePropertyName(propertyName);
            property.UnderlyingName = name;
            bool flag = false;

            if (attribute != null)
            {
                property._required            = attribute._required;
                property.Order                = attribute._order;
                property.DefaultValueHandling = attribute._defaultValueHandling;
                flag = true;
            }
            else if (dataMemberAttribute != null)
            {
                property._required            = new Required?(dataMemberAttribute.IsRequired ? Required.AllowNull : Required.Default);
                property.Order                = ((dataMemberAttribute.Order != -1) ? new int?(dataMemberAttribute.Order) : null);
                property.DefaultValueHandling = ((!dataMemberAttribute.EmitDefaultValue) ? new DefaultValueHandling?(DefaultValueHandling.Ignore) : null);
                flag = true;
            }
            bool flag2 = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null || JsonTypeReflector.GetAttribute <JsonExtensionDataAttribute>(attributeProvider) != null || JsonTypeReflector.GetAttribute <NonSerializedAttribute>(attributeProvider) != null;

            if (memberSerialization != MemberSerialization.OptIn)
            {
                bool flag3 = JsonTypeReflector.GetAttribute <IgnoreDataMemberAttribute>(attributeProvider) != null;
                property.Ignored = (flag2 || flag3);
            }
            else
            {
                property.Ignored = (flag2 || !flag);
            }
            property.Converter       = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
            DefaultValueAttribute attribute2 = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider);

            if (attribute2 != null)
            {
                property.DefaultValue = attribute2.Value;
            }
            property.NullValueHandling         = ((attribute != null) ? attribute._nullValueHandling : null);
            property.ReferenceLoopHandling     = ((attribute != null) ? attribute._referenceLoopHandling : null);
            property.ObjectCreationHandling    = ((attribute != null) ? attribute._objectCreationHandling : null);
            property.TypeNameHandling          = ((attribute != null) ? attribute._typeNameHandling : null);
            property.IsReference               = ((attribute != null) ? attribute._isReference : null);
            property.ItemIsReference           = ((attribute != null) ? attribute._itemIsReference : null);
            property.ItemConverter             = ((attribute != null && attribute.ItemConverterType != null) ? JsonConverterAttribute.CreateJsonConverterInstance(attribute.ItemConverterType) : null);
            property.ItemReferenceLoopHandling = ((attribute != null) ? attribute._itemReferenceLoopHandling : null);
            property.ItemTypeNameHandling      = ((attribute != null) ? attribute._itemTypeNameHandling : null);
            allowNonPublicAccess               = false;
            if ((this.DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (attribute != null)
            {
                allowNonPublicAccess = true;
            }
            if (memberSerialization == MemberSerialization.Fields)
            {
                allowNonPublicAccess = true;
            }
            if (dataMemberAttribute != null)
            {
                allowNonPublicAccess        = true;
                property.HasMemberAttribute = true;
            }
        }