public override object JsonToObject(EasyJsonData data, Type t) { if (data.IsNullValue) { return(null); } Type ut = Nullable.GetUnderlyingType(t); EasyJsonController controller = GetController(ut); if (controller == null) { throw new EasyJsonException("Doesn't support JSON parser for Nullable " + ut.ToString()); } return(controller.JsonToObject(data, ut)); }
public static object ToObject(EasyJsonData jsonData, Type t) { EasyJsonController controller = GetMainController(); return(controller.JsonToObject(jsonData, t)); }
public static T ToObject <T>(string json) { EasyJsonController controller = GetMainController(); return((T)(controller.JsonToObject(json, typeof(T)))); }
public static T ToObject <T>(EasyJsonData jsonData) { EasyJsonController controller = GetMainController(); return((T)(controller.JsonToObject(jsonData, typeof(T)))); }
object _readObject(EasyJsonData data, Type t) { if (data.IsNullValue) { return(null); } try { // If user register a specific type controller but still invoke base JsonToObject, there // will make a loop: base -> GetController(t) -> child -> base if (_checkedData != data) { _checkedData = data; if (data.Type == EasyJsonDataType.JsonObject) { EasyJsonData typeData = data[DYNAMIC_TYPE_NAME]; if (typeData != null) { JsonSystemTypeController typeController = GetController(typeof(Type)) as JsonSystemTypeController; if (typeController == null) { throw new EasyJsonException("Can't find System.Type controller to process dynamic type"); } t = typeController.JsonToObject(typeData, typeof(Type)) as Type; } } EasyJsonController controller = GetController(t); if (controller != null) { return(controller.JsonToObject(data, t)); } } if (data.Type != EasyJsonDataType.JsonObject) { throw new EasyJsonException("Can't load JSON data for " + t.ToString()); } object inst = Activator.CreateInstance(t); JsonClassMetadata metadata = _getMetadata(t); foreach (KeyValuePair <string, FieldInfo> kv in metadata.Fields) { EasyJsonData fieldData = data[kv.Key]; if (fieldData != null) { FieldInfo field = kv.Value; field.SetValue(inst, _readObject(fieldData, field.FieldType)); } } foreach (KeyValuePair <string, PropertyInfo> kv in metadata.Properties) { EasyJsonData propertyData = data[kv.Key]; if (propertyData != null) { PropertyInfo property = kv.Value; property.SetValue(inst, _readObject(propertyData, property.PropertyType), null); } } return(inst); } finally { _checkedData = null; } }