Ejemplo n.º 1
0
        public virtual void Write(Enum value)
        {
            string enumName = null;

            Type type = value.GetType();

            if (type.IsDefined(typeof(FlagsAttribute), true) && !Enum.IsDefined(type, value))
            {
                Enum[]   flags     = JsonWriter.GetFlagList(type, value);
                string[] flagNames = new string[flags.Length];
                for (int i = 0; i < flags.Length; i++)
                {
                    flagNames[i] = JsonNameAttribute.GetJsonName(flags[i]);
                    if (String.IsNullOrEmpty(flagNames[i]))
                    {
                        flagNames[i] = flags[i].ToString("f");
                    }
                }
                enumName = String.Join(", ", flagNames);
            }
            else
            {
                enumName = JsonNameAttribute.GetJsonName(value);
                if (String.IsNullOrEmpty(enumName))
                {
                    enumName = value.ToString("f");
                }
            }

            this.Write(enumName);
        }
Ejemplo n.º 2
0
        public virtual void Write(Enum value)
        {
            Type   type = value.GetType();
            string value2;

            if (type.IsDefined(typeof(FlagsAttribute), true) && !Enum.IsDefined(type, value))
            {
                Enum[]   flagList = JsonWriter.GetFlagList(type, value);
                string[] array    = new string[flagList.Length];
                for (int i = 0; i < flagList.Length; i++)
                {
                    array[i] = JsonNameAttribute.GetJsonName(flagList[i]);
                    if (string.IsNullOrEmpty(array[i]))
                    {
                        array[i] = flagList[i].ToString("f");
                    }
                }
                value2 = string.Join(", ", array);
            }
            else
            {
                value2 = JsonNameAttribute.GetJsonName(value);
                if (string.IsNullOrEmpty(value2))
                {
                    value2 = value.ToString("f");
                }
            }
            this.Write(value2);
        }
Ejemplo n.º 3
0
        private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
        {
            Dictionary <string, MemberInfo> dictionary;

            if (this.MemberMapCache.TryGetValue(objectType, out dictionary))
            {
                return(dictionary);
            }
            dictionary = new Dictionary <string, MemberInfo>();
            PropertyInfo[] properties = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            PropertyInfo[] array      = properties;
            for (int i = 0; i < array.Length; i++)
            {
                PropertyInfo propertyInfo = array[i];
                if (propertyInfo.CanRead && propertyInfo.CanWrite)
                {
                    if (!JsonIgnoreAttribute.IsJsonIgnore(propertyInfo))
                    {
                        string jsonName = JsonNameAttribute.GetJsonName(propertyInfo);
                        if (string.IsNullOrEmpty(jsonName))
                        {
                            dictionary[propertyInfo.Name] = propertyInfo;
                        }
                        else
                        {
                            dictionary[jsonName] = propertyInfo;
                        }
                    }
                }
            }
            FieldInfo[] fields = objectType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            FieldInfo[] array2 = fields;
            for (int j = 0; j < array2.Length; j++)
            {
                FieldInfo fieldInfo = array2[j];
                if (fieldInfo.IsPublic || fieldInfo.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length != 0)
                {
                    if (!JsonIgnoreAttribute.IsJsonIgnore(fieldInfo))
                    {
                        string jsonName2 = JsonNameAttribute.GetJsonName(fieldInfo);
                        if (string.IsNullOrEmpty(jsonName2))
                        {
                            dictionary[fieldInfo.Name] = fieldInfo;
                        }
                        else
                        {
                            dictionary[jsonName2] = fieldInfo;
                        }
                    }
                }
            }
            this.MemberMapCache[objectType] = dictionary;
            return(dictionary);
        }
