< Summary - Core.Tests

Information
Class: Common.Core.Converters.JsonDecimalString
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Converters\JsonDecimal.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 58
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_HandleNull()100%11100%
Read(...)87.5%88100%
Write(...)100%22100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Converters\JsonDecimal.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Common.Core.Converters;
 5
 6#region Nullable Decimal
 7
 8/// <summary>Converts a decimal (System.Decimal) string to or from JSON.</summary>
 9public class JsonDecimalString : JsonConverter<decimal?>
 10{
 11  /// <summary>
 12  /// Gets a value that indicates whether null should be passed to the converter on serialization.
 13  /// </summary>
 1314  public override bool HandleNull => true;
 15
 16  /// <summary>Reads and converts the JSON to a nullable decimal.</summary>
 17  /// <param name="reader">The reader.</param>
 18  /// <param name="typeToConvert">The type to convert.</param>
 19  /// <param name="options">An object that specifies serialization options to use.</param>
 20  /// <returns>The converted value or <see langword="null"/> if the value could not be converted.</returns>
 21  public override decimal? Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options )
 922  {
 923    switch( reader.TokenType )
 24    {
 25      case JsonTokenType.Number:
 126        return reader.GetDecimal();
 27
 28      case JsonTokenType.String:
 229        string? value = reader.GetString();
 230        value ??= string.Empty;
 431        if( StringConverter.TryParse( ref value, out decimal result ) ) { return result; }
 132        break;
 33
 34      default:
 635        break;
 36    }
 37
 738    return null;
 939  }
 40
 41  /// <summary>Writes a specified value as JSON.</summary>
 42  /// <param name="writer">The writer to write to.</param>
 43  /// <param name="value">The value to convert to JSON.</param>
 44  /// <param name="options">An object that specifies serialization options to use.</param>
 45  public override void Write( Utf8JsonWriter writer, decimal? value, JsonSerializerOptions options )
 1046  {
 1047    if( value.HasValue )
 348    {
 349      writer.WriteNumberValue( value.Value );
 350    }
 51    else
 752    {
 753      writer.WriteNullValue();
 754    }
 1055  }
 56}
 57
 58#endregion