Beispiel #1
0
 static void ApplyProperty <T>(T instance, PropertyInfo pi, JsonLiteObjectBase value)
 {
     if (IsKnownPropertyType(pi.PropertyType))
     {
         pi.SetValue(instance, ConvertSimpleValue(value, pi.PropertyType), null);
     }
     //else if (IsEnumerableProperty(pi.PropertyType))
     //    pi.SetValue(instance, ConvertToArrayOf(value, pi.PropertyType));
     else
     {
         pi.SetValue(instance, ConvertToObject(value, pi.PropertyType), null);
     }
 }
Beispiel #2
0
        static object ConvertSimpleValue(JsonLiteObjectBase value, Type propertyType)
        {
            JsonLiteValue jsonValue = value as JsonLiteValue;

            if (jsonValue == null)
            {
                throw new JsonLiteDeserializationException();
            }
            if (propertyType == typeof(TimeSpan))
            {
                return(TimeSpan.Parse(jsonValue.Value, CultureInfo.InvariantCulture));
            }
            return(Convert.ChangeType(jsonValue.Value, propertyType, CultureInfo.InvariantCulture));
        }
Beispiel #3
0
        static void ApplyProperties(JsonLiteObjectBase instance, object result)
        {
            Type          type  = result.GetType();
            JsonLiteArray array = instance as JsonLiteArray;

            if (array != null)
            {
                ApplyPropertiesCore(array, result);
                return;
            }
            JsonLiteObject obj = instance as JsonLiteObject;

            if (obj != null)
            {
                ApplyPropertiesCore(obj, result);
                return;
            }
        }
Beispiel #4
0
        static object ConvertToObject(JsonLiteObjectBase value, Type propertyType)
        {
            JsonLiteObject jsonValue = value as JsonLiteObject;

            if (jsonValue == null)
            {
                JsonLiteValue val = value as JsonLiteValue;
                if (val != null && val.Value == "null")
                {
                    return(null);
                }

                throw new JsonLiteDeserializationException();
            }

            object instance = Activator.CreateInstance(propertyType);

            if (instance != null)
            {
                ApplyProperties(value, instance);
            }
            return(instance);
        }