JsonExtensions 1.2.0

dotnet add package JsonExtensions --version 1.2.0
NuGet\Install-Package JsonExtensions -Version 1.2.0
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="JsonExtensions" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonExtensions --version 1.2.0
#r "nuget: JsonExtensions, 1.2.0"
#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 JsonExtensions as a Cake Addin
#addin nuget:?package=JsonExtensions&version=1.2.0

// Install JsonExtensions as a Cake Tool
#tool nuget:?package=JsonExtensions&version=1.2.0

JsonExtensions

Build Coverage Version Downloads Discord Donate

Project status: active.

This library provides a set of helpful utilities for types defined in the System.Text.Json namespace.

Download

📦 NuGet: dotnet add package JsonExtensions

Usage

Parsing JsonElement

You can use the static methods on Json class to parse JSON directly into a stateless JsonElement instance, without having to deal with JsonDocument in the process:

using JsonExtensions;

var jsonRaw = "{ \"foo\": \"bar\" }";

var jsonElement = Json.Parse(jsonRaw); // returns JsonElement
var jsonElement = Json.TryParse(jsonRaw); // returns null in case of invalid JSON

Null-safe reading

This library offers many extension methods for JsonElement that allow you to read content in a more fault-tolerant way:

using JsonExtensions.Reading;

var jsonElement = ...;

// Gets a property or returns null if:
// - element is not an object
// - property does not exist
// - property value is null
var maybeProperty = jsonElement.GetPropertyOrNull("prop");

// Gets an array child or returns null if:
// - element is not an array
// - index is out of bounds
// - child is null
var maybeChild = jsonElement.GetByIndexOrNull(3);

// Gets the value converted into specified type or returns null if:
// - element is null
// - element kind does not match the specified type
// - the value cannot be parsed into the specified type
var maybeString = jsonElement.GetStringOrNull();
var maybeInt32 = jsonElement.GetInt32OrNull();
var maybeGuid = jsonElement.GetGuidOrNull();

// Gets the value coerced into specified type or returns null if:
// - element is null
// - element kind does not match the specified type or a string
// - the value cannot be parsed into the specified type
var maybeInt32Coerced = jsonElement.GetInt32CoercedOrNull();
var maybeDoubleCoerced = jsonElement.GetDoubleCoercedOrNull();

// Enumerates an array or returns null if:
// - element is not an array
var arrayEnumerator = jsonElement.EnumerateArrayOrNull();

// Enumerates an object or returns null if:
// - element is not an object
var objectEnumerator = jsonElement.EnumerateObjectOrNull();

// Enumerates an array or returns an empty enumerator if:
// - element is not an array
foreach (var child in jsonElement.EnumerateArrayOrEmpty())
{
    // ...
}

// Enumerates an object or returns an empty enumerator if:
// - element is not an object
foreach (var (name, child) in jsonElement.EnumerateObjectOrEmpty())
{
    // ...
}

Most of these methods can also be chained together using the null-conditional operator:

// Returns null if:
// - element is not an object
// - property does not exist
// - property value is null
// - property value cannot be converted into the specified type
var maybeInt32 = jsonElement.GetPropertyOrNull("prop")?.GetInt32OrNull();

Null-safe writing

Similarly, there are also extension methods for Utf8JsonWriter that allow writing nullable versions of common value types:

using JsonExtensions.Writing;

var writer = new Utf8JsonWriter(...);

// Writes "prop":true
writer.WriteBoolean("prop", new bool?(true));

// Writes "prop":null
writer.WriteBoolean("prop", new bool?());

Parsing JSON from HTTP

To make it easier to read JSON that comes from HTTP responses, this library also provides a few helper extension methods on HttpContent and HttpClient:

using JsonExtensions.Http;

var httpClient = new HttpClient();

// Send GET request and retrieve JSON directly
var json = await httpClient.GetJsonAsync("..."); // returns JsonElement

// Read JSON from content
using var request = new HttpRequestMessage(HttpMethod.Post, "...");
using var response = await httpClient.SendAsync(request); 
var json = await response.Content.ReadAsJsonAsync(); // returns JsonElement

Accessing children by path

Using jsonElement.GetPropertyByPathOrNull(...) or jsonElement.GetPropertyByPath(...), you can get an inner child by its path:

var json = Json.Parse("{\"foo\":{\"bar\":{\"baz\":13}}}");

var child = json.GetPropertyByPath("foo.bar.baz");

var value = child.GetInt32(); // 13

Note this only supports basic paths involving child access operators. It doesn't (yet) have full support for JPath.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 is compatible.  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.
  • .NETCoreApp 3.0

    • No dependencies.
  • .NETStandard 2.0

  • net5.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on JsonExtensions:

Package Downloads
Wasari.Crunchyroll.API

Package Description

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on JsonExtensions:

Repository Stars
Tyrrrz/DiscordChatExporter
Exports Discord chat logs to a file
Tyrrrz/YoutubeDownloader
Downloads videos and playlists from YouTube
Tyrrrz/LightBulb
Reduces eye strain by adjusting gamma based on the current time
Version Downloads Last updated
1.2.0 33,948 11/15/2021
1.1.0 3,331 6/15/2021
1.0.1 1,853 11/28/2020
1.0.0 377 11/28/2020