Ejemplo n.º 4
0
        private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
        {
            Dictionary <string, MemberInfo> dictionary;

            if (this.MemberMapCache.TryGetValue(objectType, out dictionary))
            {
                return(dictionary);
            }
            dictionary = new Dictionary <string, MemberInfo>();
            for (Type type = objectType; type != null; type = type.BaseType)
            {
                foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    if (propertyInfo.CanRead && propertyInfo.CanWrite)
                    {
                        if (!JsonIgnoreAttribute.IsJsonIgnore(propertyInfo))
                        {
                            string jsonName = JsonNameAttribute.GetJsonName(propertyInfo);
                            if (string.IsNullOrEmpty(jsonName))
                            {
                                dictionary[propertyInfo.Name] = propertyInfo;
                            }
                            else
                            {
                                dictionary[jsonName] = propertyInfo;
                            }
                        }
                    }
                }
                FieldInfo[] fields = TypeCoercionUtility.GetTypeInfo(objectType).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (FieldInfo fieldInfo in fields)
                {
                    if (fieldInfo.IsPublic || fieldInfo.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length != 0)
                    {
                        if (!JsonIgnoreAttribute.IsJsonIgnore(fieldInfo))
                        {
                            string jsonName2 = JsonNameAttribute.GetJsonName(fieldInfo);
                            if (string.IsNullOrEmpty(jsonName2))
                            {
                                dictionary[fieldInfo.Name] = fieldInfo;
                            }
                            else
                            {
                                dictionary[jsonName2] = fieldInfo;
                            }
                        }
                    }
                }
            }
            this.MemberMapCache[objectType] = dictionary;
            return(dictionary);
        }
        internal object CoerceType(Type targetType, object value)
        {
            bool isNullable = TypeCoercionUtility.IsNullable(targetType);

            if (value == null)
            {
#if NETFX_CORE
                if (!allowNullValueTypes &&
                    targetType.IsValueType() &&
                    !isNullable)
#else
                if (!allowNullValueTypes &&
                    targetType.IsValueType &&
                    !isNullable)
#endif
                {
                    throw new JsonTypeCoercionException(String.Format(TypeCoercionUtility.ErrorNullValueType, targetType.FullName));
                }
                return(value);
            }

            if (isNullable)
            {
                // nullable types have a real underlying struct
                Type[] genericArgs = targetType.GetGenericArguments();
                if (genericArgs.Length == 1)
                {
                    targetType = genericArgs[0];
                }
            }

            Type actualType = value.GetType();
            if (targetType.IsAssignableFrom(actualType))
            {
                return(value);
            }

#if NETFX_CORE
            if (targetType.IsEnum())
#else
            if (targetType.IsEnum)
#endif
            {
                if (value is String)
                {
                    if (!Enum.IsDefined(targetType, value))
                    {
                        // if isn't a defined value perhaps it is the JsonName
                        foreach (FieldInfo field in targetType.GetFields())
                        {
                            string jsonName = JsonNameAttribute.GetJsonName(field);
                            if (((string)value).Equals(jsonName))
                            {
                                value = field.Name;
                                break;
                            }
                        }
                    }

                    return(Enum.Parse(targetType, (string)value));
                }
                else
                {
                    value = this.CoerceType(Enum.GetUnderlyingType(targetType), value);
                    return(Enum.ToObject(targetType, value));
                }
            }

            if (value is IDictionary)
            {
                Dictionary <string, MemberInfo> memberMap;
                return(this.CoerceType(targetType, (IDictionary)value, out memberMap));
            }

            if (typeof(IEnumerable).IsAssignableFrom(targetType) &&
                typeof(IEnumerable).IsAssignableFrom(actualType))
            {
                return(this.CoerceList(targetType, actualType, (IEnumerable)value));
            }

            if (value is String)
            {
                if (targetType == typeof(DateTime))
                {
                    DateTime date;
                    if (DateTime.TryParse(
                            (string)value,
                            DateTimeFormatInfo.InvariantInfo,
                            DateTimeStyles.RoundtripKind | DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault,
                            out date))
                    {
                        return(date);
                    }
                }
                else if (targetType == typeof(Guid))
                {
                    // try-catch is pointless since will throw upon generic conversion
                    return(new Guid((string)value));
                }
                else if (targetType == typeof(Char))
                {
                    if (((string)value).Length == 1)
                    {
                        return(((string)value)[0]);
                    }
                }
                else if (targetType == typeof(Uri))
                {
                    Uri uri;
                    if (Uri.TryCreate((string)value, UriKind.RelativeOrAbsolute, out uri))
                    {
                        return(uri);
                    }
                }
                else if (targetType == typeof(Version))
                {
                    // try-catch is pointless since will throw upon generic conversion
                    return(new Version((string)value));
                }
            }
            else if (targetType == typeof(TimeSpan))
            {
                return(new TimeSpan((long)this.CoerceType(typeof(Int64), value)));
            }

#if !WINDOWS_PHONE && !NETFX_CORE
            TypeConverter converter = TypeDescriptor.GetConverter(targetType);
            if (converter.CanConvertFrom(actualType))
            {
                return(converter.ConvertFrom(value));
            }

            converter = TypeDescriptor.GetConverter(actualType);
            if (converter.CanConvertTo(targetType))
            {
                return(converter.ConvertTo(value, targetType));
            }
#endif

            try
            {
                // fall back to basics
                return(Convert.ChangeType(value, targetType));
            }
            catch (Exception ex)
            {
                throw new JsonTypeCoercionException(
                          String.Format("Error converting {0} to {1}", value.GetType().FullName, targetType.FullName), ex);
            }
        }
        /** Creates a member map for the type */
        private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
        {
            Dictionary <string, MemberInfo> memberMap;

            if (this.MemberMapCache.TryGetValue(objectType, out memberMap))
            {
                // map was stored in cache
                return(memberMap);
            }

            // create a new map
            memberMap = new Dictionary <string, MemberInfo>();

            // load properties into property map
            PropertyInfo[] properties = objectType.GetProperties();

            foreach (PropertyInfo info in properties)
            {
                if (!info.CanRead || !info.CanWrite)
                {
                    continue;
                }

                if (JsonIgnoreAttribute.IsJsonIgnore(info))
                {
                    continue;
                }

                string jsonName = JsonNameAttribute.GetJsonName(info);
                if (String.IsNullOrEmpty(jsonName))
                {
                    memberMap[info.Name] = info;
                }
                else
                {
                    memberMap[jsonName] = info;
                }
            }

            // load public fields into property map
            FieldInfo[] fields = objectType.GetFields();

            foreach (FieldInfo info in fields)
            {
                if (!info.IsPublic)
                {
                    continue;
                }

                if (JsonIgnoreAttribute.IsJsonIgnore(info))
                {
                    continue;
                }

                string jsonName = JsonNameAttribute.GetJsonName(info);
                if (String.IsNullOrEmpty(jsonName))
                {
                    memberMap[info.Name] = info;
                }
                else
                {
                    memberMap[jsonName] = info;
                }
            }

            // store in cache for repeated usage
            this.MemberMapCache[objectType] = memberMap;

            return(memberMap);
        }
