• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

f2calv / CasCap.Common / 16933690265

13 Aug 2025 09:48AM UTC coverage: 0.32%. Remained the same
16933690265

push

github

web-flow
F2calv/2025 08 updates2 (#220)

* NoWarn updates

* re-added netstandard2.0 target

0 of 352 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 4 files covered. (0.0%)

3 existing lines in 2 files now uncovered.

3 of 585 relevant lines covered (0.51%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/CasCap.Common.Serialization.Json/Extensions/JsonSerializationHelpers.cs
1
namespace CasCap.Common.Extensions;
2

3
public static class JsonSerializationHelpers
4
{
5
    private static readonly ILogger _logger = ApplicationLogging.CreateLogger(nameof(JsonSerializationHelpers));
×
6

7
    public static string ToJson(this object obj) => obj.ToJson(options: null);
×
8

9
    public static string ToJson(this object obj, JsonSerializerOptions? options)
10
    {
11
        obj = obj ?? throw new ArgumentNullException(paramName: nameof(obj));
×
12
        try
13
        {
14
            return JsonSerializer.Serialize(obj, options);
×
15
        }
16
        catch (Exception ex)
×
17
        {
NEW
18
            _logger.LogError(ex, "{ClassName} {methodName} failed", nameof(JsonSerializationHelpers), nameof(JsonSerializer.Serialize));
×
19
            throw;
×
20
        }
21
    }
×
22

23
    public static T? FromJson<T>(this string json) => json.FromJson<T>(options: null);
×
24

25
    public static T? FromJson<T>(this string json, JsonSerializerOptions? options)
26
    {
27
        json = json ?? throw new ArgumentNullException(paramName: nameof(json));
×
28
        try
29
        {
30
            return JsonSerializer.Deserialize<T>(json, options);
×
31
        }
32
        catch (Exception ex)
×
33
        {
NEW
34
            _logger.LogError(ex, "{ClassName} {methodName} failed to deserialize {objectType}",
×
35
                nameof(JsonSerializationHelpers), nameof(JsonSerializer.Deserialize), typeof(T));
×
36
            throw;
×
37
        }
38
    }
×
39

40
    public static bool TryFromJson<T>(this string json, out T? output, JsonSerializerOptions? options = null)
41
    {
42
        json = json ?? throw new ArgumentNullException(paramName: nameof(json));
×
43
        output = default;
×
44
        try
45
        {
46
            output = JsonSerializer.Deserialize<T>(json, options);
×
47
            return true;
×
48
        }
49
        catch (Exception ex)
×
50
        {
NEW
51
            _logger.LogWarning(ex, "{ClassName} {methodName} failed to deserialize {objectType}",
×
52
                nameof(JsonSerializationHelpers), nameof(JsonSerializer.Deserialize), typeof(T));
×
53
        }
×
54
        return false;
×
55
    }
×
56

57
    public static T[,] To2D<T>(this List<List<T>> source)
58
    {
59
        // Adapted from this answer https://stackoverflow.com/a/26291720/3744182
60
        // By https://stackoverflow.com/users/3909293/diligent-key-presser
61
        // To https://stackoverflow.com/questions/26291609/converting-jagged-array-to-2d-array-c-sharp
62
        var firstDim = source.Count;
×
63
        var secondDim = source.Select(row => row.Count).FirstOrDefault();
×
64
        if (!source.All(row => row.Count == secondDim))
×
65
            throw new InvalidOperationException();
×
66
        var result = new T[firstDim, secondDim];
×
67
        for (var i = 0; i < firstDim; i++)
×
68
            for (int j = 0, count = source[i].Count; j < count; j++)
×
69
                result[i, j] = source[i][j];
×
70
        return result;
×
71
    }
72

73
    public static void WriteOrSerialize<T>(this JsonConverter<T> converter, Utf8JsonWriter writer, T value, JsonSerializerOptions options)
74
    {
75
        if (converter != null)
×
76
            converter.Write(writer, value, options);
×
77
        else
78
            JsonSerializer.Serialize(writer, value, typeof(T), options);
×
79
    }
×
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc