This commit is contained in:
“wanyongkang”
2020-12-28 14:55:48 +08:00
parent c2ec7392cb
commit 40a40b6d36
305 changed files with 20629 additions and 20629 deletions

View File

@@ -1,74 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Hncore.Infrastructure.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Hncore.Infrastructure.Serializer
{
public class NullToEmptyStringResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties()
.Select(p =>
{
var jp = base.CreateProperty(p, memberSerialization);
jp.ValueProvider = new NullToEmptyStringValueProvider(p);
return jp;
}).ToList();
}
}
public class NullToEmptyStringValueProvider : IValueProvider
{
PropertyInfo _MemberInfo;
public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
{
_MemberInfo = memberInfo;
}
public object GetValue(object target)
{
object result = _MemberInfo.GetValue(target);
if (_MemberInfo.PropertyType == typeof(string) && result == null)
{
result = "";
}
else if ((_MemberInfo.PropertyType == typeof(DateTime) || _MemberInfo.PropertyType == typeof(DateTime?)) &&
result != null)
{
if (result.ToString() == "0001/1/1 0:00:00" || result.ToString() == "1000/1/1 0:00:00")
{
result = DateTimeHelper.SqlMinTime;
}
DateTime time = Convert.ToDateTime(result);
if (time == DateTimeHelper.SqlMaxTime || time == DateTimeHelper.SqlMinTime || time == DateTime.MaxValue ||
time == DateTime.MinValue)
{
result = "";
}
}
else if (_MemberInfo.PropertyType.Name == "List`1" && result == null)
{
result = new List<object>();
}
else if (_MemberInfo.PropertyType.Name == "Object" && result == null)
{
result = new { };
}
return result;
}
public void SetValue(object target, object value)
{
_MemberInfo.SetValue(target, value);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Hncore.Infrastructure.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Hncore.Infrastructure.Serializer
{
public class NullToEmptyStringResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties()
.Select(p =>
{
var jp = base.CreateProperty(p, memberSerialization);
jp.ValueProvider = new NullToEmptyStringValueProvider(p);
return jp;
}).ToList();
}
}
public class NullToEmptyStringValueProvider : IValueProvider
{
PropertyInfo _MemberInfo;
public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
{
_MemberInfo = memberInfo;
}
public object GetValue(object target)
{
object result = _MemberInfo.GetValue(target);
if (_MemberInfo.PropertyType == typeof(string) && result == null)
{
result = "";
}
else if ((_MemberInfo.PropertyType == typeof(DateTime) || _MemberInfo.PropertyType == typeof(DateTime?)) &&
result != null)
{
if (result.ToString() == "0001/1/1 0:00:00" || result.ToString() == "1000/1/1 0:00:00")
{
result = DateTimeHelper.SqlMinTime;
}
DateTime time = Convert.ToDateTime(result);
if (time == DateTimeHelper.SqlMaxTime || time == DateTimeHelper.SqlMinTime || time == DateTime.MaxValue ||
time == DateTime.MinValue)
{
result = "";
}
}
else if (_MemberInfo.PropertyType.Name == "List`1" && result == null)
{
result = new List<object>();
}
else if (_MemberInfo.PropertyType.Name == "Object" && result == null)
{
result = new { };
}
return result;
}
public void SetValue(object target, object value)
{
_MemberInfo.SetValue(target, value);
}
}
}

View File