Ejemplo n.º 7
0
        protected virtual void WriteObject(object value, Type type, bool serializePrivate)
        {
            bool appendDelim = false;

            if (settings.HandleCyclicReferences && !type.IsValueType)
            {
                int prevIndex = 0;
                if (this.previouslySerializedObjects.TryGetValue(value, out prevIndex))
                {
                    this.Writer.Write(JsonReader.OperatorObjectStart);
                    this.WriteObjectProperty("@ref", prevIndex);
                    this.WriteLine();
                    this.Writer.Write(JsonReader.OperatorObjectEnd);
                    return;
                }
                else
                {
                    this.previouslySerializedObjects.Add(value, this.previouslySerializedObjects.Count);
                }
            }

            this.Writer.Write(JsonReader.OperatorObjectStart);

            this.depth++;
            if (this.depth > this.settings.MaxDepth)
            {
                throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, this.settings.MaxDepth));
            }
            try
            {
                if (!String.IsNullOrEmpty(this.settings.TypeHintName))
                {
                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    this.WriteObjectProperty(this.settings.TypeHintName, type.FullName + ", " + type.Assembly.GetName().Name);
                }

                bool anonymousType = type.IsGenericType && type.Name.StartsWith(JsonWriter.AnonymousTypePrefix);

                // serialize public properties
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    if (!property.CanRead)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : cannot read");
                        }
                        continue;
                    }

                    if (!property.CanWrite && !anonymousType)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : cannot write");
                        }
                        continue;
                    }

                    if (this.IsIgnored(type, property, value))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : is ignored by settings");
                        }
                        continue;
                    }

                    if (property.GetIndexParameters().Length != 0)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : is indexed");
                        }
                        continue;
                    }

                    object propertyValue = property.GetValue(value, null);
                    if (this.IsDefaultValue(property, propertyValue))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : is default value");
                        }
                        continue;
                    }

                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    // use Attributes here to control naming
                    string propertyName = JsonNameAttribute.GetJsonName(property);
                    if (String.IsNullOrEmpty(propertyName))
                    {
                        propertyName = property.Name;
                    }

                    this.WriteObjectProperty(propertyName, propertyValue);
                }

                // serialize public fields
                FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (FieldInfo field in fields)
                {
                    if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + field.Name + " : not public or is static (and does not have a JsonMember attribute)");
                        }
                        continue;
                    }

                    if (this.IsIgnored(type, field, value))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + field.Name + " : ignored by settings");
                        }
                        continue;
                    }

                    object fieldValue = field.GetValue(value);
                    if (this.IsDefaultValue(field, fieldValue))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + field.Name + " : is default value");
                        }
                        continue;
                    }

                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                        this.WriteLine();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    // use Attributes here to control naming
                    string fieldName = JsonNameAttribute.GetJsonName(field);
                    if (String.IsNullOrEmpty(fieldName))
                    {
                        fieldName = field.Name;
                    }

                    this.WriteObjectProperty(fieldName, fieldValue);
                }
            }
            finally
            {
                this.depth--;
            }

            if (appendDelim)
            {
                this.WriteLine();
            }

            this.Writer.Write(JsonReader.OperatorObjectEnd);
        }
