From 3ef1ea6199cb1af12548401819a0385eefc05a50 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Tue, 24 May 2022 15:53:26 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=97=E7=AC=A6=E4=B8=B2?=
=?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC=E6=89=A9=E5=B1=95=EF=BC=8C=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0ArrayStringJson=E8=BD=AC=E6=8D=A2=E7=89=B9=E6=80=A7?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Falcon.SugarApi.Test.csproj | 2 +-
Falcon.SugarApi.Test/StringExtendTest.cs | 31 +++++++++++++
.../SugarConnectionConfig.cs | 3 +-
.../DatabaseDefinitions/SugarDbContext.cs | 2 +-
.../JsonSerialize/ArrayJsonConverter.cs | 43 +++++++++++++++++++
README.md | 1 +
6 files changed, 79 insertions(+), 3 deletions(-)
create mode 100644 Falcon.SugarApi/JsonSerialize/ArrayJsonConverter.cs
diff --git a/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj b/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj
index b188aad..c3c6189 100644
--- a/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj
+++ b/Falcon.SugarApi.Test/Falcon.SugarApi.Test.csproj
@@ -3,7 +3,7 @@
net5
enable
-
+ bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml
false
diff --git a/Falcon.SugarApi.Test/StringExtendTest.cs b/Falcon.SugarApi.Test/StringExtendTest.cs
index 22e5381..2851890 100644
--- a/Falcon.SugarApi.Test/StringExtendTest.cs
+++ b/Falcon.SugarApi.Test/StringExtendTest.cs
@@ -29,6 +29,37 @@ namespace Falcon.SugarApi.Test
strArr = str.SplitStr();
Assert.IsNotNull(strArr);
Assert.AreEqual(0, strArr.Length);
+
+ //指定分隔符
+ str = $"a,b.c;d,e。f;g";
+
+ strArr = str.SplitStr(',');
+ Assert.AreEqual(2, strArr.Length, "分割后长度应该为7");
+ Assert.AreEqual("a", strArr[0]);
+ Assert.AreEqual("b.c;d,e。f;g", strArr[1]);
+
+ strArr = str.SplitStr(',', '.');
+ Assert.AreEqual(3, strArr.Length, "分割后长度应该为7");
+ Assert.AreEqual("a", strArr[0]);
+ Assert.AreEqual("b", strArr[1]);
+ Assert.AreEqual("c;d,e。f;g", strArr[2]);
+ }
+
+ ///
+ /// 测试ToDefault
+ ///
+ [TestMethod]
+ public void TestToDefault() {
+ var str = "";
+ var r = "";
+ r = str.ToDefault("abc");
+ Assert.AreEqual("abc", r);
+ str = null;
+ r = str.ToDefault("abc");
+ Assert.AreEqual("abc", r);
+ str = "123";
+ r = str.ToDefault("abc");
+ Assert.AreEqual("123", r);
}
}
}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
index f923d20..6918e68 100644
--- a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
+++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
@@ -1,8 +1,8 @@
using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
-using System.Reflection;
using System.Linq;
+using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions
{
@@ -20,6 +20,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
///
public SugarConnectionConfig() {
this.ConfigureExternalServices ??= new ConfigureExternalServices { };
+ //设置Nullable
this.ConfigureExternalServices.EntityService += (p, c) => {
var pt = p.PropertyType;
if (pt.GetCustomAttribute() != null) {
diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs
index 99f586e..a05dfc6 100644
--- a/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs
+++ b/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs
@@ -144,7 +144,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
#endregion
- #region 初始化
+ #region 表初始化
///
/// 已经初始化的类型列表
///
diff --git a/Falcon.SugarApi/JsonSerialize/ArrayJsonConverter.cs b/Falcon.SugarApi/JsonSerialize/ArrayJsonConverter.cs
new file mode 100644
index 0000000..d3bd10c
--- /dev/null
+++ b/Falcon.SugarApi/JsonSerialize/ArrayJsonConverter.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace Falcon.SugarApi.JsonSerialize
+{
+ ///
+ /// 序列化字符串属性到数组
+ ///
+ public class ArrayJsonConverter : JsonConverter
+ {
+ ///
+ /// 字符串分隔符数组
+ ///
+ public char[]? SplitChars { get; set; } = null;
+
+ ///
+ public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
+ var str = reader.GetString().ToDefault("")!;
+ var array = JsonSerializer.Deserialize(str) ?? new string[] { };
+ return string.Join(',', array);
+ }
+ ///
+ public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) {
+ var array = this.SplitChars == null ? value.SplitStr() : value.SplitStr(this.SplitChars);
+ var arrStr = JsonSerializer.Serialize(array);
+ writer.WriteStringValue(arrStr);
+ }
+ }
+
+ ///
+ /// 标记属性,表示这个一个数组,序列化时将会被序列化为一个以特殊字符分割的字符串数组
+ ///
+ [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
+ public class ArrayStringJsonConverterAttribute : JsonConverterAttribute
+ {
+ ///
+ /// 标记属性,表示这个一个数组,序列化时将会被序列化为一个以特殊字符分割的字符串数组
+ ///
+ public ArrayStringJsonConverterAttribute() : base(typeof(ArrayJsonConverter)) { }
+ }
+
+}
diff --git a/README.md b/README.md
index 6128661..aaf9ee2 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,7 @@
### JSONлչģ [](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/JsonSerialize)
> `IServiceCollection.AddJsonSerializeFactory`עJsonл`JsonSerializeFactory`ͨùʵһJsonл
+> `ArrayStringJsonConverterAttribute`ԱַǸַлʱᱻлΪ顣
### ַչ
> `IsNullOrEmpty()``IsNotNullOrEmpty()`ַǷΪա