mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-07-02 14:48:16 +08:00
feat: ✨ 新增string扩展函数 IsJsonString (#19)
Co-authored-by: tk <fiyne1a@dingtalk.com>
This commit is contained in:
19
src/backend/NSExt.Tests/AllTests.cs
Normal file
19
src/backend/NSExt.Tests/AllTests.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using NSExt.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace NSExt;
|
||||
|
||||
/// <summary>
|
||||
/// 所有测试用例
|
||||
/// </summary>
|
||||
public class AllTests
|
||||
{
|
||||
/// <summary>
|
||||
/// IsJsonString
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void IsJsonString()
|
||||
{
|
||||
Assert.True("{\"a\":\"b\"}".IsJsonString());
|
||||
}
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xunit" Version="2.7.0"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/>
|
||||
<PackageReference Include="xunit" Version="2.8.1"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.1">
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
@ -358,6 +358,26 @@ public static class StringExtensions
|
||||
return lastEqualSignPos == me.Length - 1 && me[firstEqualSignPos..lastEqualSignPos].All(x => x == '=');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否json字符串
|
||||
/// </summary>
|
||||
/// <param name="me">me</param>
|
||||
public static bool IsJsonString(this string me)
|
||||
{
|
||||
if (me.NullOrEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
_ = JsonDocument.Parse(me);
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 中文姓名打马赛克
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user