Skip to content

Commit 0eb9991

Browse files
committed
feat: 为数组添加插入、删除等方法
1 parent ba8607a commit 0eb9991

File tree

10 files changed

+317
-86
lines changed

10 files changed

+317
-86
lines changed

.editorconfig

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ end_of_line = crlf
77
indent_style = space
88
insert_final_newline = true
99
trim_trailing_whitespace = true
10-
dotnet_style_qualification_for_field = false:silent
11-
dotnet_style_qualification_for_property = false:silent
12-
dotnet_style_qualification_for_method = false:silent
13-
dotnet_style_qualification_for_event = false:silent
14-
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
10+
indent_size = 2
11+
tab_width = 2
1512

1613
# c# 文件
1714
[*.cs]
@@ -116,6 +113,7 @@ csharp_style_prefer_readonly_struct = true
116113
# 代码块首选项
117114
csharp_prefer_braces = true:silent
118115
csharp_prefer_simple_using_statement = true:suggestion
116+
csharp_prefer_system_threading_lock = true:suggestion
119117
csharp_style_namespace_declarations = file_scoped:suggestion
120118
csharp_style_prefer_method_group_conversion = true:silent
121119
csharp_style_prefer_top_level_statements = true:silent
@@ -131,6 +129,7 @@ csharp_style_prefer_null_check_over_type_check = true
131129
csharp_style_prefer_range_operator = true
132130
csharp_style_prefer_tuple_swap = true
133131
csharp_style_prefer_utf8_string_literals = true
132+
csharp_style_prefer_primary_constructors = true:suggestion
134133
csharp_style_throw_expression = true
135134
csharp_style_unused_value_assignment_preference = discard_variable
136135
csharp_style_unused_value_expression_statement_preference = discard_variable
@@ -297,7 +296,3 @@ dotnet_naming_style.以_为前缀.required_prefix = _
297296
dotnet_naming_style.以_为前缀.required_suffix =
298297
dotnet_naming_style.以_为前缀.word_separator =
299298
dotnet_naming_style.以_为前缀.capitalization = camel_case
300-
301-
[*.csproj]
302-
indent_size = 2
303-
tab_width = 2

.github/workflows/PublishNugetPackage.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ jobs:
1111
runs-on: ubuntu-latest
1212

1313
steps:
14-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1515
- name: Setup .NET Core SDK
16-
uses: actions/setup-dotnet@v3
16+
uses: actions/setup-dotnet@v4
1717
with:
18-
dotnet-version: '7.0.x'
18+
dotnet-version: |
19+
8.x
20+
9.x
1921
- name: restore dependencies
2022
run: dotnet restore
2123
- name: build
2224
run: dotnet build --no-restore -c Release
25+
- name: test
26+
run: dotnet test -f net9.0 && dotnet test -c Release -f net9.0
2327
- name: pack
2428
run: dotnet pack -c Release -o ./output
2529
- name: push package

Cuture.Extensions.SystemTextJson.Dynamic.sln

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Solution>
2+
<Folder Name="/.github/" />
3+
<Folder Name="/.github/workflows/">
4+
<File Path=".github/workflows/PublishNugetPackage.yml" />
5+
</Folder>
6+
<Folder Name="/benchmark/">
7+
<Project Path="benchmark/Cuture.Extensions.SystemTextJson.Dynamic.Benchmark/Cuture.Extensions.SystemTextJson.Dynamic.Benchmark.csproj" />
8+
</Folder>
9+
<Folder Name="/solution items/">
10+
<File Path=".editorconfig" />
11+
<File Path="Directory.Build.props" />
12+
<File Path="global.json" />
13+
<File Path="LICENSE.txt" />
14+
<File Path="README.md" />
15+
</Folder>
16+
<Folder Name="/src/">
17+
<Project Path="src/Cuture.Extensions.SystemTextJson.Dynamic/Cuture.Extensions.SystemTextJson.Dynamic.csproj" />
18+
</Folder>
19+
<Folder Name="/test/">
20+
<Project Path="test/Cuture.Extensions.SystemTextJson.Dynamic.Test/Cuture.Extensions.SystemTextJson.Dynamic.Test.csproj" />
21+
</Folder>
22+
</Solution>

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ var c = json.a.b.c;
4444

4545
//访问数组
4646
var v2 = json.x[1];
47+
var v3 = json.x.last();
48+
var v4 = json.x.first();
49+
var v5 = json.x.push(obj);
50+
var v6 = json.x.pop();
51+
var v7 = json.x.add(obj);
52+
var v8 = json.x.append(obj);
53+
var v9 = json.x.insert(1,obj);
54+
var va = json.x.RemoveAt(1);
55+
json.x.clear();
4756

4857
//访问属性并转换为基础数据类型
4958
bool b1 = json.b;

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"msbuild-sdks": {
3-
"MSTest.Sdk": "3.5.0"
3+
"MSTest.Sdk": "3.10.0"
44
}
55
}

src/Cuture.Extensions.SystemTextJson.Dynamic/Cuture.Extensions.SystemTextJson.Dynamic.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
55

66
<IsPackable>true</IsPackable>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -36,7 +36,7 @@
3636

