private void FillObject(object obj, Literacy lit, UnsafeJsonReader reader) { if (reader.Current == '}') { return; } if (obj is IDictionary) { var st = GenericCollection.GetDict(obj.GetType()); FillDictionary((IDictionary)obj, st.KeyType, st.ElementType, reader); } else { while (true) { var key = ReadKey(reader); //获取Key var prop = lit.Property[key]; //得到对象属性 if (prop == null || prop.CanWrite == false) //如果属性不存在或不可写 { SkipValue(reader); //跳过Json中的值 } else { object val = ReadValue(reader, prop.MemberType); //得到值 prop.SetValue(obj, val); //赋值 } if (reader.SkipChar(',') == false) { return; } } } }
/// <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> 读取对象 /// </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 = ZTReflector.Cache(type, true); obj = lit.NewObject(); FillObject(obj, lit, reader); return(obj); } }