|
|
@@ -5,6 +5,7 @@ using System.Net;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
+using System.Xml.Serialization;
|
|
|
using YSAI.Unility.objectsComparer;
|
|
|
|
|
|
namespace YSAI.Unility
|
|
|
@@ -1162,16 +1163,97 @@ namespace YSAI.Unility
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public static string ToJson(this object obj)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 转换JSON
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj">对象</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string? ToJson(this object obj)
|
|
|
{
|
|
|
return JsonConvert.SerializeObject(obj);
|
|
|
}
|
|
|
-
|
|
|
- public static T JsonToObject<T>(this string json)
|
|
|
+ /// <summary>
|
|
|
+ /// JSON转实体
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">对象</typeparam>
|
|
|
+ /// <param name="json">Json字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static T? ToJsonEntity<T>(this string json)
|
|
|
{
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 实体对象序列化成xml字符串
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">对象</typeparam>
|
|
|
+ /// <param name="obj">对象</param>
|
|
|
+ /// <returns>XM字符串</returns>
|
|
|
+ public static string? ToXml<T>(T obj)
|
|
|
+ {
|
|
|
+ if (obj is null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ using (MemoryStream Stream = new MemoryStream())
|
|
|
+ {
|
|
|
+ XmlSerializer xml = new XmlSerializer(obj.GetType());
|
|
|
+ xml.Serialize(Stream, obj);
|
|
|
+ Stream.Position = 0;
|
|
|
+ StreamReader sr = new StreamReader(Stream);
|
|
|
+ return sr.ReadToEnd();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 反序列化xml字符为对象,默认为Utf-8编码
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">对象</typeparam>
|
|
|
+ /// <param name="xml">xml 字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static T? ToXmlEntity<T>(this string xml)
|
|
|
+ {
|
|
|
+ return ToXmlEntity<T>(xml, Encoding.UTF8);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 反序列化xml字符为对象
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">对象</typeparam>
|
|
|
+ /// <param name="xml">xml字符串</param>
|
|
|
+ /// <param name="encoding">编码格式</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static T? ToXmlEntity<T>(this string xml, Encoding encoding)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var mySerializer = new XmlSerializer(typeof(T));
|
|
|
+ using (var ms = new MemoryStream(encoding.GetBytes(xml)))
|
|
|
+ {
|
|
|
+ using (var sr = new StreamReader(ms, encoding))
|
|
|
+ {
|
|
|
+ return (T?)mySerializer.Deserialize(sr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return default(T);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 是不是为空
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="value">字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
public static bool IsEmpty(this string value)
|
|
|
{
|
|
|
return string.IsNullOrWhiteSpace(value);
|
|
|
@@ -1446,13 +1528,13 @@ namespace YSAI.Unility
|
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
|
TextReader tr = new StringReader(str);
|
|
|
JsonTextReader jtr = new JsonTextReader(tr);
|
|
|
- object obj = serializer.Deserialize(jtr);
|
|
|
+ object? obj = serializer.Deserialize(jtr);
|
|
|
if (obj != null)
|
|
|
{
|
|
|
StringWriter textWriter = new StringWriter();
|
|
|
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
|
|
|
{
|
|
|
- Formatting = Formatting.Indented,
|
|
|
+ Formatting = Newtonsoft.Json.Formatting.Indented,
|
|
|
Indentation = 4,
|
|
|
IndentChar = ' '
|
|
|
};
|