Skip to content

Commit 5b8d232

Browse files
authored
Merge pull request #247 from LSDust/main
添加json设置参数, 适配Newtonsoft Json
2 parents baae6a6 + f9609bd commit 5b8d232

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

UnityProject/Assets/TEngine/Runtime/Extension/Json/DefaultJsonHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using UnityEngine;
33

44
namespace TEngine
@@ -12,8 +12,9 @@ public class DefaultJsonHelper : Utility.Json.IJsonHelper
1212
/// 将对象序列化为 JSON 字符串。
1313
/// </summary>
1414
/// <param name="obj">要序列化的对象。</param>
15+
/// <param name="settings">序列化设置。</param>
1516
/// <returns>序列化后的 JSON 字符串。</returns>
16-
public string ToJson(object obj)
17+
public string ToJson(object obj, object settings = null)
1718
{
1819
return JsonUtility.ToJson(obj);
1920
}
@@ -23,8 +24,9 @@ public string ToJson(object obj)
2324
/// </summary>
2425
/// <typeparam name="T">对象类型。</typeparam>
2526
/// <param name="json">要反序列化的 JSON 字符串。</param>
27+
/// <param name="settings">序列化设置。</param>
2628
/// <returns>反序列化后的对象。</returns>
27-
public T ToObject<T>(string json)
29+
public T ToObject<T>(string json, object settings = null)
2830
{
2931
return JsonUtility.FromJson<T>(json);
3032
}
@@ -34,8 +36,9 @@ public T ToObject<T>(string json)
3436
/// </summary>
3537
/// <param name="objectType">对象类型。</param>
3638
/// <param name="json">要反序列化的 JSON 字符串。</param>
39+
/// <param name="settings">序列化设置。</param>
3740
/// <returns>反序列化后的对象。</returns>
38-
public object ToObject(Type objectType, string json)
41+
public object ToObject(Type objectType, string json, object settings = null)
3942
{
4043
return JsonUtility.FromJson(json, objectType);
4144
}

UnityProject/Assets/TEngine/Runtime/Extension/Json/Utility.Json.IJsonHelper.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace TEngine
44
{
@@ -15,24 +15,27 @@ public interface IJsonHelper
1515
/// 将对象序列化为 JSON 字符串。
1616
/// </summary>
1717
/// <param name="obj">要序列化的对象。</param>
18+
/// <param name="settings">序列化设置。</param>
1819
/// <returns>序列化后的 JSON 字符串。</returns>
19-
string ToJson(object obj);
20+
string ToJson(object obj, object settings = null);
2021

2122
/// <summary>
2223
/// 将 JSON 字符串反序列化为对象。
2324
/// </summary>
2425
/// <typeparam name="T">对象类型。</typeparam>
2526
/// <param name="json">要反序列化的 JSON 字符串。</param>
27+
/// <param name="settings">序列化设置。</param>
2628
/// <returns>反序列化后的对象。</returns>
27-
T ToObject<T>(string json);
29+
T ToObject<T>(string json, object settings = null);
2830

2931
/// <summary>
3032
/// 将 JSON 字符串反序列化为对象。
3133
/// </summary>
3234
/// <param name="objectType">对象类型。</param>
3335
/// <param name="json">要反序列化的 JSON 字符串。</param>
36+
/// <param name="settings">序列化设置。</param>
3437
/// <returns>反序列化后的对象。</returns>
35-
object ToObject(Type objectType, string json);
38+
object ToObject(Type objectType, string json, object settings = null);
3639
}
3740
}
3841
}

UnityProject/Assets/TEngine/Runtime/Extension/Json/Utility.Json.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace TEngine
44
{
@@ -24,8 +24,9 @@ public static void SetJsonHelper(IJsonHelper jsonHelper)
2424
/// 将对象序列化为 JSON 字符串。
2525
/// </summary>
2626
/// <param name="obj">要序列化的对象。</param>
27+
/// <param name="settings">序列化设置。</param>
2728
/// <returns>序列化后的 JSON 字符串。</returns>
28-
public static string ToJson(object obj)
29+
public static string ToJson(object obj, object settings = null)
2930
{
3031
if (_jsonHelper == null)
3132
{
@@ -34,7 +35,7 @@ public static string ToJson(object obj)
3435

3536
try
3637
{
37-
return _jsonHelper.ToJson(obj);
38+
return _jsonHelper.ToJson(obj, settings);
3839
}
3940
catch (Exception exception)
4041
{
@@ -52,8 +53,9 @@ public static string ToJson(object obj)
5253
/// </summary>
5354
/// <typeparam name="T">对象类型。</typeparam>
5455
/// <param name="json">要反序列化的 JSON 字符串。</param>
56+
/// <param name="settings">序列化设置。</param>
5557
/// <returns>反序列化后的对象。</returns>
56-
public static T ToObject<T>(string json)
58+
public static T ToObject<T>(string json, object settings = null)
5759
{
5860
if (_jsonHelper == null)
5961
{
@@ -62,7 +64,7 @@ public static T ToObject<T>(string json)
6264

6365
try
6466
{
65-
return _jsonHelper.ToObject<T>(json);
67+
return _jsonHelper.ToObject<T>(json, settings);
6668
}
6769
catch (Exception exception)
6870
{
@@ -80,8 +82,9 @@ public static T ToObject<T>(string json)
8082
/// </summary>
8183
/// <param name="objectType">对象类型。</param>
8284
/// <param name="json">要反序列化的 JSON 字符串。</param>
85+
/// <param name="settings">序列化设置。</param>
8386
/// <returns>反序列化后的对象。</returns>
84-
public static object ToObject(Type objectType, string json)
87+
public static object ToObject(Type objectType, string json, object settings = null)
8588
{
8689
if (_jsonHelper == null)
8790
{
@@ -95,7 +98,7 @@ public static object ToObject(Type objectType, string json)
9598

9699
try
97100
{
98-
return _jsonHelper.ToObject(objectType, json);
101+
return _jsonHelper.ToObject(objectType, json, settings);
99102
}
100103
catch (Exception exception)
101104
{

0 commit comments

Comments
 (0)