/// <summary> 将未知对象按属性名和值转换为Json中的键值字符串写入Buffer /// </summary> /// <param name="obj">非null的位置对象</param> protected virtual void AppendOther(object obj) { Type type = obj.GetType(); ZTReflector lit = ZTReflector.Cache(type, true); UnsafeAppend('{'); var ee = lit.Properties.GetEnumerator(); bool c = false; while (ee.MoveNext()) { if (!ee.Current.CanSerialize()) { continue; } var p = ee.Current; if (c) { UnsafeAppend(','); } AppendKey(p.Name, false); AppendObject(p.GetValue(obj)); c = true; } UnsafeAppend('}'); }
/// <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); } }
private void FillObject(object obj, ZTReflector 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.Properties[key]; //得到对象属性 if (prop == null || prop.CanWrite == false) //如果属性不存在或不可写 { SkipValue(reader); //跳过Json中的值 } else { object val = ReadValue(reader, prop.MemberType); //得到值 prop.TrySetValue(obj, val); //赋值 } if (reader.SkipChar(',') == false) { return; } } } }
/// <summary> /// 用NameValue填充对象 /// </summary> /// <param name="collection"></param> /// <returns></returns> public static T FillModel <T>(IDictionary <string, string> collection) where T : class { ZTReflector reflector = ZTReflector.Cache(typeof(T), true); object model = reflector.NewObject(); FillModel(model, collection); return(model as T); }
private void FillObject(object obj, ZTReflector 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(); FillObject(obj, lit, reader); if (reader.Current != '}') { ThrowMissingCharException('}'); } } else if (reader.Current == '[') { reader.MoveNext(); var st = GenericCollection.GetList(obj.GetType()); if (st == null) { ThrowNoIList(obj.GetType()); } 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); } } } }
public static List <T> ToList <T>(string json) { if (json == null) { return(null); } var obj = new List <T>(); (new JsonParser()).FillObject(obj, ZTReflector.Cache(typeof(List <T>), true), json); return(obj); }
public static T[] ToArray <T>(string json) { if (json == null) { return(null); } var obj = new List <T>(); (new JsonParser()).FillObject(obj, ZTReflector.Cache(typeof(T[]), true), json); return(obj.ToArray()); }
public static Dictionary <string, object> ToDictionary(string json) { if (json == null) { return(null); } var obj = new Dictionary <string, object>(); (new JsonParser()).FillObject(obj, ZTReflector.Cache(typeof(Dictionary <string, object>), true), json); return(obj); }
/// <summary> 将json字符串转换为指定对象 /// </summary> public static T ToObject <T>(string json) { if (json == null) { return(default(T)); } var lit = ZTReflector.Cache(typeof(T), true); var obj = lit.NewObject(); var parser = new JsonParser(); parser.FillObject(obj, lit, json); return((T)obj); }
/// <summary> 将json字符串转换为指定对象 /// </summary> public static Object ToObject(Type type, string json) { if (type == null) { throw new ArgumentNullException("type"); } if (json == null) { return(null); } var lit = ZTReflector.Cache(type, true); var obj = lit.NewObject(); (new JsonParser()).FillObject(obj, lit, json); return(obj); }
/// <summary> /// 用namevalue填充对象 /// </summary> /// <param name="model"></param> /// <returns></returns> public static object FillModel(object model, IDictionary <string, string> collection) { if (model == null) { throw new ArgumentNullException("传递过来的填充对象为空"); } ZTReflector reflector = ZTReflector.Cache(model.GetType(), true); foreach (var item in collection) { if (reflector.Properties.ContainsKey(item.Key)) { SetValue(model, reflector.Properties[item.Key], item.Value); } } return(model); }
/// <summary> /// 对象转为字典类型 /// </summary> /// <param name="value"></param> /// <returns></returns> public static Dictionary <string, string> ObjectToDictionary(object value) { Dictionary <string, string> dic = new Dictionary <string, string>(); ZTReflector reflector = ZTReflector.Cache(value.GetType(), false); object val; string valString; foreach (var item in reflector.Properties) { val = item.GetValue(value); if (val == null) { valString = string.Empty; } else { valString = val.ToString(); } dic.Add(item.Name, valString); } return(dic); }
/// <summary> /// 复制两个对象的值 /// </summary> /// <param name="from"></param> /// <param name="to"></param> public static void CopyValue(object from, object to) { ZTReflector fromReflector = ZTReflector.Cache(from.GetType(), true); ZTReflector toReflector = ZTReflector.Cache(to.GetType(), true); foreach (var fromProperty in fromReflector.Properties) { //目标不包含此属性 if (!toReflector.Properties.ContainsKey(fromProperty.Name)) { continue; } var toProperty = toReflector.Properties[fromProperty.Name]; object val = null; if (!fromProperty.TryGetValue(from, out val)) { //无法从源中得到值 continue; } //源和目标的类型是否相同 if (fromProperty.OriginalType == toProperty.OriginalType) { toProperty.TrySetValue(to, val); } else { //类型不同 if (val != null) { SetValue(to, toProperty, val.ToString()); } } } }