Ejemplo n.º 8
0
        protected virtual void WriteObject(object value, Type type)
        {
            bool appendDelim = false;

            this.Writer.Write(JsonReader.OperatorObjectStart);

            this.depth++;
            if (this.depth > this.settings.MaxDepth)
            {
                throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, this.settings.MaxDepth));
            }
            try
            {
                if (!String.IsNullOrEmpty(this.settings.TypeHintName))
                {
                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    this.WriteObjectProperty(this.settings.TypeHintName, type.FullName + ", " + type.Assembly.GetName().Name);
                }

                bool anonymousType = type.IsGenericType && type.Name.StartsWith(JsonWriter.AnonymousTypePrefix);

                // serialize public properties
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    if (!property.CanRead)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : cannot read");
                        }
                        continue;
                    }

                    if (!property.CanWrite && !anonymousType)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : cannot write");
                        }
                        continue;
                    }

                    if (this.IsIgnored(type, property, value))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : is ignored by settings");
                        }
                        continue;
                    }

                    if (property.GetIndexParameters().Length != 0)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : is indexed");
                        }
                        continue;
                    }

                    object propertyValue = property.GetValue(value, null);
                    if (this.IsDefaultValue(property, propertyValue))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + property.Name + " : is default value");
                        }
                        continue;
                    }

                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    // use Attributes here to control naming
                    string propertyName = JsonNameAttribute.GetJsonName(property);
                    if (String.IsNullOrEmpty(propertyName))
                    {
                        propertyName = property.Name;
                    }

                    this.WriteObjectProperty(propertyName, propertyValue);
                }

                // serialize public fields
                FieldInfo[] fields = type.GetFields();
                foreach (FieldInfo field in fields)
                {
                    if (!field.IsPublic || field.IsStatic)
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + field.Name + " : not public or is static");
                        }
                        continue;
                    }

                    if (this.IsIgnored(type, field, value))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + field.Name + " : ignored by settings");
                        }
                        continue;
                    }

                    object fieldValue = field.GetValue(value);
                    if (this.IsDefaultValue(field, fieldValue))
                    {
                        if (Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + field.Name + " : is default value");
                        }
                        continue;
                    }

                    if (appendDelim)
                    {
                        this.WriteObjectPropertyDelim();
                        this.WriteLine();
                    }
                    else
                    {
                        appendDelim = true;
                    }

                    // use Attributes here to control naming
                    string fieldName = JsonNameAttribute.GetJsonName(field);
                    if (String.IsNullOrEmpty(fieldName))
                    {
                        fieldName = field.Name;
                    }

                    this.WriteObjectProperty(fieldName, fieldValue);
                }
            }
            finally
            {
                this.depth--;
            }

            if (appendDelim)
            {
                this.WriteLine();
            }

            this.Writer.Write(JsonReader.OperatorObjectEnd);
        }
Ejemplo n.º 9
0
        internal object CoerceType(Type targetType, object value)
        {
            bool flag = TypeCoercionUtility.IsNullable(targetType);

            if (value == null)
            {
                if (!this.allowNullValueTypes && TypeCoercionUtility.GetTypeInfo(targetType).IsValueType&& !flag)
                {
                    throw new JsonTypeCoercionException(string.Format("{0} does not accept null as a value", new object[]
                    {
                        targetType.FullName
                    }));
                }
                return(value);
            }
            else
            {
                if (flag)
                {
                    Type[] genericArguments = targetType.GetGenericArguments();
                    if (genericArguments.Length == 1)
                    {
                        targetType = genericArguments[0];
                    }
                }
                Type type = value.GetType();
                if (TypeCoercionUtility.GetTypeInfo(targetType).IsAssignableFrom(TypeCoercionUtility.GetTypeInfo(type)))
                {
                    return(value);
                }
                if (TypeCoercionUtility.GetTypeInfo(targetType).IsEnum)
                {
                    if (value is string)
                    {
                        if (!Enum.IsDefined(targetType, value))
                        {
                            foreach (FieldInfo fieldInfo in TypeCoercionUtility.GetTypeInfo(targetType).GetFields())
                            {
                                string jsonName = JsonNameAttribute.GetJsonName(fieldInfo);
                                if (((string)value).Equals(jsonName))
                                {
                                    value = fieldInfo.Name;
                                    break;
                                }
                            }
                        }
                        return(Enum.Parse(targetType, (string)value));
                    }
                    value = this.CoerceType(Enum.GetUnderlyingType(targetType), value);
                    return(Enum.ToObject(targetType, value));
                }
                else
                {
                    if (value is IDictionary)
                    {
                        Dictionary <string, MemberInfo> dictionary;
                        return(this.CoerceType(targetType, (IDictionary)value, out dictionary));
                    }
                    if (TypeCoercionUtility.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TypeCoercionUtility.GetTypeInfo(targetType)) && TypeCoercionUtility.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TypeCoercionUtility.GetTypeInfo(type)))
                    {
                        return(this.CoerceList(targetType, type, (IEnumerable)value));
                    }
                    if (value is string)
                    {
                        if (object.Equals(targetType, typeof(DateTime)))
                        {
                            DateTime dateTime;
                            if (DateTime.TryParse((string)value, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite | DateTimeStyles.AllowInnerWhite | DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.RoundtripKind, out dateTime))
                            {
                                return(dateTime);
                            }
                        }
                        else
                        {
                            if (object.Equals(targetType, typeof(Guid)))
                            {
                                return(new Guid((string)value));
                            }
                            if (object.Equals(targetType, typeof(char)))
                            {
                                if (((string)value).Length == 1)
                                {
                                    return(((string)value)[0]);
                                }
                            }
                            else if (object.Equals(targetType, typeof(Uri)))
                            {
                                Uri result;
                                if (Uri.TryCreate((string)value, UriKind.RelativeOrAbsolute, out result))
                                {
                                    return(result);
                                }
                            }
                            else if (object.Equals(targetType, typeof(Version)))
                            {
                                return(new Version((string)value));
                            }
                        }
                    }
                    else if (object.Equals(targetType, typeof(TimeSpan)))
                    {
                        return(new TimeSpan((long)this.CoerceType(typeof(long), value)));
                    }
                    TypeConverter converter = TypeDescriptor.GetConverter(targetType);
                    if (converter.CanConvertFrom(type))
                    {
                        return(converter.ConvertFrom(value));
                    }
                    converter = TypeDescriptor.GetConverter(type);
                    if (converter.CanConvertTo(targetType))
                    {
                        return(converter.ConvertTo(value, targetType));
                    }
                    object result2;
                    try
                    {
                        result2 = Convert.ChangeType(value, targetType);
                    }
                    catch (Exception innerException)
                    {
                        throw new JsonTypeCoercionException(string.Format("Error converting {0} to {1}", new object[]
                        {
                            value.GetType().FullName,
                            targetType.FullName
                        }), innerException);
                    }
                    return(result2);
                }
            }
        }
