Skip to content

GStefanowich/json-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

This is a very simple library I built for more easily interactive with the System.Text.Json library built into newer versions of .Net

Examples

System.Text.Json is great about working with Serializing and Deserializing between C# Objects and JsonObject / JsonArray / JsonNode / etc; but not always so straight forward when you just want to do simple JSON access.

In vanilla System.Text.Json you may do something like:

JsonObject obj = new JsonObject();
if (
    obj.TryGetPropertyValue("key", out JsonNode? node)
    && node is JsonObject obj2
    && obj2.TryGetPropertyValue("key2", out JsonNode2? node)
) {
    // ... Do stuff here
}

JsonObject has one built-in TryGetValue Method which outs a JsonNode?, which could be a JsonValue<T>, JsonArray, or JsonObject.

Here instead we can do

JsonObject obj = new JsonObject();
if (
    obj.TryGetPropertyValue("key", out JsonObject? obj2)
    && obj2.TryGetPropertyValue("key2", out JsonObject? ob3)
) {
    // ... Do stuff here
}

We can also easily out values from JsonValue<T>

JsonObject obj = new();
obj.TryGetPropertyValue("key", out string _);
obj.TryGetPropertyValue("key", out bool _);
obj.TryGetPropertyValue("key", out long _);
obj.TryGetPropertyValue("key", out ulong _);
obj.TryGetPropertyValue("key", out int _);
obj.TryGetPropertyValue("key", out uint _);
obj.TryGetPropertyValue("key", out Guid _);
obj.TryGetPropertyValue("key", out DateTimeOffset _);

About

A C# library that adds Extension Methods for System.Text.Json

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

 
 
 

Contributors

Languages