VOOZH about

URL: https://www.nuget.org/packages/Dynamic.Json/

⇱ NuGet Gallery | Dynamic.Json 1.4.2




Dynamic.Json 1.4.2

dotnet add package Dynamic.Json --version 1.4.2
 
 
NuGet\Install-Package Dynamic.Json -Version 1.4.2
 
 
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="Dynamic.Json" Version="1.4.2" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dynamic.Json" Version="1.4.2" />
 
Directory.Packages.props
<PackageReference Include="Dynamic.Json" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Dynamic.Json --version 1.4.2
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Dynamic.Json, 1.4.2"
 
 
#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.
#:package Dynamic.Json@1.4.2
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Dynamic.Json&version=1.4.2
 
Install as a Cake Addin
#tool nuget:?package=Dynamic.Json&version=1.4.2
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

This is a .NET Standard 2.0 library providing a lightweight dynamic wrapper for super-neat, fast and low-allocating working with JSON, based on the new System.Text.Json.

This library is especially useful for prototyping and scripting when you have to keep the code as clean and easy-to-read as possible.

Install

PM> Install-Package Dynamic.Json

How to use

Instantiate dynamic json:

// parse json from string/stream/etc, for example
var json = DJson.Parse(@"
{
 ""versionNumber"": 1,
 ""product_name"": ""qwerty"",
 ""items"": [ 1, 2, 3 ],
 ""settings"": {
 ""enabled"": false
 }
}");

// or read json from file
var json = DJson.Read("file.json");

// or get json from HTTP
var json = await DJson.GetAsync("https://api.com/endpoint");

// or use HttpClient extension
var json = await httpClient.GetJsonAsync("https://api.com/endpoint");

Use dynamic json as a normal class

You can access json props either like class properties (json.someProperty) or like dictionary elements (json["someProperty"]). Both methods are identical, however, the first one looks better 😃

int version = json.versionNumber;
string name = json.product_name;
int item = json.items[1];
int length = json.items.length; //or json.items.count, as you prefer
bool enabled = json.settings.enabled;

Flexible naming convention

camelCase and snake_case are common naming styles in JSON, but uncommon in C# where we use PascalCase for public properties. Therefore, for perfectionists like me DJson allows to access json props not only by their original names (e.g. json.ip_endpoint), but also by its PascalCase version (e.g. json.IPEndpoint). DJson will try to resolve names automatically.

var val = json.VersionNumber;
var str = json.ProductName;
var arr = json.Items[1];
var len = json.Items.Length; //or .Count
var obj = json.Settings.Enabled;

Enumerate arrays

You can enumerate json arrays in foreach or by implicit conversion to IEnumerable<dynamic>.

foreach (var item in json.Items)
 Console.WriteLine($"{item}");

var items = (IEnumerable<dynamic>)json.Items;
var sum = items.Where(x => x > 1).Sum(x => x);

Enumerate objects

You can enumerate json object props (string Name, dynamic Value) in foreach or by implicit conversion to IEnumerable<dynamic>.

foreach (var prop in json.Settings)
 Console.WriteLine($"{prop.Name}: {prop.Value}");

var props = (IEnumerable<dynamic>)json.Settings;
var names = props.Where(x => !x.Value).Select(x => x.Name);

Convert dynamic json to any valid type

While conversion to all built-in C# types is pretty transparent, you can also serialize json to any other type using implicit conversion.

var list = (List<int>)json.Items; // dynamic array -> List<int>
var settings = (MyClass)json.Settings; // dynamic object -> MyClass
var obj = (AnotherType)json;

Extract DateTime like a boss

People often use Unix time format in JSON and that's quite annoying to convert it to DateTime, so DJson does this for you automatically.

var json = DJson.Parse(@"
{
 ""time"": ""2020-09-23T21:12:16Z"",
 ""unix_time"": 1600895536,
 ""unix_time_ms"": 1600895536123
}");

DateTime time = json.Time;
DateTime unixTime = json.UnixTime; // DJson automatically detects if it's Unix time in seconds
DateTime unixTimeMs = json.UnixTimeMs; // or if it's Unix time in milliseconds

What about performance?

One can think that using dynamic wrapper brings a huge overhead, but it's actually not. Here is a simple benchmark, comparing DJson with JsonDocument from System.Text.Json and JToken from Newtonsoft.Json:

| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated |
|--------------- |---------:|----------:|----------:|------:|--------:|-------:|----------:|
| SystemTextJson | 3.046 us | 0.0590 us | 0.0655 us | 1.00 | 0.00 | 0.1945 | 1.21 KB |
| DJson | 4.111 us | 0.0308 us | 0.0257 us | 1.35 | 0.03 | 0.5722 | 3.53 KB |
| Newtonsoft | 7.003 us | 0.1357 us | 0.1946 us | 2.29 | 0.09 | 1.4496 | 8.91 KB |

Of course, using .Deserialize<T>() would be even faster, but we're only talking about dynamic-like access, when you will likely spend a lot more time describing the types do deserialize your JSON to.

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

    • No dependencies.
  • .NETStandard 2.0

  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (13)

Showing the top 5 NuGet packages that depend on Dynamic.Json:

Package Downloads
Netezos

Tezos blockchain SDK for .NET

ModifiedNetezos

.NET SDK for Tezos blockchain (.NET Stansard 2.0)

SomeTestPackage123

.NET SDK for Tezos blockchain (.NET Stansard 2.0)

Zack.EventBus

Package Description

HMEFramework.EventBus

HMEFramework.EventBus for .net core, Please upgrade to the latest HMENetCore.EventBus.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Dynamic.Json:

Repository Stars
yangzhongke/NETBookMaterials
Version Downloads Last Updated
1.4.2 7,081 4/19/2025
1.4.1 14,369 5/3/2024
1.4.0 15,699 3/2/2023
1.3.0 89,711 3/13/2021
1.2.0 7,014 10/22/2020
1.1.0 530 10/7/2020