Ejemplo n.º 10
0
        internal object CoerceType(Type targetType, object value)
        {
            bool isNullable = TypeCoercionUtility.IsNullable(targetType);

            if (value == null)
            {
                if (!allowNullValueTypes &&
                    TCU.GetTypeInfo(targetType).IsValueType&&
                    !isNullable)
                {
                    throw new JsonTypeCoercionException(String.Format(TypeCoercionUtility.ErrorNullValueType, new System.Object[] { targetType.FullName }));
                }
                return(value);
            }

            if (isNullable)
            {
                // nullable types have a real underlying struct
                Type[] genericArgs = targetType.GetGenericArguments();
                if (genericArgs.Length == 1)
                {
                    targetType = genericArgs[0];
                }
            }

            Type actualType = value.GetType();

            if (TCU.GetTypeInfo(targetType).IsAssignableFrom(TCU.GetTypeInfo(actualType)))
            {
                return(value);
            }

            if (TCU.GetTypeInfo(targetType).IsEnum)
            {
                if (value is String)
                {
                    if (!Enum.IsDefined(targetType, value))
                    {
                        // if isn't a defined value perhaps it is the JsonName
                        foreach (FieldInfo field in TCU.GetTypeInfo(targetType).GetFields())
                        {
                            string jsonName = JsonNameAttribute.GetJsonName(field);
                            if (((string)value).Equals(jsonName))
                            {
                                value = field.Name;
                                break;
                            }
                        }
                    }

                    return(Enum.Parse(targetType, (string)value));
                }
                else
                {
                    value = this.CoerceType(Enum.GetUnderlyingType(targetType), value);
                    return(Enum.ToObject(targetType, value));
                }
            }

            // Value is of the wrong type and it has been deserialized as an IDictionary.
            // Previously coercion was supported
            // but this generally just caused more problems than it solved, so type hints are recommended now.
            // More specifically this can cause annoying problems with tags when a tag is referencing some object
            // for which there is no type info for (e.g the field has been removed), that data would have been
            // deserialized as an IDictionary, but when the tag gets the information it will try to coerce it
            // which will often fail horribly
            if (value is IDictionary)
            {
                return(null);
                // Dictionary<string, MemberInfo> memberMap;
                // return this.CoerceType(targetType, (IDictionary)value, out memberMap);
            }

            if (TCU.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TCU.GetTypeInfo(targetType)) &&
                TCU.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(TCU.GetTypeInfo(actualType)))
            {
                return(this.CoerceList(targetType, actualType, (IEnumerable)value));
            }

            if (value is String)
            {
                if (Type.Equals(targetType, typeof(DateTime)))
                {
                    DateTime date;
                    if (DateTime.TryParse(
                            (string)value,
                            DateTimeFormatInfo.InvariantInfo,
                            DateTimeStyles.RoundtripKind | DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault,
                            out date))
                    {
                        return(date);
                    }
                }
                else if (Type.Equals(targetType, typeof(Guid)))
                {
                    // try-catch is pointless since will throw upon generic conversion
                    return(new Guid((string)value));
                }
                else if (Type.Equals(targetType, typeof(Char)))
                {
                    if (((string)value).Length == 1)
                    {
                        return(((string)value)[0]);
                    }
                }
                else if (Equals(targetType, typeof(Uri)))
                {
                    Uri uri;
                    if (Uri.TryCreate((string)value, UriKind.RelativeOrAbsolute, out uri))
                    {
                        return(uri);
                    }
                }
                else if (Type.Equals(targetType, typeof(Version)))
                {
                    // try-catch is pointless since will throw upon generic conversion
                    return(new Version((string)value));
                }
            }
            else if (Type.Equals(targetType, typeof(TimeSpan)))
            {
                return(new TimeSpan((long)this.CoerceType(typeof(Int64), value)));
            }

