public JSONSerializer(JSON jSON)
 {
     this.owner = jSON;
 }
Beispiel #2
0
        private object ChangeType(object value, Type conversionType)
        {
            if (conversionType == typeof(int))
            {
                string s = value as string;
                if (s == null)
                {
                    return((int)((long)value));
                }
                else
                {
                    return(CreateInteger(s, 0, s.Length));
                }
            }
            else if (conversionType == typeof(long))
            {
                string s = value as string;
                if (s == null)
                {
                    return((long)value);
                }
                else
                {
                    return(JSON.CreateLong(s, 0, s.Length));
                }
            }
            else if (conversionType == typeof(string))
            {
                return((string)value);
            }

            else if (conversionType.IsEnum)
            {
                return(CreateEnum(conversionType, value));
            }

            else if (conversionType == typeof(DateTime))
            {
                return(CreateDateTime((string)value));
            }

            else if (conversionType == typeof(DateTimeOffset))
            {
                return(CreateDateTimeOffset((string)value));
            }

            else if (Reflection.Instance.IsTypeRegistered(conversionType))
            {
                return(Reflection.Instance.CreateCustom((string)value, conversionType));
            }

            // 8-30-2014 - James Brooks - Added code for nullable types.
            if (IsNullable(conversionType))
            {
                if (value == null)
                {
                    return(value);
                }
                conversionType = UnderlyingTypeOf(conversionType);
            }

            // 8-30-2014 - James Brooks - Nullable Guid is a special case so it was moved after the "IsNullable" check.
            if (conversionType == typeof(Guid))
            {
                return(CreateGuid((string)value));
            }

            // 2016-04-02 - Enrico Padovani - proper conversion of byte[] back from string
            if (conversionType == typeof(byte[]))
            {
                return(Convert.FromBase64String((string)value));
            }

            if (conversionType == typeof(TimeSpan))
            {
                return(new TimeSpan((long)value));
            }

            return(Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture));
        }