< Summary - Core.Tests

Information
Class: Common.Core.Converters.JsonIntegerString
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Converters\JsonInteger.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\JsonInteger.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Common.Core.Converters;
 5
 6#region Nullable Integer
 7
 8/// <summary>Converts a nullable integer (System.Int32) string to or from JSON.</summary>
 9public class JsonIntegerString : JsonConverter<int?>
 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 integer.</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 int? Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options )
 922  {
 923    switch( reader.TokenType )
 24    {
 25      case JsonTokenType.Number:
 126        return reader.GetInt32();
 27
 28      case JsonTokenType.String:
 329        string? value = reader.GetString();
 330        value ??= string.Empty;
 531        if( StringConverter.TryParse( ref value, out int result ) ) { return result; }
 232        break;
 33
 34      default:
 535        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, int? 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