#if !WINPHONE_8
            TypeConverter converter = TypeDescriptor.GetConverter(targetType);
            if (converter.CanConvertFrom(actualType))
            {
                return(converter.ConvertFrom(value));
            }

            converter = TypeDescriptor.GetConverter(actualType);
            if (converter.CanConvertTo(targetType))
            {
                return(converter.ConvertTo(value, targetType));
            }
#endif

            try
            {
                // fall back to basics
                return(Convert.ChangeType(value, targetType));
            }
            catch (Exception ex)
            {
                throw new JsonTypeCoercionException(
                          String.Format("Error converting {0} to {1}", new System.Object[] { value.GetType().FullName, targetType.FullName }), ex);
            }
        }
Ejemplo n.º 11
0
        /** Creates a member map for the type */
        private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
        {
            Dictionary <string, MemberInfo> memberMap;

            if (this.MemberMapCache.TryGetValue(objectType, out memberMap))
            {
                // map was stored in cache
                return(memberMap);
            }

            // create a new map
            memberMap = new Dictionary <string, MemberInfo>();

            // load properties into property map
            Type tp = objectType;

            while (tp != null)
            {
                PropertyInfo[] properties = TCU.GetTypeInfo(tp).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyInfo info = properties [i];
                    if (!info.CanRead || !info.CanWrite)
                    {
                        continue;
                    }

                    if (JsonIgnoreAttribute.IsJsonIgnore(info))
                    {
                        continue;
                    }

                    string jsonName = JsonNameAttribute.GetJsonName(info);
                    if (String.IsNullOrEmpty(jsonName))
                    {
                        memberMap[info.Name] = info;
                    }
                    else
                    {
                        memberMap[jsonName] = info;
                    }
                }

                // load public fields into property map
                FieldInfo[] fields = TCU.GetTypeInfo(tp).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                foreach (FieldInfo info in fields)
                {
                    if (!info.IsPublic &&
        #if WINDOWS_STORE
                        info.GetCustomAttribute <JsonMemberAttribute>(false) == null
        #else
                        info.GetCustomAttributes(typeof(JsonMemberAttribute), false).Length == 0
        #endif
                        )
                    {
                        continue;
                    }

                    if (JsonIgnoreAttribute.IsJsonIgnore(info))
                    {
                        continue;
                    }

                    string jsonName = JsonNameAttribute.GetJsonName(info);
                    if (String.IsNullOrEmpty(jsonName))
                    {
                        memberMap[info.Name] = info;
                    }
                    else
                    {
                        memberMap[jsonName] = info;
                    }

                    var formerlySerializedAs = info.GetCustomAttributes(typeof(JsonFormerlySerializedAsAttribute), true);
                    if (formerlySerializedAs.Length > 0)
                    {
                        var formerName = (formerlySerializedAs [0] as JsonFormerlySerializedAsAttribute).name;
                        if (!String.IsNullOrEmpty(formerName) && !memberMap.ContainsKey(formerName))
                        {
                            memberMap [formerName] = info;
                        }
                    }
                }

                tp = tp.BaseType;
            }

            // store in cache for repeated usage
            this.MemberMapCache[objectType] = memberMap;

            return(memberMap);
        }
