AJP.JsonElementExtensions 1.2.0

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AJP.JsonElementExtensions" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AJP.JsonElementExtensions --version 1.2.0                
#r "nuget: AJP.JsonElementExtensions, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.8.0 72 10/22/2024
1.7.0 20,345 10/22/2022
1.6.0 26,553 8/9/2021
1.5.0 4,061 6/12/2021
1.4.0 1,443 5/23/2021
1.3.0 2,498 2/28/2021
1.2.0 477 2/28/2021
1.1.0 2,815 2/29/2020
1.0.0 680 2/28/2020

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