viral32111.JsonExtensions 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package viral32111.JsonExtensions --version 0.2.0                
NuGet\Install-Package viral32111.JsonExtensions -Version 0.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="viral32111.JsonExtensions" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add viral32111.JsonExtensions --version 0.2.0                
#r "nuget: viral32111.JsonExtensions, 0.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 viral32111.JsonExtensions as a Cake Addin
#addin nuget:?package=viral32111.JsonExtensions&version=0.2.0

// Install viral32111.JsonExtensions as a Cake Tool
#tool nuget:?package=viral32111.JsonExtensions&version=0.2.0                

JSON Extensions

This is a NuGet package for .NET 6.0 that adds several creation and extension methods to System.Text.Json data types, primarily for filesystem manipulation and accessing nested properties.

Usage

  1. Add the latest version of the viral32111.JsonExtensions NuGet package to your .NET project.

  2. Include the namespace at the beginning of your C# source file(s) with the following code:

using viral32111.JsonExtensions;

Documentation

Read JSON from a file

<pre> <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a> <a href="./Library/Source/JsonExtensions.cs#L35-L52">JsonExtensions.ReadFromFile</a>( string filePath ) </pre>

Parses the contents of the file at the specified path as JSON and returns a JsonObject.

The following exceptions may be thrown:

Version history:

  • This method has not changed since 0.1.0.

Create a new JSON file

<pre> <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a> <a href="./Library/Source/JsonExtensions.cs#L55-L69">JsonExtensions.CreateNewFile</a>( string filePath, JsonObject? jsonObject ) </pre>

Creates (or overwrites) the file at the specified path with the specified JsonObject (or an empty one if one is not specified), then returns it.

The following exceptions may be thrown:

  • No exceptions should be thrown when using this method.

Version history:

Save JSON to file

<pre> void <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a>.<a href="./Library/Source/JsonExtensions.cs#L72-L83">SaveToFile</a>( string? filePath ) </pre>

Saves this JsonObject to the file at the specified path. Defaults to the path used to read or create the file if no path is specified.

The following exceptions may be thrown:

  • No exceptions should be thrown when using this method.

Version history:

  • This method has not changed since 0.1.0.

Set nested property

<pre> void <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a>.<a href="./Library/Source/JsonExtensions.cs#L86-L120">NestedSet</a>( string propertyPath, JsonNode? newValue ) </pre>

Sets the specified nested property in this JsonObject to the specified value. Dots (.) are used to separate nested property names.

The following exceptions may be thrown:

Version history:

  • This method has not changed since 0.1.0.

Get nested property

<pre> <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonnode">System.Text.Json.Nodes.JsonNode?</a> <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a>.<a href="./Library/Source/JsonExtensions.cs#L123-L157">NestedGet</a>( string propertyPath ) </pre>

Gets the specified nested property in this JsonObject, and returns it as a JsonNode (may be null). Dots (.) are used to separate nested property names.

The following exceptions may be thrown:

Version history:

Get nested property as type

<pre> <a href="https://docs.microsoft.com/dotnet/api/system.type">System.Type</a> <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a>.<a href="./Library/Source/JsonExtensions.cs#L160-L177">NestedGet</a><<a href="https://docs.microsoft.com/dotnet/api/system.type">System.Type</a>>( string propertyPath ) </pre>

Gets the specified nested property in this JsonObject as the specified data type. See JsonObject.NestedGet() for more information.

The following exceptions may be thrown:

Version history:

  • This method has not changed since 0.1.0.

Get array as type

<pre> <a href="https://docs.microsoft.com/dotnet/api/system.type">System.Type</a>[] <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonnode">System.Text.Json.Nodes.JsonNode</a>.<a href="./Library/Source/JsonExtensions.cs#L180-L201">AsArray</a><<a href="https://docs.microsoft.com/dotnet/api/system.type">System.Type</a>>() </pre>

Converts this JsonNode to a JsonArray then to an array of the specified data type.

The following exceptions may be thrown:

Version history:

  • This method has not changed since 0.1.0.

Clone node

<pre> <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonnode">JsonNode</a>? <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonnode">System.Text.Json.Nodes.JsonNode</a>?.<a href="./Library/Source/JsonExtensions.cs#L202-L204">Clone</a>() </pre>

Creates a copy of this JsonNode, or returns null if it is null.

The following exceptions may be thrown:

  • No exceptions should be thrown when using this method.

Version history:

  • This method was added in 0.2.0.

Has nested property

<pre> bool <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a>.<a href="./Library/Source/JsonExtensions.cs#L207-L238">NestedHas</a>( string propertyPath ) </pre>

Checks if a nested property exists in this JsonObject. A null property value counts as the property existing.

The following exceptions may be thrown:

Version history:

  • This method was added in 0.2.0.

List nested property

<pre> string[] <a href="https://docs.microsoft.com/dotnet/api/system.text.json.nodes.jsonobject">System.Text.Json.Nodes.JsonObject</a>.<a href="./Library/Source/JsonExtensions.cs#L241-L275">NestedList</a>() </pre>

Creates a list of paths to all nested properties in this JsonObject, null property values are included.

The following exceptions may be thrown:

  • No exceptions should be thrown when using this method.

Version history:

  • This method was added in 0.2.0.

License

Copyright (C) 2022 viral32111.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,209 8/1/2023
0.3.1 151 8/1/2023
0.3.0 169 8/1/2023
0.2.0 183 7/23/2023