https://github.com/netlah/dynamic.json
Dynamic access to property/member of Json object deserialized by System.Text.Json
https://github.com/netlah/dynamic.json
Last synced: 5 months ago
JSON representation
Dynamic access to property/member of Json object deserialized by System.Text.Json
- Host: GitHub
- URL: https://github.com/netlah/dynamic.json
- Owner: NetLah
- License: mit
- Created: 2020-12-11T11:33:14.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-01-10T02:32:02.000Z (over 2 years ago)
- Last Synced: 2024-12-06T05:47:12.627Z (over 1 year ago)
- Language: C#
- Homepage:
- Size: 44.9 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Testing.Dynamic.Json
Provides dynamicly access to property/member of Json object deserialized by System.Text.Json. This library is used for unit testing purpose only. It is not designated for production usage.
### Nuget package
[](https://www.nuget.org/packages/Testing.Dynamic.Json/)
### Build Status
[](https://actions-badge.atrox.dev/NetLah/Dynamic.Json/goto?ref=main)
### Getting started
To support testing with dynamic json, first install the NuGet package:
```
Install-Package Testing.Dynamic.Json
```
### Example dynamic json
- Newtonsoft Json support
```
const string json = "[ 123, \"abc\", { \"iss\": \"Sso Issuer\", \"iat\": 1607746395 }]";
var jToken = Newtonsoft.Json.Linq.JToken.Parse(json);
Assert.Equal(3, (jToken as JArray).Count);
Assert.Equal(123, jToken[0]);
Assert.Equal(123L, jToken[0]);
Assert.Equal("abc", jToken[1]);
var jwtPayload = jToken[2];
Assert.Equal("Sso Issuer", jwtPayload["iss"]);
Assert.Equal(1607746395, jwtPayload["iat"]);
```
- Testing.Dynamic.Json support
```
var djson = System.Text.Json.JsonSerializer.Deserialize(json);
Assert.Equal(3, djson.CountMembers);
Assert.Equal(123, (int)(long)djson[0]);
Assert.Equal(123L, djson[0]);
Assert.Equal("abc", djson[1]);
var jwtPayload = (DJson)djson[2];
Assert.Equal("Sso Issuer", jwtPayload["iss"]);
Assert.Equal(1607746395L, jwtPayload["iat"]);
```