Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/viklover/jsonhelper.net
Newtonsoft.Json helper we needed
https://github.com/viklover/jsonhelper.net
dotnet json newtonsoft-json
Last synced: about 1 month ago
JSON representation
Newtonsoft.Json helper we needed
- Host: GitHub
- URL: https://github.com/viklover/jsonhelper.net
- Owner: viklover
- License: mit
- Created: 2025-01-05T16:51:00.000Z (about 1 month ago)
- Default Branch: master
- Last Pushed: 2025-01-06T09:38:40.000Z (about 1 month ago)
- Last Synced: 2025-01-06T09:39:10.502Z (about 1 month ago)
- Topics: dotnet, json, newtonsoft-json
- Language: C#
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Newtonsoft.Json helper
[![Nuget](https://badge.fury.io/nu/JsonHelper.Net.svg)](https://badge.fury.io/nu/JsonHelper.Net)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/viklover/JsonHelper.Net/blob/master/LICENSE.txt)
![Linter workflow](https://github.com/viklover/JsonHelper.Net/actions/workflows/lint.yml/badge.svg)
![Unit tests workflow](https://github.com/viklover/JsonHelper.Net/actions/workflows/unit-tests.yml/badge.svg)Helper for Newtonsoft.Json library.
Now you can select JSON tokens by [paths](https://en.wikipedia.org/wiki/JSONPath) without nullability checks:
```csharp
var json = JToken.Parse("{\"hello\":\"world\"}");// Newtonsoft.Json approach
var worldToken = json.SelectToken("$.world");
var worldTokenValue = worldToken?.ToObject();
if (worldToken == null || worldTokenValue == null) {
throw Exception("Expected 'world' token");
}// JsonHelper.Net approach
var world = JsonHelper.SelectStringOrThrow(json, "$.world");
```
You can also select .NET native types like `Guid` or `DateTime`:```csharp
// {"guid":"b3f3f9bc-8b7d-4199-971b-6c35152412da","date":"2024-01-05T00:00:00.0000000"}Guid guid = JsonHelper.SelectGuid(json, "$.guid")
DateTime date = JsonHelper.SelectDate(json, "$.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:
```csharp
// {"dict":{"body":["a","b","c",{"foo":"bar"}]}}JToken body = JsonHelper.SelectOrThrow(json, "$.dict.body", JTokenType.Array);
```Selecting `List`:
```csharp
// {"list":[1, -0.92, 3.47, 4]}
var list = JsonHelper.SelectListOrThrow(json, "$.list");
```## Installation
```bash
dotnet add package JsonHelper.Net --version 1.0.4
```
Nuget page is [here](https://www.nuget.org/packages/JsonHelper.Net)## Contribution
Pull requests are welcome! I will gladly review any feature suggestions and consider them for inclusion in the library.