using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Reflection; using Zhaizj.Framework.Reflection; using Zhaizj.Framework.ORM; namespace Zhaizj.Framework.Serialization { /// /// json 序列化工具:将对象转换成 json 字符串 /// public class JsonString { private static Boolean getDefaultIsBreakline() { return false; } private static String empty() { return "\"\""; } /// /// 将对象转换成 json 字符串 /// /// /// public static String Convert( Object obj ) { return Convert( obj, getDefaultIsBreakline() ); } /// /// 将对象转换成 json 字符串 /// /// /// 是否换行(默认不换行,阅读起来更加清晰) /// public static String Convert( Object obj, Boolean isBreakline ) { if (obj == null) return empty(); Type t = obj.GetType(); if (t.IsArray) return ConvertArray( (object[])obj ); if (rft.IsInterface( t, typeof( IList ) )) return ConvertList( (IList)obj ); if (rft.IsInterface( t, typeof( IDictionary ) )) return ConvertDictionary( (IDictionary)obj, isBreakline ); if (t == typeof( int ) || t == typeof( decimal ) || t == typeof( double )) { return obj.ToString(); } if (t == typeof( Boolean )) return obj.ToString().ToLower(); if (t == typeof( DateTime )) return "\"" + obj.ToString() + "\""; if (t == typeof( String )) { // 转义双引号,消除换行 return "\"" + ClearNewLine( obj.ToString() ) + "\""; } return ConvertObject( obj, isBreakline ); } /// /// 清楚json字符串中的换行符 /// /// /// public static String ClearNewLine( String str ) { if (str == null) return null; return str .Replace( @"\", @"\\" ) .Replace( "\"", "\\" + "\"" ) .Replace( "\r", "" ) .Replace( "\n", "" ) .Replace( "\t", "" ); } /// /// 将对象数组转换成 json 字符串 /// /// /// public static String ConvertArray( object[] arrObj ) { if (arrObj == null) return "[]"; StringBuilder sb = new StringBuilder(); sb.Append( "[ " ); for (int i = 0; i < arrObj.Length; i++) { if (arrObj[i] == null) continue; sb.Append( Convert( arrObj[i], getDefaultIsBreakline() ) ); if (i < arrObj.Length - 1) sb.Append( ", " ); } sb.Append( " ]" ); return sb.ToString(); } /// /// 将对象列表转换成 json 字符串 /// /// /// public static String ConvertList( IList list ) { return ConvertList( list, getDefaultIsBreakline() ); } /// /// 将对象列表转换成 json 字符串 /// /// /// 是否换行(默认不换行,阅读起来更加清晰) /// public static String ConvertList( IList list, Boolean isBreakline ) { if (list == null) return "[]"; StringBuilder sb = new StringBuilder(); sb.Append( "[ " ); if (isBreakline) sb.AppendLine(); for (int i = 0; i < list.Count; i++) { if (list[i] == null) continue; sb.Append( Convert( list[i], isBreakline ) ); if (i < list.Count - 1) sb.Append( ", " ); if (isBreakline) sb.AppendLine(); } sb.Append( " ]" ); return sb.ToString(); } /// /// 将字典 Dictionary 转换成 json 字符串 /// /// /// public static String ConvertDictionary( IDictionary dic ) { return ConvertDictionary( dic, getDefaultIsBreakline() ); } /// /// 将字典 Dictionary 转换成 json 字符串 /// /// /// 是否换行(默认不换行,阅读起来更加清晰) /// public static String ConvertDictionary( IDictionary dic, Boolean isBreakline ) { if (dic == null) return empty(); StringBuilder builder = new StringBuilder(); builder.Append( "{ " ); if (isBreakline) builder.AppendLine(); foreach (DictionaryEntry pair in dic) { builder.Append( "\"" ); builder.Append( pair.Key ); builder.Append( "\":" ); builder.Append( Convert( pair.Value, isBreakline ) ); builder.Append( ", " ); if (isBreakline) builder.AppendLine(); } String result = builder.ToString().Trim().TrimEnd( ',' ); if (isBreakline) result += Environment.NewLine; return result + " }"; } /// /// 将对象转换成 json 字符串 /// /// /// public static String ConvertObject( Object obj ) { return ConvertObject( obj, getDefaultIsBreakline() ); } /// /// 将对象转换成 json 字符串 /// /// /// 是否换行(默认不换行,阅读起来更加清晰) /// public static String ConvertObject( Object obj, Boolean isBreakline ) { return ConvertObject( obj, isBreakline, true ); } /// /// 将对象转换成 json 字符串 /// /// /// 是否换行(默认不换行,阅读起来更加清晰) /// 属性名是否使用引号(默认不启用) /// public static String ConvertObject( Object obj, Boolean isBreakline, Boolean withQuotation ) { StringBuilder builder = new StringBuilder(); builder.Append( "{ " ); if (isBreakline) builder.AppendLine(); PropertyInfo[] properties = obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.Instance ); Boolean isIdFind = false; Boolean isNameFind = false; Object idValue = ""; Object nameValue = ""; List propertyList = new List(); foreach (PropertyInfo info in properties) { if (info.Name.Equals( "Id" )) { isIdFind = true; idValue = ReflectionUtil.GetPropertyValue( obj, "Id" ); } else if (info.Name.Equals( "Name" )) { isNameFind = true; nameValue = ReflectionUtil.GetPropertyValue( obj, "Name" ); } else { propertyList.Add( info ); } } if (withQuotation) { if (isIdFind) builder.AppendFormat( "\"Id\":{0}, ", idValue ); if (isNameFind) builder.AppendFormat( "\"Name\":\"{0}\", ", nameValue ); } else { if (isIdFind) builder.AppendFormat( "Id:{0}, ", idValue ); if (isNameFind) builder.AppendFormat( "Name:\"{0}\", ", nameValue ); } foreach (PropertyInfo info in propertyList) { if (info.IsDefined( typeof( NotSerializeAttribute ), false )) { continue; } Object propertyValue = ReflectionUtil.GetPropertyValue( obj, info.Name ); String jsonValue; if (info.PropertyType.IsArray) { jsonValue = ConvertArray( (object[])propertyValue ); } else if (rft.IsInterface( info.PropertyType, typeof( IList ) )) { jsonValue = ConvertList( (IList)propertyValue, isBreakline ); } else { jsonValue = Convert( propertyValue, isBreakline ); } if (withQuotation) { builder.AppendFormat( "\"{0}\":{1}", info.Name, jsonValue ); } else { builder.AppendFormat( "{0}:{1}", info.Name, jsonValue ); } builder.Append( ", " ); if (isBreakline) builder.AppendLine(); } String result = builder.ToString().Trim().TrimEnd( ',' ); if (isBreakline) result += Environment.NewLine; return result + " }"; } public static String ConvertEntity( IEntity obj ) { StringBuilder builder = new StringBuilder(); builder.Append( "{ " ); EntityInfo ei = Entity.GetInfo( obj ); List ps = ei.PropertyListAll; foreach (EntityPropertyInfo info in ps) { if (shouldPass( info )) continue; Object propertyValue = info.GetValue( obj ); String jsonValue; if (propertyValue == null) { jsonValue = empty(); } else { if (info.IsEntity) { jsonValue = ConvertEntity( propertyValue as IEntity ); } else jsonValue = Convert( propertyValue, false ); } builder.AppendFormat( "{0}:{1}", info.Name, jsonValue ); builder.Append( ", " ); } String result = builder.ToString().Trim().TrimEnd( ',' ); return result + " }"; } private static bool shouldPass( EntityPropertyInfo info ) { if (info.Type == typeof( int )) return false; if (info.Type == typeof( string )) return false; if (info.Type == typeof( decimal )) return false; if (info.Type == typeof( DateTime )) return false; if (info.Type == typeof( bool )) return false; if (info.Type == typeof( double )) return false; if (info.IsEntity) return false; return true; } } }