Skip to content

Commit 39c6880

Browse files
committed
feat: 添加JSON.isArray方法,用于检查对象是否为数组;
fix: 修正IDynamicKeyValueEnumerable不能正确修改原始对象的问题
1 parent 30f0c9e commit 39c6880

6 files changed

Lines changed: 131 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
<Authors>Stratos</Authors>
4141

42-
<Version>1.1.2</Version>
42+
<Version>1.1.3</Version>
4343

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ static JSON()
102102
return new JsonObjectDynamicAccessor(jsonNode);
103103
}
104104

105+
/// <summary>
106+
/// 判断<paramref name="value"/>是否为数组
107+
/// </summary>
108+
/// <param name="value"></param>
109+
/// <returns></returns>
110+
public static bool isArray(object? value) => value is Array || value is JsonArrayDynamicAccessor || value is JsonArray;
111+
105112
/// <inheritdoc cref="Undefined.IsUndefined(in object?)"/>
106113
public static bool isUndefined(object? value) => System.Text.Json.Dynamic.Undefined.IsUndefined(value);
107114

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override bool TrySetMember(SetMemberBinder binder, object? value)
7878
{
7979
foreach (var item in _jsonObject)
8080
{
81-
yield return new(item.Key, JSON.create(item.Value));
81+
yield return new(item.Key, JSON.dynamic(item.Value));
8282
}
8383
}
8484

test/Cuture.Extensions.SystemTextJson.Dynamic.Test/DynamicEnumerableTest.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,60 @@ static void Check(object origin, string key, dynamic value)
157157
}
158158
}
159159

160+
[TestMethod]
161+
public void ShouldModifyPropertySuccess()
162+
{
163+
DynamicJSONTestClass.GetTestValue(out var origin, out var json);
164+
DynamicJSONTestClass.GetTestValue(out var origin2, out var json2);
165+
166+
//IDynamicEnumerable
167+
{
168+
var originEnumerable = ((IDynamicKeyValueEnumerable)json2.MyProperty8).AsEnumerable();
169+
170+
foreach (var item in ((IDynamicKeyValueEnumerable)json.MyProperty8).AsEnumerable())
171+
{
172+
item.Value.Value1 = item.Value.Value1 + 1;
173+
}
174+
175+
foreach (var item in ((IDynamicKeyValueEnumerable)json.MyProperty8).AsEnumerable())
176+
{
177+
Check(origin2.MyProperty8, item.Key, item.Value);
178+
}
179+
}
180+
181+
//IDynamicEnumerable
182+
{
183+
var originEnumerable = ((IDynamicEnumerable)json2.MyProperty7).AsEnumerable();
184+
185+
foreach (var item in ((IDynamicEnumerable)json.MyProperty7).AsEnumerable())
186+
{
187+
item.Value1 = item.Value1 + 1;
188+
}
189+
190+
int index = 0;
191+
foreach (var item in ((IDynamicEnumerable)json.MyProperty7).AsEnumerable())
192+
{
193+
AreNotEqual(originEnumerable.Skip(index++).First(), item);
194+
}
195+
}
196+
197+
static void Check(object origin, string key, dynamic value)
198+
{
199+
var type = origin.GetType();
200+
var originProperty = type.GetProperty(key);
201+
Assert.IsNotNull(originProperty);
202+
203+
var originValue = originProperty.GetValue(origin);
204+
205+
AreNotEqual(originValue, value);
206+
}
207+
208+
static void AreNotEqual(object originValue, dynamic value)
209+
{
210+
//直接比较有点麻烦。。直接转json字符串比较
211+
Assert.AreNotEqual(JsonSerializer.Serialize(originValue), JSON.stringify(value));
212+
}
213+
}
214+
160215
#endregion Public 方法
161216
}

test/Cuture.Extensions.SystemTextJson.Dynamic.Test/DynamicJSONTestClass.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ internal class DynamicJSONTestClass
2121
}
2222
};
2323

24-
public string[] MyProperty6 { get; set; } = new[] { "1", "2", "3", "4", "5" };
24+
public string[] MyProperty6 { get; set; } = ["1", "2", "3", "4", "5"];
25+
26+
public NestedClass[] MyProperty7 { get; set; } = [new() { Value1 = 1, Value2 = "1" }, new() { Value1 = 2, Value2 = "2" }, new() { Value1 = 3, Value2 = "3" }];
27+
28+
public NestedClassCollection MyProperty8 { get; set; } = new();
2529

2630
public int? NullableProperty { get; set; } = null;
2731

@@ -55,7 +59,43 @@ public void Check(dynamic json)
5559
Assert.AreEqual(MyProperty6[i], json.MyProperty6[i]);
5660
}
5761
}
62+
63+
if (MyProperty6?.Length > 0)
64+
{
65+
for (int i = 0; i < MyProperty6.Length; i++)
66+
{
67+
Assert.AreEqual(MyProperty6[i], json.MyProperty6[i]);
68+
}
69+
}
5870
}
5971

6072
#endregion Public 方法
73+
74+
#region Public 类
75+
76+
public class NestedClass
77+
{
78+
#region Public 属性
79+
80+
public int Value1 { get; set; }
81+
82+
public string Value2 { get; set; }
83+
84+
#endregion Public 属性
85+
}
86+
87+
public class NestedClassCollection
88+
{
89+
#region Public 属性
90+
91+
public NestedClass Property1 { get; set; } = new() { Value1 = 1, Value2 = "1" };
92+
93+
public NestedClass Property2 { get; set; } = new() { Value1 = 2, Value2 = "3" };
94+
95+
public NestedClass Property3 { get; set; } = new() { Value1 = 2, Value2 = "3" };
96+
97+
#endregion Public 属性
98+
}
99+
100+
#endregion Public 类
61101
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace System.Text.Json.Dynamic;
2+
3+
[TestClass]
4+
public class JSONisArrayTest
5+
{
6+
#region Public 方法
7+
8+
[TestMethod]
9+
public void Should_Check_Success()
10+
{
11+
DynamicJSONTestClass.GetTestValue(out var origin, out var json);
12+
13+
Assert.IsFalse(JSON.isArray(json.MyProperty1));
14+
Assert.IsFalse(JSON.isArray(json.MyProperty2));
15+
Assert.IsFalse(JSON.isArray(json.MyProperty3));
16+
Assert.IsFalse(JSON.isArray(json.MyProperty4));
17+
Assert.IsFalse(JSON.isArray(json.MyProperty5));
18+
19+
Assert.IsTrue(JSON.isArray(json.MyProperty6));
20+
Assert.IsTrue(JSON.isArray(origin.MyProperty6));
21+
Assert.IsTrue(JSON.isArray(json.MyProperty7));
22+
Assert.IsTrue(JSON.isArray(origin.MyProperty7));
23+
}
24+
25+
#endregion Public 方法
26+
}

0 commit comments

Comments
 (0)