mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
增加可选的JsonHelper - NewtonsoftJsonHelper
增加可选的JsonHelper - NewtonsoftJsonHelper
This commit is contained in:
37
Assets/TEngine/Runtime/Utility/NewtonsoftJsonHelper.cs
Normal file
37
Assets/TEngine/Runtime/Utility/NewtonsoftJsonHelper.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace TEngine
|
||||
{
|
||||
public class NewtonsoftJsonHelper : Utility.Json.IJsonHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 将对象序列化为 JSON 字符串。
|
||||
/// </summary>
|
||||
/// <param name="obj">要序列化的对象。</param>
|
||||
/// <returns>序列化后的 JSON 字符串。</returns>
|
||||
public string ToJson(object obj)
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 JSON 字符串反序列化为对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型。</typeparam>
|
||||
/// <param name="json">要反序列化的 JSON 字符串。</param>
|
||||
/// <returns>反序列化后的对象。</returns>
|
||||
public T ToObject<T>(string json)
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 JSON 字符串反序列化为对象。
|
||||
/// </summary>
|
||||
/// <param name="objectType">对象类型。</param>
|
||||
/// <param name="json">要反序列化的 JSON 字符串。</param>
|
||||
/// <returns>反序列化后的对象。</returns>
|
||||
public object ToObject(System.Type objectType, string json)
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject(json, objectType);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38484765ede44acc840c664a828ca6ff
|
||||
timeCreated: 1687360419
|
Reference in New Issue
Block a user