Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/JsonExtensions.Tests/coverl…
Browse files Browse the repository at this point in the history
…et.collector-6.0.0
  • Loading branch information
viral32111 authored Jul 23, 2023
2 parents ef49872 + 0372bee commit 51d56d2
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 271 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: CI
on:
push:
paths:
- 'JsonExtensions/'
- 'JsonExtensions.Examples/'
- 'JsonExtensions.Tests/'
- 'JsonExtensions/**/*'
- 'JsonExtensions.Examples/**/*'
- 'JsonExtensions.Tests/**/*'
- 'JsonExtensions.sln'
- '.github/workflows/ci.yml'
branches:
Expand Down
36 changes: 36 additions & 0 deletions JsonExtensions/Source/ConversionExtensions.cs
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
}

}
34 changes: 34 additions & 0 deletions JsonExtensions/Source/Defaults.cs
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

};

}
27 changes: 27 additions & 0 deletions JsonExtensions/Source/Exceptions.cs
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 ) { }
}
Loading

0 comments on commit 51d56d2

Please sign in to comment.