1
This commit is contained in:
93
Pilot_KD_Parino/Common/JsonHelper.cs
Normal file
93
Pilot_KD_Parino/Common/JsonHelper.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilot_KD_Parino.Common
|
||||
{
|
||||
public class JsonHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Json序列化器
|
||||
/// </summary>
|
||||
static readonly JsonSerializer JsonSerializer = new JsonSerializer();
|
||||
|
||||
#region ToObject<T>(将Json字符串转换为对象)
|
||||
/// <summary>
|
||||
/// 将Json字符串转换为对象
|
||||
/// </summary>
|
||||
/// <param name="json">Json字符串</param>
|
||||
public static T ToObject<T>(string json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
return default(T);
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ToObject(字节数组转成对象)
|
||||
/// <summary>
|
||||
/// 反序列化对象
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
/// <returns></returns>
|
||||
public static object ToObject(byte[] value)
|
||||
{
|
||||
|
||||
using (var ms = new MemoryStream(value, writable: false))
|
||||
{
|
||||
using (var tr = new StreamReader(ms))
|
||||
{
|
||||
using (var jr = new JsonTextReader(tr))
|
||||
{
|
||||
jr.Read();
|
||||
if (jr.TokenType == JsonToken.StartArray)
|
||||
{
|
||||
// 读取类型
|
||||
var typeName = jr.ReadAsString();
|
||||
var type = Type.GetType(typeName, throwOnError: true);// 获取类型
|
||||
// 读取对象
|
||||
jr.Read();
|
||||
|
||||
if (type.Name == "String") { return jr.Value; }
|
||||
|
||||
return JsonSerializer.Deserialize(jr, type);
|
||||
}
|
||||
else if (jr.TokenType == JsonToken.StartObject)
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidDataException("JsonTranscoder 仅支持 [\"TypeName\", object]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ToJson(将对象转换为Json字符串)
|
||||
/// <summary>
|
||||
/// 将对象转换为Json字符串
|
||||
/// </summary>
|
||||
/// <param name="target">目标对象</param>
|
||||
/// <param name="isConvertToSingleQuotes">是否将双引号转成单引号</param>
|
||||
public static string ToJson(object target, bool isConvertToSingleQuotes = false)
|
||||
{
|
||||
if (target == null)
|
||||
return "{}";
|
||||
var result = JsonConvert.SerializeObject(target);
|
||||
if (isConvertToSingleQuotes)
|
||||
result = result.Replace("\"", "'");
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user