diff --git a/Falcon.SugarApi.Test/JsonSerializeTest.cs b/Falcon.SugarApi.Test/JsonSerializeTest.cs new file mode 100644 index 0000000..8748a6c --- /dev/null +++ b/Falcon.SugarApi.Test/JsonSerializeTest.cs @@ -0,0 +1,31 @@ +using Falcon.SugarApi.JsonSerialize; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Text.Json; + +namespace Falcon.SugarApi.Test +{ + /// + /// Json序列化测试 + /// + [TestClass] + public class JsonSerializeTest { + + /// + /// 注入测试 + /// + [TestMethod] + public void JsonSerializeDITest() { + var service = new ServiceCollection(); + service.AddSingleton(); + service.AddSingleton(new JsonSerializerOptions()); + var provider = service.BuildServiceProvider(); + var _ = provider.GetService(); + + service = new ServiceCollection(); + service.AddSingleton(); + provider = service.BuildServiceProvider(); + _ = provider.GetService(); + } + } +} diff --git a/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs b/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs index 71fbb27..8e00beb 100644 --- a/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs +++ b/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs @@ -8,13 +8,32 @@ namespace Falcon.SugarApi.JsonSerialize /// public class JsonSerialize : IJsonSerialize { + /// + /// 默认选项 + /// + public JsonSerializerOptions Options { get; set; } + + /// + /// 构造序列化器 + /// + public JsonSerialize() : this(new JsonSerializerOptions()) { } + + /// + /// 通过选项构造默认序列化器 + /// + /// + public JsonSerialize(JsonSerializerOptions options) { + Options = options; + } + /// /// 反序列化json字符串 /// /// 对象类型 /// json字符串 /// json对象 - public T? Deserialize(string str) where T : class => JsonSerializer.Deserialize(str); + public T? Deserialize(string str) where T : class + => JsonSerializer.Deserialize(str, Options); /// /// 序列化json对象 @@ -22,7 +41,8 @@ namespace Falcon.SugarApi.JsonSerialize /// 对象类型 /// json对象 /// json字符串 - public string Serialize(T obj) => JsonSerializer.Serialize(obj); + public string Serialize(T obj) + => JsonSerializer.Serialize(obj,Options); /// /// 反序列化json字符串 @@ -30,16 +50,17 @@ namespace Falcon.SugarApi.JsonSerialize /// json字符串 /// 返回类型 /// json对象 - public object? Deserialize(string str, Type returnType) => JsonSerializer.Deserialize(str, returnType); + public object? Deserialize(string str, Type returnType) + => JsonSerializer.Deserialize(str, returnType,Options); /// /// 序列化json对象 /// - /// 对象类型 /// json对象 /// 输入类型 /// json字符串 - public string Serialize(object obj, Type inputType) => JsonSerializer.Serialize(obj, inputType); + public string Serialize(object obj, Type inputType) + => JsonSerializer.Serialize(obj, inputType, Options); } } diff --git a/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs b/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs index 6b93c64..9b541fd 100644 --- a/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs +++ b/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs @@ -1,4 +1,6 @@ -namespace Falcon.SugarApi.JsonSerialize +using System.Text.Json; + +namespace Falcon.SugarApi.JsonSerialize { /// /// Json序列化器工厂 @@ -13,5 +15,13 @@ return new JsonSerialize(); } + /// + /// 使用默认设置创建Json序列化器 + /// + /// 序列化器 + public IJsonSerialize CreateJsonSerialize(JsonSerializerOptions options) { + return new JsonSerialize(options); + } + } }