/// <summary> 读取对象 /// </summary> /// <param name="reader"></param> /// <param name="type"></param> /// <returns></returns> private object ReadObject(UnsafeJsonReader reader, Type type) { object obj; if (type.GetInterface("System.Collections.IDictionary") == typeof(IDictionary)) { var st = GenericCollection.GetDict(type); if (st.Init == null) { ThrowNoConstructor(type); } obj = st.Init(); FillDictionary((IDictionary)obj, st.KeyType, st.ElementType, reader); return(obj); } else if (type == typeof(object)) { obj = new Dictionary <string, object>(); FillDictionary((IDictionary)obj, typeof(string), typeof(object), reader); return(obj); } else { var lit = Literacy.Cache(type, true); obj = lit.NewObject(); FillObject(obj, lit, reader); return(obj); } }
public List <object> ToList(string json) { if (json == null) { return(null); } var obj = new List <object>(); FillObject(obj, Literacy.Cache(typeof(List <object>), true), json); return(obj); }
public T[] ToArray <T>(string json) { if (json == null) { return(null); } var obj = new ArrayList(); FillObject(obj, Literacy.Cache(typeof(T[]), true), json); return((T[])obj.ToArray(typeof(T))); }
public Dictionary <string, object> ToDictionary(string json) { if (json == null) { return(null); } var obj = new Dictionary <string, object>(); FillObject(obj, Literacy.Cache(typeof(Dictionary <string, object>), true), json); return(obj); }
/// <summary> 将json字符串中的数据填充到指定对象 /// </summary> public void FillObject(object obj, string json) { if (obj == null) { throw new ArgumentNullException("obj"); } if (json != null) { FillObject(obj, Literacy.Cache(obj.GetType(), true), json); } }
/// <summary> 将json字符串转换为指定对象 /// </summary> public T ToObject <T>(string json) { if (json == null) { return(default(T)); } var lit = Literacy.Cache(typeof(T), true); var obj = lit.NewObject(); FillObject(obj, lit, json); return((T)obj); }
/// <summary> 将json字符串转换为指定对象 /// </summary> public Object ToObject(Type type, string json) { if (type == null) { throw new ArgumentNullException("type"); } if (json == null) { return(null); } var lit = Literacy.Cache(type, true); var obj = lit.NewObject(); FillObject(obj, lit, json); return(obj); }
/// <summary> 将未知对象按属性名和值转换为Json中的键值字符串写入Buffer /// </summary> /// <param name="obj">非null的位置对象</param> protected override void AppendOther(object obj) { Type type = obj.GetType(); Literacy lit = Literacy.Cache(type, true); UnsafeAppend('{'); var ee = lit.Property.GetEnumerator(); if (ee.MoveNext()) { var p = ee.Current; AppendKey(p.Name, false); AppendObject(p.GetValue(obj)); while (ee.MoveNext()) { p = ee.Current; UnsafeAppend(','); AppendKey(p.Name, false); AppendObject(p.GetValue(obj)); } } UnsafeAppend('}'); }