/// <summary> /// Json转换List<T> /// </summary> /// <param name="json"></param> /// <param name="itemType">T的type</param> /// <returns></returns> private static object JsonToList(string json, Type itemType) { if (string.IsNullOrEmpty(json)) { return(null); } object obj = SimpleJsonTool.DeserializeObject(json); object res = JsonObjectToList(obj, itemType); return(res); }
public static object FromJson(Type type, string json) { try { object jsonObj = SimpleJsonTool.DeserializeObject(json); return(ChangeJsonDataToObjectByType(type, jsonObj)); } catch (Exception e) { Debug.LogError("Json:" + json + "\n" + e); } return(null); }
/// <summary> /// json转换为Dictionary<k,v> /// </summary> /// <param name="json"></param> /// <param name="keyType">key的type</param> /// <param name="valueType">value的type</param> /// <returns></returns> private static object JsonToDictionary(string json, Type keyType, Type valueType) { object obj = SimpleJsonTool.DeserializeObject(json); return(JsonObjectToDictionary(obj, keyType, valueType)); }
private static object ChangeJsonDataToObjectByType(Type type, object data) { object value = null; if (data == null) { return(value); } if (IsSupportBaseValueParseJson(type)) { value = SimpleJsonTool.DeserializeObject(data, type); } else if (type.IsArray) { try { value = JsonObjectToArray(data, type.GetElementType()); } catch (Exception e) { Debug.LogError("Array无法转换类型, data:" + data.GetType().FullName + " type.GetElementType(): " + type.GetElementType().FullName); Debug.LogError(e); } } else if (type.IsGenericType) { if (list_Type.Name == type.Name) { value = JsonObjectToList(data, type.GetGenericArguments()[0]); } else if (dictionary_Type.Name == type.Name) { Type[] ts = type.GetGenericArguments(); value = JsonObjectToDictionary(data, ts[0], ts[1]); } else { value = JsonObjectToClassOrStruct(data, type); } } else { if (type.IsClass || type.IsValueType) { value = JsonObjectToClassOrStruct(data, type); } } if (value == null) { return(value); } try { value = ChangeType(value, type); } catch (Exception e) { Debug.LogError("无法转换类型, type:" + type.FullName + " valueType: " + value.GetType().FullName + "\n " + e); } return(value); }
/// <summary> /// Json转换为Array /// </summary> /// <param name="json"></param> /// <param name="itemType">数组的类型T[]的T类型</param> /// <returns></returns> private static object JsonToArray(string json, Type itemType) { object obj = SimpleJsonTool.DeserializeObject(json); return(JsonObjectToArray(obj, itemType)); }
public static T DeserializeObject <T>(string json) { return((T)((object)SimpleJsonTool.DeserializeObject(json, typeof(T), null))); }
public static T DeserializeObject <T>(string json, IJsonSerializerStrategy jsonSerializerStrategy) { return((T)((object)SimpleJsonTool.DeserializeObject(json, typeof(T), jsonSerializerStrategy))); }
public static object DeserializeObject(string json, Type type) { return(SimpleJsonTool.DeserializeObject(json, type, null)); }
public static object DeserializeObject(string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy) { object obj = SimpleJsonTool.DeserializeObject(json); return((type != null && (obj == null || !ReflectionsUtils.IsAssignableFrom(obj.GetType(), type))) ? (jsonSerializerStrategy ?? SimpleJsonTool.CurrentJsonSerializerStrategy).DeserializeObject(obj, type) : obj); }