/// <summary> /// 序列化引用类型的数据(从object派生的) /// </summary> /// <param name="input"></param> /// <param name="targetType"></param> /// <param name="resolverTypeLevel"></param> /// <returns></returns> private static Dictionary <string, object> SerializeReferenceData(object input, Type targetType, int resolverTypeLevel) { Dictionary <string, object> result = new Dictionary <string, object>(); targetType = NormalizeTargetType(input, targetType); foreach (PropertyInfo pi in targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { ScriptIgnoreAttribute ignoreAttr = (ScriptIgnoreAttribute)Attribute.GetCustomAttribute(pi, typeof(ScriptIgnoreAttribute), false); if (ignoreAttr == null) { MethodInfo mi = pi.GetGetMethod(); if (mi != null && mi.GetParameters().Length <= 0) { object v = PreSerializeObject(mi.Invoke(input, null), null, resolverTypeLevel - 1);; result.Add(ClientPropertyNameAttribute.GetClientPropertyName(pi), v); } } } return(result); }
/// <summary> /// List转成json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonName"></param> /// <param name="list"></param> /// <returns></returns> public static string ToJson <T>(this IList <T> list, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) { if (list.Count > 1) { jsonName = list[0].GetType().Name; } else { jsonName = list.GetType().FullName; } } Json.Append("{\"" + jsonName + "\":["); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { T obj = Activator.CreateInstance <T>(); PropertyInfo[] pi = obj.GetType().GetProperties(); Json.Append("{"); for (int j = 0; j < pi.Length; j++) { Type type = pi[j].PropertyType; String strValue = string.Empty; #region 查看其是否Attributes bool IsIgnoreAttribute = false; #region 取出所以属性时 需要遍历 /* * // 取出所以属性 * object[] objAttribute = pi[j].GetCustomAttributes(typeof(Attribute), true); * * for (int attrU = 0; attrU < objAttribute.Length; attrU++) * { * ScriptIgnoreAttribute JosonAttribute = objAttribute[attrU] as ScriptIgnoreAttribute; * * if (JosonAttribute != null) * { * IsIgnoreAttribute = JosonValidate.NotIsNullOrEmpty(JosonAttribute.GetType().Name.ToStrings()); * } * * } */ #endregion object[] objAttributes = pi[j].GetCustomAttributes(typeof(ScriptIgnoreAttribute), true); if (objAttributes.Length > 0) { ScriptIgnoreAttribute JosonAttribute = objAttributes[0] as ScriptIgnoreAttribute; if (JosonAttribute != null) { IsIgnoreAttribute = JosonValidate.NotIsNullOrEmpty(JosonAttribute.GetType().Name.ToStrings()); } } #endregion // 忽略有IgnoreAttribute 的属性 if (!IsIgnoreAttribute) { #region 反射获取属性及值 if (type.IsGenericType) { //是否泛型 #region 泛型Nullable<> Type genericTypeDefinition = pi[j].PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable <>)) { //String value = Convert.ToString(pi[j].GetValue(list[i], null)); //pi[j].SetValue(List[0], string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(type)), null); object o = pi[j].GetValue(list[i], null); type = Nullable.GetUnderlyingType(type); strValue = Convert.ToString(o); } #endregion } else { //String value = Convert.ToString(pi[j].GetValue(list[i], null)); //pi[j].SetValue(Lists, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(type)), null); type = pi[j].GetValue(list[i], null).ToStrings().GetType(); strValue = pi[j].GetValue(list[i], null).ToStrings(); } #endregion Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(strValue, type)); if (j < pi.Length - 1) { Json.Append(","); } } } Json.Append("}"); if (i < list.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return(Json.ToString()); }
/// <summary> /// 预处理序列化对象,通过调用此函数,可对序列化进行预处理 /// </summary> /// <param name="input">要序列化的对象</param> /// <param name="type">要序列化的类型,可以是input的基类或input实现的接口</param> /// <param name="resolverTypeLevel">要输出类型信息的级别深度</param> /// <returns>处理结果</returns> /// <remarks>预处理序列化对象,通过调用此函数,可对序列化进行预处理</remarks> public static object PreSerializeObject(object input, Type type, int resolverTypeLevel) { object result = input; if (input != null) { JavaScriptConverter converter = JsonSerializerFactory.GetJavaScriptConverter(input.GetType()); if (converter != null) { result = converter.Serialize(input, JsonSerializerFactory.GetJavaScriptSerializer()); } else { if (input is XElement || input is XmlElement) { result = null; } else if (input is DateTime) { result = input.ToString(); } //当前判断条件,只适用经营指标系统,其他系统请注销此判断条件。 else if (input is double) { result = ((double)input).ToString("F7"); } else if (!(input is ValueType) && !(input is string) && !(input is IDictionary)) { IEnumerable list = input as IEnumerable; if (list != null) { ArrayList a = new ArrayList(); foreach (object o in list) { a.Add(PreSerializeObject(o, null, resolverTypeLevel - 1)); } result = a; } else { Dictionary <string, object> dict = new Dictionary <string, object>(); Type inputType = input.GetType(); if (type != null) { ExceptionHelper.FalseThrow(type.IsAssignableFrom(inputType), string.Format("{0} 没有从类型{1}继承", inputType, type)); } else { type = inputType; } foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { ScriptIgnoreAttribute ignoreAttr = (ScriptIgnoreAttribute)Attribute.GetCustomAttribute(pi, typeof(ScriptIgnoreAttribute), false); if (ignoreAttr == null) { MethodInfo mi = pi.GetGetMethod(); if (mi != null && mi.GetParameters().Length <= 0) { object v = PreSerializeObject(mi.Invoke(input, null), null, resolverTypeLevel - 1); ClientPropertyNameAttribute nameAttr = (ClientPropertyNameAttribute)Attribute.GetCustomAttribute(pi, typeof(ClientPropertyNameAttribute), false); string name = nameAttr == null ? pi.Name : nameAttr.PropertyName; dict.Add(name, v); } } } result = dict; } } } } if (resolverTypeLevel > 0 && result is Dictionary <string, object> ) { Dictionary <string, object> resultDict = result as Dictionary <string, object>; if (!resultDict.ContainsKey("__type")) { resultDict["__type"] = input.GetType().AssemblyQualifiedName; } } return(result); }