Ejemplo n.º 12
0
        public void GetMemberWritingMap(Type objectType, JsonWriterSettings settings, out Member[] outMembers)
        {
            if (writingMaps == null)
            {
                writingMaps = new Dictionary <Type, Member[]> ();
            }

            if (writingMaps.TryGetValue(objectType, out outMembers))
            {
                return;
            }

            bool anonymousType = objectType.IsGenericType && objectType.Name.StartsWith(JsonWriter.AnonymousTypePrefix);

            Type tp = objectType;

            if (memberList == null)
            {
                memberList = new List <Member> ();
            }

            memberList.Clear();

            while (tp != null)
            {
                FieldInfo[] fields = tp.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int j = 0; j < fields.Length; j++)
                {
                    FieldInfo field = fields[j];

                    if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize " + field.Name + " : not public or is static (and does not have a JsonMember attribute)");
                        continue;
                    }

                    if (settings.IsIgnored(objectType, field, null))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize " + field.Name + " : ignored by settings");
                        continue;
                    }

                    // use Attributes here to control naming
                    string fieldName = JsonNameAttribute.GetJsonName(field);
                    if (String.IsNullOrEmpty(fieldName))
                    {
                        fieldName = field.Name;
                    }

                    memberList.Add(new Member(fieldName, field));
                }

                PropertyInfo[] properties = tp.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int j = 0; j < properties.Length; j++)
                {
                    PropertyInfo property = properties[j];

                    //Console.WriteLine (property.Name);
                    if (!property.CanRead)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read");
                        continue;
                    }

                    if (!property.CanWrite && !anonymousType)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write");
                        continue;
                    }

                    if (settings.IsIgnored(objectType, property, null))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings");
                        continue;
                    }

                    if (property.GetIndexParameters().Length != 0)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed");
                        continue;
                    }


                    // use Attributes here to control naming
                    string propertyName = JsonNameAttribute.GetJsonName(property);
                    if (String.IsNullOrEmpty(propertyName))
                    {
                        propertyName = property.Name;
                    }

                    memberList.Add(new Member(propertyName, property));
                }

                tp = tp.BaseType;
            }

            for (int i = 0; i < memberList.Count; i++)
            {
                memberList [i].index = i;
            }

            memberList.Sort((a, b) => {
                var attrA  = Attribute.GetCustomAttribute(a.member, typeof(JsonOrderAttribute), true) as JsonOrderAttribute;
                var attrB  = Attribute.GetCustomAttribute(b.member, typeof(JsonOrderAttribute), true) as JsonOrderAttribute;
                var orderA = attrA != null ? attrA.order : 0;
                var orderB = attrB != null ? attrB.order : 0;
                var c      = orderA.CompareTo(orderB);
                if (c != 0)
                {
                    return(c);
                }

                // Ensure stable sort
                return(a.index.CompareTo(b.index));
            });

            outMembers = memberList.ToArray();
            writingMaps[objectType] = outMembers;
        }
Ejemplo n.º 13
0
        public void GetMemberWritingMap(Type objectType, JsonWriterSettings settings, out KeyValuePair <string, FieldInfo>[] outFields, out KeyValuePair <string, PropertyInfo>[] outProps)
        {
            if (writingMaps == null)
            {
                writingMaps = new Dictionary <Type, KeyValuePair <KeyValuePair <string, FieldInfo>[], KeyValuePair <string, PropertyInfo>[]> > ();
            }

            KeyValuePair <KeyValuePair <string, FieldInfo>[], KeyValuePair <string, PropertyInfo>[]> pair;

            if (writingMaps.TryGetValue(objectType, out pair))
            {
                outFields = pair.Key;
                outProps  = pair.Value;
                return;
            }

            bool anonymousType = objectType.IsGenericType && objectType.Name.StartsWith(JsonWriter.AnonymousTypePrefix);

            Type tp = objectType;

            if (fieldList == null)
            {
                fieldList = new List <KeyValuePair <string, FieldInfo> > ();
            }

            if (propList == null)
            {
                propList = new List <KeyValuePair <string, PropertyInfo> > ();
            }

            fieldList.Clear();
            propList.Clear();

            while (tp != null)
            {
                FieldInfo[] fields = tp.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int j = 0; j < fields.Length; j++)
                {
                    FieldInfo field = fields[j];

                    if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize " + field.Name + " : not public or is static (and does not have a JsonMember attribute)");
                        continue;
                    }

                    if (settings.IsIgnored(objectType, field, null))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize " + field.Name + " : ignored by settings");
                        continue;
                    }

                    // use Attributes here to control naming
                    string fieldName = JsonNameAttribute.GetJsonName(field);
                    if (String.IsNullOrEmpty(fieldName))
                    {
                        fieldName = field.Name;
                    }

                    fieldList.Add(new KeyValuePair <string, FieldInfo> (fieldName, field));
                }

                PropertyInfo[] properties = tp.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int j = 0; j < properties.Length; j++)
                {
                    PropertyInfo property = properties[j];

                    //Console.WriteLine (property.Name);
                    if (!property.CanRead)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read");
                        continue;
                    }

                    if (!property.CanWrite && !anonymousType)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write");
                        continue;
                    }

                    if (settings.IsIgnored(objectType, property, null))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings");
                        continue;
                    }

                    if (property.GetIndexParameters().Length != 0)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed");
                        continue;
                    }


                    // use Attributes here to control naming
                    string propertyName = JsonNameAttribute.GetJsonName(property);
                    if (String.IsNullOrEmpty(propertyName))
                    {
                        propertyName = property.Name;
                    }

                    propList.Add(new KeyValuePair <string, PropertyInfo>(propertyName, property));
                }

                tp = tp.BaseType;
            }

            outFields = fieldList.ToArray();
            outProps  = propList.ToArray();

            pair = new KeyValuePair <KeyValuePair <string, FieldInfo>[], KeyValuePair <string, PropertyInfo>[]> (outFields, outProps);

            writingMaps[objectType] = pair;
        }
