Skip to content

Commit

Permalink
ovrride GetDynamicMemberNames
Browse files Browse the repository at this point in the history
  • Loading branch information
stratosblue committed Feb 14, 2023
1 parent c0deb74 commit 530f249
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ var v2 = json.x[1];
//访问属性并转换为基础数据类型
bool b1 = json.b;
string s1 = json.s;

//局部反序列化
MyClass obj = json.a.b; //将 json.a.b 反序列化到类型 MyClass
//获取内部的原始 System.Text.Json.Nodes.* 对象
JsonArray array = json.ArrayProperty;
JsonObject obj = json.ObjectProperty;
JsonNode node = json.NodeProperty;
```

## 修改 `JSON` 对象
Expand Down Expand Up @@ -96,4 +104,4 @@ var enumerable = ((IDynamicEnumerable)json.Array).AsEnumerable();
IEnumerable<KeyValuePair<string, dynamic?>> enumerable = json;
//通过IDynamicKeyValueEnumerable
var enumerable = ((IDynamicKeyValueEnumerable)json).AsEnumerable();
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<Authors>Stratos</Authors>

<Version>1.0.1</Version>
<Version>1.0.2</Version>

<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/cuture/Cuture.Extensions.SystemTextJson.Dynamic</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ internal class JsonArrayDynamicAccessor

#endregion Private 字段

#region Public 属性

public int Length => _jsonArray.Count;

#endregion Public 属性

#region Public 构造函数

public JsonArrayDynamicAccessor(JsonArray jsonArray) : base(jsonArray)
Expand All @@ -26,11 +32,26 @@ public JsonArrayDynamicAccessor(JsonArray jsonArray) : base(jsonArray)

#region Public 方法

public override IEnumerable<string> GetDynamicMemberNames()
{
yield return "Length";
}

public IEnumerator GetEnumerator()
{
return new JsonArrayEnumerator(_jsonArray);
}

public override bool TryConvert(ConvertBinder binder, out object? result)
{
if (binder.ReturnType == typeof(JsonArray))
{
result = _jsonArray;
return true;
}
return base.TryConvert(binder, out result);
}

public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object? result)
{
var index = GetIndex(indexes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public override bool TryConvert(ConvertBinder binder, out object? result)
{
result = dynamicKeyValueEnumerable.AsEnumerable();
}
else if (binder.ReturnType == typeof(JsonNode))
{
result = Node;
}
else
{
result = Node.Deserialize(binder.ReturnType, JSON.s_defaultJsonSerializerOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ public JsonObjectDynamicAccessor(JsonNode jsonNode) : base(jsonNode)

#region Public 方法

public override IEnumerable<string> GetDynamicMemberNames()
{
foreach (var item in _jsonObject)
{
yield return item.Key;
}
}

public override bool TryConvert(ConvertBinder binder, out object? result)
{
if (binder.ReturnType == typeof(JsonObject))
{
result = _jsonObject;
return true;
}
return base.TryConvert(binder, out result);
}

public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object? result)
{
var index = GetIndex(indexes);
Expand Down
10 changes: 0 additions & 10 deletions test/Cuture.Extensions.SystemTextJson.Dynamic.Test/GenericTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ public class JSONParseTest
{
#region Public 方法

[TestMethod]
public void ShouldDeSerializePartialSuccess()
{
var obj = new DeSerializePartialClass1()
{
P = new()
{
P = new()
{
Age = 10,
Name = "HelloWorld",
}
}
};

var json = JSON.create(obj);

DeSerializePartialClass3 dp = json.P.P;

Assert.IsNotNull(dp);

Assert.AreEqual(10, dp.Age);
Assert.AreEqual("HelloWorld", dp.Name);
}

[TestMethod]
public void ShouldSuccessForNull()
{
Expand Down Expand Up @@ -45,5 +70,34 @@ public void ShouldSuccessForValueString()
Assert.AreEqual("1", JSON.parse("\"1\""));
}

private class DeSerializePartialClass1
{
#region Public 属性

public DeSerializePartialClass2 P { get; set; }

#endregion Public 属性
}

private class DeSerializePartialClass2
{
#region Public 属性

public DeSerializePartialClass3 P { get; set; }

#endregion Public 属性
}

private class DeSerializePartialClass3
{
#region Public 属性

public int Age { get; set; }

public string Name { get; set; }

#endregion Public 属性
}

#endregion Public 方法
}

0 comments on commit 530f249

Please sign in to comment.