增加Json序列化
This commit is contained in:
parent
18d048218e
commit
96002f73f3
24
Falcon.SugarApi/ISerialize.cs
Normal file
24
Falcon.SugarApi/ISerialize.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using Falcon.SugarApi.ApiDefinistions;
|
using Falcon.SugarApi.ApiDefinistions;
|
||||||
using Falcon.SugarApi.DatabaseDefinitions;
|
using Falcon.SugarApi.DatabaseDefinitions;
|
||||||
|
using Falcon.SugarApi.JsonSerialize;
|
||||||
using Falcon.SugarApi.XmlSerialize;
|
using Falcon.SugarApi.XmlSerialize;
|
||||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
@ -60,5 +61,14 @@ namespace Falcon.SugarApi
|
||||||
public static IServiceCollection AddXmlSerializeFactory(this IServiceCollection services) {
|
public static IServiceCollection AddXmlSerializeFactory(this IServiceCollection services) {
|
||||||
return services.AddSingleton<XmlSerializeFactory>(new XmlSerializeFactory());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
Falcon.SugarApi/JsonSerialize/IJsonSerialize.cs
Normal file
10
Falcon.SugarApi/JsonSerialize/IJsonSerialize.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
namespace Falcon.SugarApi.JsonSerialize
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Json文档序列化接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IJsonSerialize : ISerialize
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
Falcon.SugarApi/JsonSerialize/JsonSerialize.cs
Normal file
27
Falcon.SugarApi/JsonSerialize/JsonSerialize.cs
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
17
Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs
Normal file
17
Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,23 +3,8 @@
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// XML文档序列化接口
|
/// XML文档序列化接口
|
||||||
/// </summary>
|
/// </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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user