/// <summary> 读取对象 /// </summary> /// <param name="reader"></param> /// <param name="type"></param> /// <returns></returns> private object ReadObject(UnsafeJsonReader reader, Type type) { object obj; if (type == typeof(object)) { obj = new Dictionary <string, object>(); FillDictionary((IDictionary)obj, typeof(string), typeof(object), reader); return(obj); } if (typeof(IDictionary).IsAssignableFrom(type)) { var st = GenericCollection.GetDict(type); if (st.Init == null) { ThrowNoConstructor(type); } obj = st.Init(); FillDictionary((IDictionary)obj, st.KeyType, st.ElementType, reader); return(obj); } var lit = Literacy.Cache(type, true); obj = lit.NewObject(); FillObject(obj, lit, reader); return(obj); }
/// <summary> 将json字符串转换为指定对象 /// </summary> public Object ToObject(Type type, string json) { if (json == null) { return(null); } if (type == null) { object obj = null; FillObject(ref obj, null, json); return(obj); } if (type.IsArray) { object arr = new ArrayList(); var eleType = type.GetElementType(); FillObject(ref arr, Literacy.Cache(eleType, true), json); return(((ArrayList)arr).ToArray(eleType)); } else { var lit = Literacy.Cache(type, true); var obj = lit.NewObject(); FillObject(ref obj, lit, json); return(obj); } }
/// <summary> 将json字符串中的数据填充到指定对象 /// </summary> public void FillObject(object obj, string json) { if (obj == null) { throw new ArgumentNullException("obj"); } if (json != null) { FillObject(ref obj, Literacy.Cache(obj.GetType(), true), json); } }
/// <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(); var fix = ""; while (ee.MoveNext()) { var p = ee.Current; var value = p.GetValue(obj); if (value != null) { UnsafeAppend(fix); AppendKey(p.Name, false); AppendObject(value); fix = ","; } } UnsafeAppend('}'); }
private void FillObject(ref object obj, Literacy lit, string json) { if (json == null || json.Length == 0) { return; } unsafe { fixed(char *p = json) { UnsafeJsonReader reader = new UnsafeJsonReader(p, json.Length); if (reader.IsEnd()) { return; } if (reader.Current == '{') { reader.MoveNext(); if (obj == null) { obj = new Dictionary <string, object>(); lit = Literacy.Cache(typeof(Dictionary <string, object>), true); } FillObject(obj, lit, reader); if (reader.Current != '}') { ThrowMissingCharException('}'); } } else if (reader.Current == '[') { reader.MoveNext(); if (obj == null) { obj = new ArrayList(); FillList((IList)obj, typeof(object), reader); } else if (obj is ArrayList) { FillList((IList)obj, lit.Type, reader); } else { var st = GenericCollection.GetList(lit.Type); if (st == null) { ThrowNoIList(lit.Type); } FillList((IList)obj, st.ElementType, reader); } if (reader.Current != ']') { ThrowMissingCharException(']'); } } else { ThrowException("起始字符:" + reader.Current); } reader.MoveNext(); if (reader.IsEnd()) { reader.Dispose(); } else { ThrowException("错误的结束字符:" + reader.Current); } } } }