Cuture.Extensions.SystemTextJson.Dynamic 1.0.3

dotnet add package Cuture.Extensions.SystemTextJson.Dynamic --version 1.0.3                
NuGet\Install-Package Cuture.Extensions.SystemTextJson.Dynamic -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cuture.Extensions.SystemTextJson.Dynamic" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cuture.Extensions.SystemTextJson.Dynamic --version 1.0.3                
#r "nuget: Cuture.Extensions.SystemTextJson.Dynamic, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cuture.Extensions.SystemTextJson.Dynamic as a Cake Addin
#addin nuget:?package=Cuture.Extensions.SystemTextJson.Dynamic&version=1.0.3

// Install Cuture.Extensions.SystemTextJson.Dynamic as a Cake Tool
#tool nuget:?package=Cuture.Extensions.SystemTextJson.Dynamic&version=1.0.3                

Cuture.Extensions.SystemTextJson.Dynamic

Extension of System.Text.Json to support dynamic access using dynamic

System.Text.Json 的拓展,以支持使用 dynamic 进行动态访问

  • 使用 dynamic 访问,无需预定义实体类型
  • 支持修改/添加属性

创建 JSON 对象

Json字符串 创建 JSON 对象

using System.Text.Json.Dynamic;

var rawJson = 
    """
    {
    "a":1,
    "b":2.0,
    "c":{
        "a":"1",
        "b":"2",
        },
    }
    """;

var json = JSON.parse(rawJson);

对象 创建 JSON 对象

using System.Text.Json.Dynamic;

var json = JSON.create(new object());

访问 JSON 对象

//访问属性
var a = json.a;
var c = json.a.b.c;

//访问数组
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 对象

//设置值(不可多级设置未定义字段)
json.a = new {};
json.a.b = 1;

json.c = new
{
    d = new
    {
        e = 2
    }
};

int value = json.a.b;

检查 JSON 对象

//判断是否为未定义字段
var b1 = json.notexistfield == null;
var b2 = json.notexistfield == JSON.Undefined;
var b3 = JSON.isUndefined(json.notexistfield);

//多级属性判断是否为未定义字段
var b4 = JSON.isUndefined(() => json.a.b.c.e.f.g.h.i.j.k);

IEnumerable

使用IEnumerable以支持使用Linq, (C#不支持实现dynamic的接口,所以需要额外的转换)

//遍历Array
//显式赋值类型
IEnumerable<dynamic> enumerable = json.Array;
//通过IDynamicEnumerable
var enumerable = ((IDynamicEnumerable)json.Array).AsEnumerable();

//遍历属性
//显式赋值类型
IEnumerable<KeyValuePair<string, dynamic?>> enumerable = json;
//通过IDynamicKeyValueEnumerable
var enumerable = ((IDynamicKeyValueEnumerable)json).AsEnumerable();

Index && Range

支持使用 Index 和 Range 语法访问数组

Note: 当前使用 Range 语法将会创建新的对象,对其修改不会反映到原始对象

//Index
var value = json.Array[^1];

//Range
var items = json.Array[..1];
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 110 8/11/2024
1.0.2 264 2/14/2023
1.0.1 248 2/11/2023