@@ -1,185 +1,185 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Hncore.Infrastructure.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Hncore.Infrastructure.Serializer
{
public static class ObjectExtension
{
#region
/// <summary>
/// 序列化位二进制
/// </summary>
/// <param name="request">要序列化的对象</param>
/// <returns>字节数组</returns>
public static byte[] SerializeBinary(this object request)
{
using (MemoryStream memStream = new MemoryStream())
{
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(memStream, request);
return memStream.GetBuffer();
}
}
#endregion
#region
/// <summary>
/// 二进制反序列化
/// </summary>
/// <param name="buf">字节数组</param>
/// <returns>得到的对象</returns>
public static T DeserializeBinary<T>(this byte[] buf) where T : class, new()
{
if (buf == null)
{
return default(T);
}
using (MemoryStream memStream = new MemoryStream(buf))
{
memStream.Position = 0;
BinaryFormatter deserializer = new BinaryFormatter();
T info = (T) deserializer.Deserialize(memStream);
memStream.Close();
return info;
}
}
#endregion
#region Json序列化
/// <summary>
/// Json序列化
/// </summary>
public static string ToJson(this object item, bool format = false)
{
using (StringWriter sw = new StringWriter())
{
JsonSerializer serializer = JsonSerializer.Create(
new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
//NullValueHandling = NullValueHandling.Ignore,
DateFormatString = "yyyy-MM-dd HH:mm:ss"
}
);
JsonWriter jsonWriter;
if (format)
{
jsonWriter = new JsonTextWriter(sw)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
}
else
{
jsonWriter = new JsonTextWriter(sw);
}
using (jsonWriter)
{
serializer.Serialize(jsonWriter, item);
}
return sw.ToString();
}
}
#endregion
#region Json反序列化
/// <summary>
/// Json反序列化
/// </summary>
public static T FromJsonTo<T>(this string jsonString)
{
try
{
if (!string.IsNullOrWhiteSpace(jsonString))
{
T t = JsonConvert.DeserializeObject<T>(jsonString);
return t;
}
else
{
return default(T);
}
}
catch (Exception ex)
{
LogHelper.Error($"Json反序列化出错", $"待反序列化的json字符串为{jsonString},错误信息:{ex}");
return default(T);
}
}
public static T FromJsonToOrDefault<T>(this string str)
{
try
{
if (string.IsNullOrEmpty(str) || str == "[]" || str == "{}")
{
return default(T);
}
else
{
return JsonConvert.DeserializeObject<T>(str);
}
}
catch (Exception ex)
{
LogHelper.Error($"Json反序列化出错", $"待反序列化的json字符串为{str},错误信息:{ex}");
return default(T);
}
}
#endregion
#region json字符串中的属性值
/// <summary>
/// 获取json字符串中的属性值
/// </summary>
/// <param name="str">json字符串</param>
/// <param name="key">"result:data:name"</param>
/// <returns></returns>
public static string JsonItemValue(this string str, string key)
{
var defaultValue = "";
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(key)) return defaultValue;
var JObject = JsonConvert.DeserializeObject(str) as JToken;
var res = GetJsonItem(JObject, key.Split(':'), 0) ?? defaultValue;
return res.ToString();
}
private static JToken GetJsonItem(JToken JToken, string[] arr, int index)
{
if (JToken == null || index == arr.Length)
{
return JToken;
}
else
{
JToken = JToken[arr[index]];
index++;
}
return GetJsonItem(JToken, arr, index);
}
#endregion
}
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Hncore.Infrastructure.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Hncore.Infrastructure.Serializer
{
public static class ObjectExtension
{
#region
/// <summary>
/// 序列化位二进制
/// </summary>
/// <param name="request">要序列化的对象</param>
/// <returns>字节数组</returns>
public static byte[] SerializeBinary(this object request)
{
using (MemoryStream memStream = new MemoryStream())
{
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(memStream, request);
return memStream.GetBuffer();
}
}
#endregion
#region
/// <summary>
/// 二进制反序列化
/// </summary>
/// <param name="buf">字节数组</param>
/// <returns>得到的对象</returns>
public static T DeserializeBinary<T>(this byte[] buf) where T : class, new()
{
if (buf == null)
{
return default(T);
}
using (MemoryStream memStream = new MemoryStream(buf))
{
memStream.Position = 0;
BinaryFormatter deserializer = new BinaryFormatter();
T info = (T) deserializer.Deserialize(memStream);
memStream.Close();
return info;
}
}
#endregion
#region Json序列化
/// <summary>
/// Json序列化
/// </summary>
public static string ToJson(this object item, bool format = false)
{
using (StringWriter sw = new StringWriter())
{
JsonSerializer serializer = JsonSerializer.Create(
new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
//NullValueHandling = NullValueHandling.Ignore,
DateFormatString = "yyyy-MM-dd HH:mm:ss"
}
);
JsonWriter jsonWriter;
if (format)
{
jsonWriter = new JsonTextWriter(sw)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
}
else
{
jsonWriter = new JsonTextWriter(sw);
}
using (jsonWriter)
{
serializer.Serialize(jsonWriter, item);
}
return sw.ToString();
}
}
#endregion
#region Json反序列化
/// <summary>
/// Json反序列化
/// </summary>
public static T FromJsonTo<T>(this string jsonString)
{
try
{
if (!string.IsNullOrWhiteSpace(jsonString))
{
T t = JsonConvert.DeserializeObject<T>(jsonString);
return t;
}
else
{
return default(T);
}
}
catch (Exception ex)
{
LogHelper.Error($"Json反序列化出错", $"待反序列化的json字符串为{jsonString},错误信息:{ex}");
return default(T);
}
}
public static T FromJsonToOrDefault<T>(this string str)
{
try
{
if (string.IsNullOrEmpty(str) || str == "[]" || str == "{}")
{
return default(T);
}
else
{
return JsonConvert.DeserializeObject<T>(str);
}
}
catch (Exception ex)
{
LogHelper.Error($"Json反序列化出错", $"待反序列化的json字符串为{str},错误信息:{ex}");
return default(T);
}
}
#endregion
#region json字符串中的属性值
/// <summary>
/// 获取json字符串中的属性值
/// </summary>
/// <param name="str">json字符串</param>
/// <param name="key">"result:data:name"</param>
/// <returns></returns>
public static string JsonItemValue(this string str, string key)
{
var defaultValue = "";
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(key)) return defaultValue;
var JObject = JsonConvert.DeserializeObject(str) as JToken;
var res = GetJsonItem(JObject, key.Split(':'), 0) ?? defaultValue;
return res.ToString();
}
private static JToken GetJsonItem(JToken JToken, string[] arr, int index)
{
if (JToken == null || index == arr.Length)
{
return JToken;
}
else
{
JToken = JToken[arr[index]];
index++;
}
return GetJsonItem(JToken, arr, index);
}
#endregion
}
}

