AJP.JsonElementExtensions
1.2.0
See the version list below for details.
dotnet add package AJP.JsonElementExtensions --version 1.2.0
NuGet\Install-Package AJP.JsonElementExtensions -Version 1.2.0
<PackageReference Include="AJP.JsonElementExtensions" Version="1.2.0" />
paket add AJP.JsonElementExtensions --version 1.2.0
#r "nuget: AJP.JsonElementExtensions, 1.2.0"
// Install AJP.JsonElementExtensions as a Cake Addin #addin nuget:?package=AJP.JsonElementExtensions&version=1.2.0 // Install AJP.JsonElementExtensions as a Cake Tool #tool nuget:?package=AJP.JsonElementExtensions&version=1.2.0
JsonElement.Extensions
What is it?
A collection of extension methods for System.Text.Json.JsonElement, for adding/removing properties dynamically
Why is it needed?
JsonElement is immutable, which is good, but lots of people have use case involving deserializing to a dynamic or JObject in order to 'tweak' an object thats returned from an API etc. There is currently no support for using dynamics with the serializers in System.Text.Json and wont be until after dotnet5. So I thought I'd try to fill the gap, at least temporarily.
How does it work?
So, these methods work by enumerating the existing properties of the JsonElement and then writing them into a new jsonstring in memory with additional properties added or existing properties removed along the way. The resulting string is parsed into a new JsonElement which is returned. The original JsonElement is obviously unchanged.
Please note this roundtrip process happens for every call, so if lots of changes are needed, please consider/test using ParseAsJsonStringAndMutate() so that all changes can be done together, with only one roudtrip process.
How to use it?
var jsonString = "{ \"Name\": \"Andrew\", \"EmailAddress\": \"a@b.com\" }";
var jElement = JsonDocument.Parse(jsonString).RootElement;
jElement = jElement.AddProperty("isAdmin", true);
jElement.ToString().Dump(); // yields {"isAdmin":true,"Name":"Andrew","EmailAddress":"a@b.com"}
Limitations?
Doesn't currently support the ordering of properties, all new properties end up t the beginning
License
Feel free to use for whatever - MIT license. If its usefull please let me know!
Contributing
Contributions would be most welcome! the only thing I ask is that new features are covered by minimal unit tests, along the same lines as the existing ones. Please create a PR.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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 was computed. 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. |
-
.NETStandard 2.0
- System.Text.Json (>= 4.7.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on AJP.JsonElementExtensions:
Package | Downloads |
---|---|
AJP.MediatrEndpoints
Small library which wires up aspnetcore endpoints to MediatR request handlers. |
GitHub repositories
This package is not used by any popular GitHub repositories.
1.2.0) fixed issue where primitive style objects passed in as object were treated incorrectly
1.1.0) changed target framework to netstandard2.0
1.0.0) initial version