增加字符串默认值扩展,增加ArrayStringJson转换特性。
This commit is contained in:
parent
0064ceda3d
commit
3ef1ea6199
|
@ -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>
|
||||||
|
|
||||||
|
|
|
@ -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;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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -144,7 +144,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 初始化
|
#region 表初始化
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 已经初始化的类型列表
|
/// 已经初始化的类型列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
43
Falcon.SugarApi/JsonSerialize/ArrayJsonConverter.cs
Normal file
43
Falcon.SugarApi/JsonSerialize/ArrayJsonConverter.cs
Normal 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)) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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()`方法返回字符串是否为空。
|
||||||
|
|
Loading…
Reference in New Issue
Block a user