using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Text; using Zhaizj.Framework.ORM; using Zhaizj.Framework.Reflection; namespace Zhaizj.Framework.Serialization { /// /// 封装了 json 反序列化中的常见操作:将 json 字符串反序列化为对象、对象列表、字典等。 /// 序列化工具见 JsonString /// public class JSON { /// /// 将字典序列化为 json 字符串 /// /// /// public static String DicToString( Dictionary dic ) { return JsonString.ConvertDictionary( dic, false ); } //----------------------------------------------------- String to obj --------------------------------------------------------------------- /// /// 将 json 字符串反序列化为对象 /// /// json 字符串 /// 目标类型 /// public static Object ToObject( String oneJsonString, Type t ) { Dictionary map = JsonParser.Parse( oneJsonString ) as Dictionary; return setValueToObject( t, map ); } /// /// 将 json 字符串反序列化为对象 /// /// /// json 字符串 /// public static T ToObject( String jsonString ) { Object result = ToObject( jsonString, typeof( T ) ); return (T)result; } /// /// 将 json 字符串反序列化为对象列表 /// /// /// json 字符串 /// 返回对象列表 public static List ToList( String jsonString ) { List list = new List(); if (strUtil.IsNullOrEmpty( jsonString )) return list; List lists = JsonParser.Parse( jsonString ) as List; foreach (Dictionary map in lists) { Object result = setValueToObject( typeof( T ), map ); list.Add( (T)result ); } return list; } internal static Object setValueToObject( Type t, Dictionary map ) { Object result = rft.GetInstance( t ); PropertyInfo[] properties = t.GetProperties( BindingFlags.Public | BindingFlags.Instance ); foreach (KeyValuePair pair in map) { String pName = pair.Key; String pValue = pair.Value.ToString(); PropertyInfo info = getPropertyInfo( properties, pName ); if ((info != null) && !info.IsDefined( typeof( NotSaveAttribute ), false )) { Object objValue; if (ReflectionUtil.IsBaseType( info.PropertyType )) { objValue = Convert.ChangeType( pValue, info.PropertyType ); } else if (info.PropertyType == typeof( Dictionary )) { objValue = pair.Value; } else if (rft.IsInterface( info.PropertyType, typeof( IList ) )) { objValue = pair.Value; } else { objValue = rft.GetInstance( info.PropertyType ); ReflectionUtil.SetPropertyValue( objValue, "Id", cvt.ToInt( pValue ) ); } ReflectionUtil.SetPropertyValue( result, pName, objValue ); } } return result; } public static IEntity ToEntity( String jsonString, Type t ) { Dictionary map = JsonParser.Parse( jsonString ) as Dictionary; return toEntityByMap( t, map, null ); } private static IEntity toEntityByMap( Type t, Dictionary map, Type parentType ) { if (map == null) return null; IEntity result = Entity.New( t.FullName ); EntityInfo ei = Entity.GetInfo( t ); foreach (KeyValuePair pair in map) { String pName = pair.Key; object pValue = pair.Value; EntityPropertyInfo p = ei.GetProperty( pName ); Object objValue = null; if (ReflectionUtil.IsBaseType( p.Type )) { objValue = Convert.ChangeType( pValue, p.Type ); } else if (p.IsList) { continue; } else if (pValue is Dictionary) { if (p.Type == parentType) continue; Dictionary dic = pValue as Dictionary; if (dic != null && dic.Count > 0) { objValue = toEntityByMap( p.Type, dic, t ); } } else continue; p.SetValue( result, objValue ); } return result; } //-------------------------------------------------------------------------------------------------------------------------- /// /// 将 json 字符串反序列化为字典对象的列表 /// /// /// public static List> ToDictionaryList( String jsonString ) { List list = JsonParser.Parse( jsonString ) as List; List> results = new List>(); foreach (Object obj in list) { Dictionary item = obj as Dictionary; results.Add( item ); } return results; } /// /// 将 json 字符串反序列化为字典对象 /// /// /// public static Dictionary ToDictionary( String oneJsonString ) { String str = trimBeginEnd( oneJsonString, "[", "]" ); if (strUtil.IsNullOrEmpty( str )) return new Dictionary(); return JsonParser.Parse( str ) as Dictionary; } private static String trimBeginEnd( String str, String beginStr, String endStr ) { str = str.Trim(); str = strUtil.TrimStart( str, beginStr ); str = strUtil.TrimEnd( str, endStr ); str = str.Trim(); return str; } private static PropertyInfo getPropertyInfo( PropertyInfo[] propertyList, String pName ) { foreach (PropertyInfo info in propertyList) { if (info.Name.Equals( pName )) { return info; } } return null; } /// /// 将引号、冒号、逗号进行编码 /// /// /// public static String Encode( String str ) { return str.Replace( "\"", """ ).Replace( ":", ":" ).Replace( ",", "," ).Replace( "'", "\\'" ); } /// /// 将引号、冒号、逗号进行解码 /// /// /// public static String Decode( String str ) { return str.Replace( """, "\"" ).Replace( ":", ":" ).Replace( ",", "," ).Replace( "\\'", "'" ); } } }