增加Json序列化

This commit is contained in:
falcon 2022-03-31 11:24:10 +08:00
parent 18d048218e
commit 96002f73f3
6 changed files with 89 additions and 16 deletions

View File

@ -0,0 +1,24 @@
namespace Falcon.SugarApi
{
/// <summary>
/// 序列化对象到字符串
/// </summary>
public interface ISerialize
{
/// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象</param>
/// <returns>序列化字符串</returns>
public string Serialize<T>(T obj);
/// <summary>
/// 反序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="str">序列化字符串</param>
/// <returns>对象实例</returns>
public T? Deserialize<T>(string str) where T : class;
}
}

View File

@ -1,5 +1,6 @@
using Falcon.SugarApi.ApiDefinistions;
using Falcon.SugarApi.DatabaseDefinitions;
using Falcon.SugarApi.JsonSerialize;
using Falcon.SugarApi.XmlSerialize;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.DependencyInjection;
@ -60,5 +61,14 @@ namespace Falcon.SugarApi
public static IServiceCollection AddXmlSerializeFactory(this IServiceCollection services) {
return services.AddSingleton<XmlSerializeFactory>(new XmlSerializeFactory());
}
/// <summary>
/// 添加Json序列化工厂JsonSerializeFactory该工厂可以创建一个IJsonSerialize实现
/// </summary>
/// <param name="services">服务集合</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddJsonSerializeFactory(this IServiceCollection services) {
return services.AddSingleton<JsonSerializeFactory>(new JsonSerializeFactory());
}
}
}

View File

@ -0,0 +1,10 @@
namespace Falcon.SugarApi.JsonSerialize
{
/// <summary>
/// Json文档序列化接口
/// </summary>
public interface IJsonSerialize : ISerialize
{
}
}

View File

@ -0,0 +1,27 @@
using System.Text.Json;
namespace Falcon.SugarApi.JsonSerialize
{
/// <summary>
/// 完成JSON序列化
/// </summary>
public class JsonSerialize : IJsonSerialize
{
/// <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);
/// <summary>
/// 序列化json对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">json对象</param>
/// <returns>json字符串</returns>
public string Serialize<T>(T obj) => JsonSerializer.Serialize(obj);
}
}

View File

@ -0,0 +1,17 @@
namespace Falcon.SugarApi.JsonSerialize
{
/// <summary>
/// Json序列化器工厂
/// </summary>
public class JsonSerializeFactory
{
/// <summary>
/// 使用默认设置创建Json序列化器
/// </summary>
/// <returns>序列化器</returns>
public IJsonSerialize CreateJsonSerialize() {
return new JsonSerialize();
}
}
}

View File

@ -3,23 +3,8 @@
/// <summary>
/// XML文档序列化接口
/// </summary>
public interface IXmlSerialize
public interface IXmlSerialize:ISerialize
{
/// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象</param>
/// <returns>序列化字符串</returns>
public string Serialize<T>(T obj);
/// <summary>
/// 反序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="str">序列化字符串</param>
/// <returns>对象实例</returns>
public T? Deserialize<T>(string str) where T : class;
}
}