-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/nuget/JsonExtensions.Tests/xunit-…
…2.5.0
- Loading branch information
Showing
6 changed files
with
338 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Collections.Generic; | ||
|
||
namespace viral32111.JsonExtensions; | ||
|
||
public static class ConversionExtensions { | ||
|
||
// Convert a JSON array to an array of a certain type | ||
public static T[] AsArray<T>( this JsonNode array ) { | ||
|
||
// Create an empty list | ||
List<T> list = new(); | ||
|
||
// Loop through each value in the JSON array... | ||
foreach ( JsonNode? value in array.AsArray() ) { | ||
|
||
// Fail if the value is invalid | ||
if ( value == null ) throw new JsonPropertyNullException( $"Value is null'" ); | ||
|
||
// Add the value as the desired type to the list | ||
list.Add( value.GetValue<T>() ); | ||
|
||
} | ||
|
||
// Return the list as an array | ||
return list.ToArray(); | ||
|
||
} | ||
|
||
// Copies a JSON node so it can be set in another JSON object | ||
public static JsonNode? Clone( this JsonNode? node ) { | ||
return JsonSerializer.Deserialize<JsonNode>( node ); // https://stackoverflow.com/a/71590703 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Text.Json; | ||
|
||
namespace viral32111.JsonExtensions; | ||
|
||
/// <summary> | ||
/// Default parameter values for methods provided by the library. | ||
/// </summary> | ||
public static class Defaults { | ||
|
||
/// <summary> | ||
/// Property separator character for accessing nested properties. | ||
/// </summary> | ||
public const char NestedPropertyDelimiter = '.'; | ||
|
||
/// <summary> | ||
/// Options for controlling seralization & deseralizaion. | ||
/// Retains property names, ignores comments/trailing commas, and intents. | ||
/// </summary> | ||
public static readonly JsonSerializerOptions SerializerOptions = new() { | ||
|
||
// Keep property names as they are | ||
PropertyNamingPolicy = null, | ||
PropertyNameCaseInsensitive = false, | ||
|
||
// Ignore human comments & mistakes | ||
ReadCommentHandling = JsonCommentHandling.Skip, | ||
AllowTrailingCommas = true, | ||
|
||
// Make human editing easier | ||
WriteIndented = true | ||
|
||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
|
||
namespace viral32111.JsonExtensions; | ||
|
||
/// <summary> | ||
/// Thrown when parsing JSON fails. | ||
/// </summary> | ||
public class JsonParseException : Exception { | ||
public JsonParseException( string? message ) : base( message ) { } | ||
public JsonParseException( string? message, Exception? innerException ) : base( message, innerException ) { } | ||
} | ||
|
||
/// <summary> | ||
/// Thrown when a JSON property name cannot be found. | ||
/// </summary> | ||
public class JsonPropertyNotFoundException : Exception { | ||
public JsonPropertyNotFoundException( string? message ) : base( message ) { } | ||
public JsonPropertyNotFoundException( string? message, Exception? innerException ) : base( message, innerException ) { } | ||
} | ||
|
||
/// <summary> | ||
/// Thrown when a JSON property value is not expected to be null. | ||
/// </summary> | ||
public class JsonPropertyNullException : Exception { | ||
public JsonPropertyNullException( string? message ) : base( message ) { } | ||
public JsonPropertyNullException( string? message, Exception? innerException ) : base( message, innerException ) { } | ||
} |
Oops, something went wrong.