< Summary - Core.Tests

Information
Class: Common.Core.Converters.JsonDateOnlyString
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Converters\JsonDateOnly.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 55
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
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(...)83.33%66100%
Write(...)100%22100%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Common.Core.Converters;
 5
 6#region Nullable DateOnly
 7
 8/// <summary>Converts a nullable DateOnly string to or from JSON.</summary>
 9public class JsonDateOnlyString : JsonConverter<DateOnly?>
 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 DateOnly.</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 DateOnly? Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options )
 922  {
 923    switch( reader.TokenType )
 24    {
 25      case JsonTokenType.String:
 226        string? value = reader.GetString();
 227        value ??= string.Empty;
 428        if( StringConverter.TryParse( ref value, out DateOnly result ) ) { return result; }
 129        break;
 30
 31      default:
 732        break;
 33    }
 34
 835    return null;
 936  }
 37
 38  /// <summary>Writes a specified value as JSON.</summary>
 39  /// <param name="writer">The writer to write to.</param>
 40  /// <param name="value">The value to convert to JSON.</param>
 41  /// <param name="options">An object that specifies serialization options to use.</param>
 42  public override void Write( Utf8JsonWriter writer, DateOnly? value, JsonSerializerOptions options )
 1043  {
 1044    if( value.HasValue )
 245    {
 246      writer.WriteStringValue( value.Value.ToString() );
 247    }
 48    else
 849    {
 850      writer.WriteNullValue();
 851    }
 1052  }
 53}
 54
 55#endregion