Пример #1
0
        /// <summary>
        /// Determines which contract type is created for the given type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>A <see cref="JsonContract"/> for the given type.</returns>
        protected virtual JsonContract CreateContract(Type objectType)
        {
            Type t = ReflectionUtils.EnsureNotNullableType(objectType);

            if (JsonConvert.IsJsonPrimitiveType(t))
            {
                return(CreatePrimitiveContract(t));
            }

            if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
            {
                return(CreateObjectContract(t));
            }

            if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
            {
                return(CreateArrayContract(t));
            }

            if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
            {
                return(CreateLinqContract(t));
            }

            if (CollectionUtils.IsDictionaryType(t))
            {
                return(CreateDictionaryContract(t));
            }

            if (typeof(IEnumerable).IsAssignableFrom(t))
            {
                return(CreateArrayContract(t));
            }

            if (CanConvertToString(t))
            {
                return(CreateStringContract(t));
            }

#if !((UNITY_WINRT && !UNITY_EDITOR) || (UNITY_WP8 || UNITY_WP_8_1))
            if (typeof(ISerializable).IsAssignableFrom(t))
            {
                return(CreateISerializableContract(t));
            }
#endif
            return(CreateObjectContract(t));
        }
Пример #2
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;
            }
        }
Пример #3
0
        /// <summary>
        /// Gets the serializable members for the type.
        /// </summary>
        /// <param name="objectType">The type to get serializable members for.</param>
        /// <returns>The serializable members for the type.</returns>
        protected virtual List <MemberInfo> GetSerializableMembers(Type objectType)
        {
            List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
                                               .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
            List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                                           .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();

            List <MemberInfo> serializableMembers = new List <MemberInfo>();

            foreach (MemberInfo member in allMembers)
            {
                // exclude members that are compiler generated if set
                if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true))
                {
                    if (defaultMembers.Contains(member))
                    {
                        // add all members that are found by default member search
                        serializableMembers.Add(member);
                    }
                    else
                    {
                        // add members that are explicitly marked with JsonProperty/DataMember attribute
                        if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null)
                        {
                            serializableMembers.Add(member);
                        }
                    }
                }
            }

            Type match;

            // don't include EntityKey on entities objects... this is a bit hacky
            if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match))
            {
                serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList();
            }

            return(serializableMembers);
        }
Пример #4
0
        private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute)
        {
            hasExplicitAttribute = false;

            JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider);

            if (propertyAttribute != null)
            {
                hasExplicitAttribute = true;
            }

            bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null);

            string mappedName;

            if (propertyAttribute != null && propertyAttribute.PropertyName != null)
            {
                mappedName = propertyAttribute.PropertyName;
            }
            else
            {
                mappedName = name;
            }

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

            if (propertyAttribute != null)
            {
                property.Required = propertyAttribute.Required;
                property.Order    = propertyAttribute._order;
            }
            else
            {
                property.Required = Required.Default;
            }

            property.Ignored = (hasIgnoreAttribute ||
                                (memberSerialization == MemberSerialization.OptIn &&
                                 propertyAttribute == null
                                ));

            // 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;

            allowNonPublicAccess = false;
            if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
            {
                allowNonPublicAccess = true;
            }
            if (propertyAttribute != null)
            {
                allowNonPublicAccess = true;
            }
        }
Пример #5
0
 /// <summary>
 /// Resolves the default <see cref="JsonConverter" /> for the contract.
 /// </summary>
 /// <param name="objectType">Type of the object.</param>
 /// <returns></returns>
 protected virtual JsonConverter ResolveContractConverter(Type objectType)
 {
     return(JsonTypeReflector.GetJsonConverter(objectType, objectType));
 }