3737
<Authors>Stratos</Authors>
3838

39-
<Version>1.0.3</Version>
39+
<Version>1.1.0</Version>
4040

4141
<PackageLicenseExpression>MIT</PackageLicenseExpression>
4242
<PackageProjectUrl>https://github.com/cuture/Cuture.Extensions.SystemTextJson.Dynamic</PackageProjectUrl>

src/Cuture.Extensions.SystemTextJson.Dynamic/JsonArrayDynamicAccessor.cs

Lines changed: 122 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,111 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result)
124124
return base.TryGetMember(binder, out result);
125125
}
126126

127+
public override bool TryInvokeMember(InvokeMemberBinder binder, object?[]? args, out object? result)
128+
{
129+
switch (binder.Name.ToLowerInvariant())
130+
{
131+
case "insert":
132+
{
133+
if (args?.Length != 2
134+
|| args[0] is not int index)
135+
{
136+
throw new InvalidOperationException($"Method \"{binder.Name}\" for array must has 2 argument with 'index' and 'value'.");
137+
}
138+
139+
if (index < _jsonArray.Count)
140+
{
141+
var node = JsonNode.Parse(JSON.stringify(args[1]));
142+
_jsonArray.Insert(index, node);
143+
144+
result = JsonNodeUtil.GetNodeAccessValue(node);
145+
}
146+
else
147+
{
148+
result = SetValueAtIndex(index, args[1]);
149+
}
150+
151+
return true;
152+
}
153+
154+
case "append":
155+
case "add":
156+
case "push":
157+
{
158+
if (args?.Length > 0 != true)
159+
{
160+
throw new InvalidOperationException($"Method \"{binder.Name}\" for array must has argument.");
161+
}
162+
163+
result = null;
164+
165+
foreach (var item in args)
166+
{
167+
result = SetValueAtIndex(_jsonArray.Count, item);
168+
}
169+
return true;
170+
}
171+
172+
case "pop":
173+
{
174+
var index = _jsonArray.Count - 1;
175+
if (index >= 0)
176+
{
177+
result = JsonNodeUtil.GetNodeAccessValue(_jsonArray[index]);
178+
_jsonArray.RemoveAt(index);
179+
}
180+
else
181+
{
182+
result = null;
183+
}
184+
return true;
185+
}
186+
187+
case "removeat":
188+
{
189+
if (args?.Length != 1
190+
|| args[0] is not int index)
191+
{
192+
throw new InvalidOperationException($"Method \"{binder.Name}\" for array must has 1 argument with 'index'.");
193+
}
194+
var node = _jsonArray[index];
195+
_jsonArray.RemoveAt(index);
196+
result = JsonNodeUtil.GetNodeAccessValue(node);
197+
return true;
198+
}
199+
200+
case "clear":
201+
case "removeall":
202+
{
203+
while (_jsonArray.Count > 0)
204+
{
205+
_jsonArray.RemoveAt(0);
206+
}
207+
result = null;
208+
return true;
209+
}
210+
211+
case "first":
212+
case "firstordefault":
213+
{
214+
result = _jsonArray.Count > 0
215+
? JsonNodeUtil.GetNodeAccessValue(_jsonArray[0])
216+
: null;
217+
return true;
218+
}
219+
220+
case "last":
221+
case "lastordefault":
222+
{
223+
result = _jsonArray.Count > 0
224+
? JsonNodeUtil.GetNodeAccessValue(_jsonArray[_jsonArray.Count - 1])
225+
: null;
226+
return true;
227+
}
228+
}
229+
return base.TryInvokeMember(binder, args, out result);
230+
}
231+
127232
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object? value)
128233
{
129234
if (indexes.Length > 1)
@@ -135,11 +240,7 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
135240

136241
if (index is int intIndex)
137242
{
138-
while (intIndex >= _jsonArray.Count)
139-
{
140-
_jsonArray.Add(null);
141-
}
142-
_jsonArray[intIndex] = JsonNode.Parse(JSON.stringify(value));
243+
SetValueAtIndex(intIndex, value);
143244
return true;
144245
}
145246
#if NET6_0_OR_GREATER
@@ -170,6 +271,22 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
170271

171272
#endregion Public 方法
172273

274+
#region Private 方法
275+
276+
private object? SetValueAtIndex(int index, object? value)
277+
{
278+
while (index >= _jsonArray.Count)
279+
{
280+
_jsonArray.Add(null);
281+
}
282+
var node = JsonNode.Parse(JSON.stringify(value));
283+
_jsonArray[index] = node;
284+
285+
return JsonNodeUtil.GetNodeAccessValue(node);
286+
}
287+
288+
#endregion Private 方法
289+
173290
#region Private 类
174291

175292
private class JsonArrayEnumerator : IEnumerator

test/Cuture.Extensions.SystemTextJson.Dynamic.Test/Cuture.Extensions.SystemTextJson.Dynamic.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="MSTest.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks>
5-
</PropertyGroup>
4+
<TargetFrameworks>net472;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
5+
</PropertyGroup>
66

77
<ItemGroup>
88
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />

0 commit comments

Comments
 (0)