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()`ַǷΪա