增加字符串默认值扩展,增加ArrayStringJson转换特性。

This commit is contained in:
falcon 2022-05-24 15:53:26 +08:00
parent 0064ceda3d
commit 3ef1ea6199
6 changed files with 79 additions and 3 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net5</TargetFrameworks> <TargetFrameworks>net5</TargetFrameworks>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>

View File

@ -29,6 +29,37 @@ namespace Falcon.SugarApi.Test
strArr = str.SplitStr(); strArr = str.SplitStr();
Assert.IsNotNull(strArr); Assert.IsNotNull(strArr);
Assert.AreEqual(0, strArr.Length); Assert.AreEqual(0, strArr.Length);
//指定分隔符
str = $"a,b.c;de。fg";
strArr = str.SplitStr(',');
Assert.AreEqual(2, strArr.Length, "分割后长度应该为7");
Assert.AreEqual("a", strArr[0]);
Assert.AreEqual("b.c;de。fg", strArr[1]);
strArr = str.SplitStr(',', '.');
Assert.AreEqual(3, strArr.Length, "分割后长度应该为7");
Assert.AreEqual("a", strArr[0]);
Assert.AreEqual("b", strArr[1]);
Assert.AreEqual("c;de。fg", strArr[2]);
}
/// <summary>
/// 测试ToDefault
/// </summary>
[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);
} }
} }
} }

View File

@ -1,8 +1,8 @@
using SqlSugar; using SqlSugar;
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Linq; using System.Linq;
using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions namespace Falcon.SugarApi.DatabaseDefinitions
{ {
@ -20,6 +20,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary> /// </summary>
public SugarConnectionConfig() { public SugarConnectionConfig() {
this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices ??= new ConfigureExternalServices { };
//设置Nullable
this.ConfigureExternalServices.EntityService += (p, c) => { this.ConfigureExternalServices.EntityService += (p, c) => {
var pt = p.PropertyType; var pt = p.PropertyType;
if (pt.GetCustomAttribute<RequiredAttribute>() != null) { if (pt.GetCustomAttribute<RequiredAttribute>() != null) {

View File

@ -144,7 +144,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
#endregion #endregion
#region #region
/// <summary> /// <summary>
/// 已经初始化的类型列表 /// 已经初始化的类型列表
/// </summary> /// </summary>

View File

@ -0,0 +1,43 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Falcon.SugarApi.JsonSerialize
{
/// <summary>
/// 序列化字符串属性到数组
/// </summary>
public class ArrayJsonConverter : JsonConverter<string>
{
/// <summary>
/// 字符串分隔符数组
/// </summary>
public char[]? SplitChars { get; set; } = null;
/// <inheritdoc/>
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
var str = reader.GetString().ToDefault("")!;
var array = JsonSerializer.Deserialize<string[]>(str) ?? new string[] { };
return string.Join(',', array);
}
/// <inheritdoc/>
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);
}
}
/// <summary>
/// 标记属性,表示这个一个数组,序列化时将会被序列化为一个以特殊字符分割的字符串数组
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class ArrayStringJsonConverterAttribute : JsonConverterAttribute
{
/// <summary>
/// 标记属性,表示这个一个数组,序列化时将会被序列化为一个以特殊字符分割的字符串数组
/// </summary>
public ArrayStringJsonConverterAttribute() : base(typeof(ArrayJsonConverter)) { }
}
}

View File

@ -29,6 +29,7 @@
### JSON序列化扩展模块 [进入](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/JsonSerialize) ### JSON序列化扩展模块 [进入](/Falcon/Falcon.SugarApi/src/branch/master/Falcon.SugarApi/JsonSerialize)
> `IServiceCollection.AddJsonSerializeFactory`方法注册Json序列化工厂`JsonSerializeFactory`可以通过该工厂实例化一个Json序列化器。 > `IServiceCollection.AddJsonSerializeFactory`方法注册Json序列化工厂`JsonSerializeFactory`可以通过该工厂实例化一个Json序列化器。
> `ArrayStringJsonConverterAttribute`特性标记字符串是个数组字符串,当进行序列化时候会被序列化为数组。
### 字符串扩展方法 ### 字符串扩展方法
> `IsNullOrEmpty()`和`IsNotNullOrEmpty()`方法返回字符串是否为空。 > `IsNullOrEmpty()`和`IsNotNullOrEmpty()`方法返回字符串是否为空。