Ejemplo n.º 14
0
        protected virtual void WriteObject(object value, Type type, bool serializePrivate)
        {
            bool flag = false;

            if (this.settings.HandleCyclicReferences && !type.IsValueType)
            {
                int num = 0;
                if (this.previouslySerializedObjects.TryGetValue(value, out num))
                {
                    this.Writer.Write('{');
                    this.WriteObjectProperty("@ref", num);
                    this.WriteLine();
                    this.Writer.Write('}');
                    return;
                }
                this.previouslySerializedObjects.Add(value, this.previouslySerializedObjects.Count);
            }
            this.Writer.Write('{');
            this.depth++;
            if (this.depth > this.settings.MaxDepth)
            {
                throw new JsonSerializationException(string.Format("The maxiumum depth of {0} was exceeded. Check for cycles in object graph.", this.settings.MaxDepth));
            }
            try
            {
                if (!string.IsNullOrEmpty(this.settings.TypeHintName))
                {
                    if (flag)
                    {
                        this.WriteObjectPropertyDelim();
                    }
                    else
                    {
                        flag = true;
                    }
                    this.WriteObjectProperty(this.settings.TypeHintName, type.FullName + ", " + type.Assembly.GetName().Name);
                }
                bool           flag2      = type.IsGenericType && type.Name.StartsWith("<>f__AnonymousType");
                PropertyInfo[] properties = type.GetProperties();
                PropertyInfo[] array      = properties;
                for (int i = 0; i < array.Length; i++)
                {
                    PropertyInfo propertyInfo = array[i];
                    if (!propertyInfo.CanRead)
                    {
                        if (this.Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + propertyInfo.Name + " : cannot read");
                        }
                    }
                    else if (!propertyInfo.CanWrite && !flag2)
                    {
                        if (this.Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + propertyInfo.Name + " : cannot write");
                        }
                    }
                    else if (this.IsIgnored(type, propertyInfo, value))
                    {
                        if (this.Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + propertyInfo.Name + " : is ignored by settings");
                        }
                    }
                    else if (propertyInfo.GetIndexParameters().Length != 0)
                    {
                        if (this.Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + propertyInfo.Name + " : is indexed");
                        }
                    }
                    else
                    {
                        object value2 = propertyInfo.GetValue(value, null);
                        if (this.IsDefaultValue(propertyInfo, value2))
                        {
                            if (this.Settings.DebugMode)
                            {
                                Console.WriteLine("Cannot serialize " + propertyInfo.Name + " : is default value");
                            }
                        }
                        else
                        {
                            if (flag)
                            {
                                this.WriteObjectPropertyDelim();
                            }
                            else
                            {
                                flag = true;
                            }
                            string text = JsonNameAttribute.GetJsonName(propertyInfo);
                            if (string.IsNullOrEmpty(text))
                            {
                                text = propertyInfo.Name;
                            }
                            this.WriteObjectProperty(text, value2);
                        }
                    }
                }
                FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                FieldInfo[] array2 = fields;
                for (int j = 0; j < array2.Length; j++)
                {
                    FieldInfo fieldInfo = array2[j];
                    if (fieldInfo.IsStatic || (!fieldInfo.IsPublic && fieldInfo.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0))
                    {
                        if (this.Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + fieldInfo.Name + " : not public or is static (and does not have a JsonMember attribute)");
                        }
                    }
                    else if (this.IsIgnored(type, fieldInfo, value))
                    {
                        if (this.Settings.DebugMode)
                        {
                            Console.WriteLine("Cannot serialize " + fieldInfo.Name + " : ignored by settings");
                        }
                    }
                    else
                    {
                        object value3 = fieldInfo.GetValue(value);
                        if (this.IsDefaultValue(fieldInfo, value3))
                        {
                            if (this.Settings.DebugMode)
                            {
                                Console.WriteLine("Cannot serialize " + fieldInfo.Name + " : is default value");
                            }
                        }
                        else
                        {
                            if (flag)
                            {
                                this.WriteObjectPropertyDelim();
                                this.WriteLine();
                            }
                            else
                            {
                                flag = true;
                            }
                            string text2 = JsonNameAttribute.GetJsonName(fieldInfo);
                            if (string.IsNullOrEmpty(text2))
                            {
                                text2 = fieldInfo.Name;
                            }
                            this.WriteObjectProperty(text2, value3);
                        }
                    }
                }
            }
            finally
            {
                this.depth--;
            }
            if (flag)
            {
                this.WriteLine();
            }
            this.Writer.Write('}');
        }