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

f2calv / CasCap.Common / 18116895416

30 Sep 2025 02:36AM UTC coverage: 0.318% (-0.002%) from 0.32%
18116895416

push

github

web-flow
F2calv/updates2 (#223)

* fixes

* fine tuning

* nuget updates

* rename file

* fixes

* renaming

* renaming

* rn

* rn

* rn

* tweaks

* rn

* build warnings

* rn

* fix info msgs

* fix info msgs

* tidy

* rn

* rn

* disable

* rn

* rn

* rn

* custom exception msgs

* more improvements

* lint fix

0 of 352 branches covered (0.0%)

Branch coverage included in aggregate %.

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

3 of 592 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/JsonExtensions.cs
1
namespace CasCap.Common.Extensions;
2

3
public static class JsonExtensions
4
{
NEW
5
    private static readonly ILogger _logger = ApplicationLogging.CreateLogger(nameof(JsonExtensions));
×
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(JsonExtensions), 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}",
×
NEW
35
                nameof(JsonExtensions), 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}",
×
NEW
52
                nameof(JsonExtensions), 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
NEW
78
            JsonSerializer.Serialize(writer, value, 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