忽略dll文件git
This commit is contained in:
@@ -1,185 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user