View File

@@ -1,54 +1,54 @@
using System.IO;
using System.Xml.Serialization;
namespace Hncore.Infrastructure.Serializer
{
public class XML
{
#region C#xml数据
/// <summary>
/// 将C#数据实体转化为xml数据
/// </summary>
/// <param name="obj">要转化的数据实体</param>
/// <returns>xml格式字符串</returns>
public static string XmlSerialize<T>(T obj)
{
using (MemoryStream stream = new MemoryStream())
{
XmlSerializer xml = new XmlSerializer(typeof(T));
//序列化对象
xml.Serialize(stream, obj);
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream))
{
string str = sr.ReadToEnd();
return str;
}
}
}
#endregion
#region xml数据转化为C#
/// <summary>
/// 将xml数据转化为C#数据实体
/// </summary>
/// <param name="xml">符合xml格式的字符串</param>
/// <returns>T类型的对象</returns>
public static T XmlDeserialize<T>(string xml)
{
XmlSerializer xmldes = new XmlSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml.ToCharArray())))
{
return (T)xmldes.Deserialize(stream);
}
}
#endregion
}
using System.IO;
using System.Xml.Serialization;
namespace Hncore.Infrastructure.Serializer
{
public class XML
{
#region C#xml数据
/// <summary>
/// 将C#数据实体转化为xml数据
/// </summary>
/// <param name="obj">要转化的数据实体</param>
/// <returns>xml格式字符串</returns>
public static string XmlSerialize<T>(T obj)
{
using (MemoryStream stream = new MemoryStream())
{
XmlSerializer xml = new XmlSerializer(typeof(T));
//序列化对象
xml.Serialize(stream, obj);
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream))
{
string str = sr.ReadToEnd();
return str;
}
}
}
#endregion
#region xml数据转化为C#
/// <summary>
/// 将xml数据转化为C#数据实体
/// </summary>
/// <param name="xml">符合xml格式的字符串</param>
/// <returns>T类型的对象</returns>
public static T XmlDeserialize<T>(string xml)
{
XmlSerializer xmldes = new XmlSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml.ToCharArray())))
{
return (T)xmldes.Deserialize(stream);
}
}
#endregion
}
}