From 96002f73f3a5f02abd438bc3b7e71dbb43994f0d Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Thu, 31 Mar 2022 11:24:10 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Json=E5=BA=8F=E5=88=97?=
=?UTF-8?q?=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi/ISerialize.cs | 24 +++++++++++++++++
Falcon.SugarApi/IServiceCollectionExtend.cs | 10 +++++++
.../JsonSerialize/IJsonSerialize.cs | 10 +++++++
.../JsonSerialize/JsonSerialize.cs | 27 +++++++++++++++++++
.../JsonSerialize/JsonSerializeFactory.cs | 17 ++++++++++++
Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs | 17 +-----------
6 files changed, 89 insertions(+), 16 deletions(-)
create mode 100644 Falcon.SugarApi/ISerialize.cs
create mode 100644 Falcon.SugarApi/JsonSerialize/IJsonSerialize.cs
create mode 100644 Falcon.SugarApi/JsonSerialize/JsonSerialize.cs
create mode 100644 Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs
diff --git a/Falcon.SugarApi/ISerialize.cs b/Falcon.SugarApi/ISerialize.cs
new file mode 100644
index 0000000..248e52d
--- /dev/null
+++ b/Falcon.SugarApi/ISerialize.cs
@@ -0,0 +1,24 @@
+namespace Falcon.SugarApi
+{
+ ///
+ /// 序列化对象到字符串
+ ///
+ public interface ISerialize
+ {
+ ///
+ /// 序列化对象
+ ///
+ /// 对象类型
+ /// 对象
+ /// 序列化字符串
+ public string Serialize(T obj);
+
+ ///
+ /// 反序列化对象
+ ///
+ /// 对象类型
+ /// 序列化字符串
+ /// 对象实例
+ public T? Deserialize(string str) where T : class;
+ }
+}
diff --git a/Falcon.SugarApi/IServiceCollectionExtend.cs b/Falcon.SugarApi/IServiceCollectionExtend.cs
index 20e9741..881b003 100644
--- a/Falcon.SugarApi/IServiceCollectionExtend.cs
+++ b/Falcon.SugarApi/IServiceCollectionExtend.cs
@@ -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(new XmlSerializeFactory());
}
+
+ ///
+ /// 添加Json序列化工厂JsonSerializeFactory,该工厂可以创建一个IJsonSerialize实现
+ ///
+ /// 服务集合
+ /// 服务集合
+ public static IServiceCollection AddJsonSerializeFactory(this IServiceCollection services) {
+ return services.AddSingleton(new JsonSerializeFactory());
+ }
}
}
diff --git a/Falcon.SugarApi/JsonSerialize/IJsonSerialize.cs b/Falcon.SugarApi/JsonSerialize/IJsonSerialize.cs
new file mode 100644
index 0000000..769916a
--- /dev/null
+++ b/Falcon.SugarApi/JsonSerialize/IJsonSerialize.cs
@@ -0,0 +1,10 @@
+namespace Falcon.SugarApi.JsonSerialize
+{
+ ///
+ /// Json文档序列化接口
+ ///
+ public interface IJsonSerialize : ISerialize
+ {
+ }
+
+}
diff --git a/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs b/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs
new file mode 100644
index 0000000..cc0eff1
--- /dev/null
+++ b/Falcon.SugarApi/JsonSerialize/JsonSerialize.cs
@@ -0,0 +1,27 @@
+using System.Text.Json;
+
+namespace Falcon.SugarApi.JsonSerialize
+{
+ ///
+ /// 完成JSON序列化
+ ///
+ public class JsonSerialize : IJsonSerialize
+ {
+ ///
+ /// 反序列化json字符串
+ ///
+ /// 对象类型
+ /// json字符串
+ /// json对象
+ public T? Deserialize(string str) where T : class => JsonSerializer.Deserialize(str);
+
+ ///
+ /// 序列化json对象
+ ///
+ /// 对象类型
+ /// json对象
+ /// json字符串
+ public string Serialize(T obj) => JsonSerializer.Serialize(obj);
+
+ }
+}
diff --git a/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs b/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs
new file mode 100644
index 0000000..6b93c64
--- /dev/null
+++ b/Falcon.SugarApi/JsonSerialize/JsonSerializeFactory.cs
@@ -0,0 +1,17 @@
+namespace Falcon.SugarApi.JsonSerialize
+{
+ ///
+ /// Json序列化器工厂
+ ///
+ public class JsonSerializeFactory
+ {
+ ///
+ /// 使用默认设置创建Json序列化器
+ ///
+ /// 序列化器
+ public IJsonSerialize CreateJsonSerialize() {
+ return new JsonSerialize();
+ }
+
+ }
+}
diff --git a/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs b/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs
index 93b5a9c..3d60e07 100644
--- a/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs
+++ b/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs
@@ -3,23 +3,8 @@
///
/// XML文档序列化接口
///
- public interface IXmlSerialize
+ public interface IXmlSerialize:ISerialize
{
- ///
- /// 序列化对象
- ///
- /// 对象类型
- /// 对象
- /// 序列化字符串
- public string Serialize(T obj);
-
- ///
- /// 反序列化对象
- ///
- /// 对象类型
- /// 序列化字符串
- /// 对象实例
- public T? Deserialize(string str) where T : class;
}
}