/JsonHelper.NET

Newtonsoft.Json extension methods we needed

Primary LanguageC#MIT LicenseMIT

Newtonsoft.Json helper drawing

Nuget License Linter workflow Unit tests workflow

Extension methods for Newtonsoft.Json library.

Now you can select JSON tokens by paths without nullability checks:

var json = JToken.Parse("{\"hello\":\"world\"}");

// Newtonsoft.Json approach
var worldToken = json.SelectToken("$.world");
var worldTokenValue = worldToken?.ToObject<string>();
if (worldToken == null || worldTokenValue == null) {
    throw Exception("Expected 'world' token");
}

// JsonHelper.Net approach
var world = json.SelectStringOrThrow("$.world");

You can also select .NET native types like Guid or DateTime:

// {"guid":"b3f3f9bc-8b7d-4199-971b-6c35152412da","date":"2024-01-05T00:00:00.0000000"}

Guid guid = json.SelectGuid("$.guid")
DateTime date = json.SelectDate("$.date");

// use `SelectGuidOrThrow` or 'SelectDateOrThrow' if expected non-nullable value

If you need to select raw complex JToken objects, the helper provides some useful methods:

// {"dict":{"body":["a","b","c",{"foo":"bar"}]}}

JToken body = json.SelectOrThrow("$.dict.body", JTokenType.Array);

Selecting List<T>:

// {"list":[1, -0.92, 3.47, 4]}
var list = json.SelectListOrThrow<float>("$.list");

Installation

dotnet add package JsonHelper.Net --version 1.1.0

Nuget page is here

Contribution

Pull requests are welcome! I will gladly review any feature suggestions and consider them for inclusion in the library.