Json序列化增加支持JsonSerializerOptions
This commit is contained in:
parent
dee4c60fee
commit
641ebc880b
31
Falcon.SugarApi.Test/JsonSerializeTest.cs
Normal file
31
Falcon.SugarApi.Test/JsonSerializeTest.cs
Normal file
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Json序列化测试
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class JsonSerializeTest {
|
||||
|
||||
/// <summary>
|
||||
/// 注入测试
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void JsonSerializeDITest() {
|
||||
var service = new ServiceCollection();
|
||||
service.AddSingleton<IJsonSerialize, Falcon.SugarApi.JsonSerialize.JsonSerialize>();
|
||||
service.AddSingleton<JsonSerializerOptions>(new JsonSerializerOptions());
|
||||
var provider = service.BuildServiceProvider();
|
||||
var _ = provider.GetService<IJsonSerialize>();
|
||||
|
||||
service = new ServiceCollection();
|
||||
service.AddSingleton<IJsonSerialize, Falcon.SugarApi.JsonSerialize.JsonSerialize>();
|
||||
provider = service.BuildServiceProvider();
|
||||
_ = provider.GetService<IJsonSerialize>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,13 +8,32 @@ namespace Falcon.SugarApi.JsonSerialize
|
|||
/// </summary>
|
||||
public class JsonSerialize : IJsonSerialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认选项
|
||||
/// </summary>
|
||||
public JsonSerializerOptions Options { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造序列化器
|
||||
/// </summary>
|
||||
public JsonSerialize() : this(new JsonSerializerOptions()) { }
|
||||
|
||||
/// <summary>
|
||||
/// 通过选项构造默认序列化器
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
public JsonSerialize(JsonSerializerOptions options) {
|
||||
Options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化json字符串
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型</typeparam>
|
||||
/// <param name="str">json字符串</param>
|
||||
/// <returns>json对象</returns>
|
||||
public T? Deserialize<T>(string str) where T : class => JsonSerializer.Deserialize<T>(str);
|
||||
public T? Deserialize<T>(string str) where T : class
|
||||
=> JsonSerializer.Deserialize<T>(str, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 序列化json对象
|
||||
|
@ -22,7 +41,8 @@ namespace Falcon.SugarApi.JsonSerialize
|
|||
/// <typeparam name="T">对象类型</typeparam>
|
||||
/// <param name="obj">json对象</param>
|
||||
/// <returns>json字符串</returns>
|
||||
public string Serialize<T>(T obj) => JsonSerializer.Serialize(obj);
|
||||
public string Serialize<T>(T obj)
|
||||
=> JsonSerializer.Serialize(obj,Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化json字符串
|
||||
|
@ -30,16 +50,17 @@ namespace Falcon.SugarApi.JsonSerialize
|
|||
/// <param name="str">json字符串</param>
|
||||
/// <param name="returnType">返回类型</param>
|
||||
/// <returns>json对象</returns>
|
||||
public object? Deserialize(string str, Type returnType) => JsonSerializer.Deserialize(str, returnType);
|
||||
public object? Deserialize(string str, Type returnType)
|
||||
=> JsonSerializer.Deserialize(str, returnType,Options);
|
||||
|
||||
/// <summary>
|
||||
/// 序列化json对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型</typeparam>
|
||||
/// <param name="obj">json对象</param>
|
||||
/// <param name="inputType">输入类型</param>
|
||||
/// <returns>json字符串</returns>
|
||||
public string Serialize(object obj, Type inputType) => JsonSerializer.Serialize(obj, inputType);
|
||||
public string Serialize(object obj, Type inputType)
|
||||
=> JsonSerializer.Serialize(obj, inputType, Options);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
namespace Falcon.SugarApi.JsonSerialize
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Falcon.SugarApi.JsonSerialize
|
||||
{
|
||||
/// <summary>
|
||||
/// Json序列化器工厂
|
||||
|
@ -13,5 +15,13 @@
|
|||
return new JsonSerialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用默认设置创建Json序列化器
|
||||
/// </summary>
|
||||
/// <returns>序列化器</returns>
|
||||
public IJsonSerialize CreateJsonSerialize(JsonSerializerOptions options) {
|
||||
return new JsonSerialize(options);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user