< Summary - Core.Tests

Information
Class: Common.Core.Converters.JsonBooleanString
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Converters\JsonBoolean.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 61
Line coverage: 100%
Branch coverage
90%
Covered branches: 10
Total branches: 11
Branch coverage: 90.9%
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(...)88.88%99100%
Write(...)100%22100%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Common.Core.Converters;
 5
 6#region Nullable Boolean
 7
 8/// <summary>Converts a nullable boolean (System.Boolean) string to or from JSON.</summary>
 9public class JsonBooleanString : JsonConverter<bool?>
 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 boolean.</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 bool? Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options )
 922  {
 923    switch( reader.TokenType )
 24    {
 25      case JsonTokenType.False:
 126        return false;
 27
 28      case JsonTokenType.True:
 129        return true;
 30
 31      case JsonTokenType.String:
 532        string? value = reader.GetString();
 533        value ??= string.Empty;
 1334        if( StringConverter.TryParse( ref value, out bool result ) ) { return result; }
 135        break;
 36
 37      default:
 238        break;
 39    }
 40
 341    return null;
 942  }
 43
 44  /// <summary>Writes a specified value as JSON.</summary>
 45  /// <param name="writer">The writer to write to.</param>
 46  /// <param name="value">The value to convert to JSON.</param>
 47  /// <param name="options">An object that specifies serialization options to use.</param>
 48  public override void Write( Utf8JsonWriter writer, bool? value, JsonSerializerOptions options )
 1049  {
 1050    if( value.HasValue )
 751    {
 752      writer.WriteBooleanValue( value.Value );
 753    }
 54    else
 355    {
 356      writer.WriteNullValue();
 357    }
 1058  }
 59}
 60
 61#endregion