< Summary

Information
Class: ToolCore.JsonUtils
Assembly: ToolCore
File(s): /home/runner/work/InvAit/InvAit/ToolCore/JsonUtils.cs
Tag: 194_44631168438
Line coverage
83%
Covered lines: 46
Uncovered lines: 9
Coverable lines: 55
Total lines: 126
Line coverage: 83.6%
Branch coverage
82%
Covered branches: 23
Total branches: 28
Branch coverage: 82.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Serialize(...)100%11100%
SerializeCompact(...)100%11100%
Deserialize(...)100%11100%
DeserializeParameters(...)50%22100%
GetValue(...)100%22100%
GetString(...)100%22100%
GetBool(...)92.85%1414100%
GetInt(...)75%4480%
GetDictionary(...)50%2271.42%
GetObject(...)50%3233.33%
GetObject(...)100%1150%

File(s)

/home/runner/work/InvAit/InvAit/ToolCore/JsonUtils.cs

#LineLine coverage
 1using System.Text.Encodings.Web;
 2using System.Text.Json;
 3
 4namespace ToolCore;
 5
 6public static class JsonUtils
 7{
 28    private static readonly JsonSerializerOptions _jsonOptions = new()
 29    {
 210        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 211        WriteIndented = true,
 212        PropertyNameCaseInsensitive = true,
 213        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
 214    };
 15
 216    private static readonly JsonSerializerOptions _compactOptions = new()
 217    {
 218        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 219        WriteIndented = false,
 220        PropertyNameCaseInsensitive = true,
 221        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
 222    };
 23
 424    public static string Serialize(object value) => JsonSerializer.Serialize(value, _jsonOptions);
 25
 3226    public static string SerializeCompact(object value) => JsonSerializer.Serialize(value, _compactOptions);
 27
 28    public static T? Deserialize<T>(string json)
 29    {
 30        try
 31        {
 832            return JsonSerializer.Deserialize<T>(json, _jsonOptions);
 33        }
 234        catch
 35        {
 236            return default;
 37        }
 838    }
 39
 40    public static IReadOnlyDictionary<string, object> DeserializeParameters(string json)
 41    {
 42        try
 43        {
 1044            return JsonSerializer.Deserialize<Dictionary<string, object>>(json, _jsonOptions) ?? [];
 45        }
 246        catch
 47        {
 248            return new Dictionary<string, object>();
 49        }
 1050    }
 51
 52    public static object? GetValue(this IReadOnlyDictionary<string, object> parameters, string key)
 53    {
 5654        return parameters.TryGetValue(key, out var value) ? value : null;
 55    }
 56
 57    public static string? GetString(this IReadOnlyDictionary<string, object> parameters, string key)
 58    {
 659        return parameters.GetValue(key)?.ToString();
 60    }
 61
 62    public static bool GetBool(this IReadOnlyDictionary<string, object> parameters, string key, bool defaultValue = fals
 63    {
 2064        var value = parameters.GetValue(key);
 2065        return value?.ToString()?.ToLowerInvariant() switch
 2066        {
 867            "true" or "1" or "yes" => true,
 868            "false" or "0" or "no" => false,
 469            _ => defaultValue
 2070        };
 71    }
 72
 73    public static int GetInt(this IReadOnlyDictionary<string, object> parameters, string key, int defaultValue = 0)
 74    {
 2075        var value = parameters.GetValue(key);
 2676        if (value == null) return defaultValue;
 77
 1478        if (int.TryParse(value.ToString(), out int result))
 1479            return result;
 80
 081        return defaultValue;
 82    }
 83
 84    public static Dictionary<string, string>? GetDictionary(this IReadOnlyDictionary<string, object> parameters, string 
 85    {
 286        var value = parameters.GetValue(key);
 287        if (value == null) return null;
 88
 89        try
 90        {
 291            var json = JsonSerializer.Serialize(value, _jsonOptions);
 292            return JsonSerializer.Deserialize<Dictionary<string, string>>(json, _jsonOptions);
 93        }
 094        catch
 95        {
 096            return null;
 97        }
 298    }
 99
 100    public static T? GetObject<T>(this IReadOnlyDictionary<string, object> parameters, string key) where T : class
 101    {
 2102        var value = parameters.GetValue(key);
 4103        if (value == null) return null;
 104
 105        try
 106        {
 0107            return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value, _jsonOptions), _jsonOptions);
 108        }
 0109        catch
 110        {
 0111            return null;
 112        }
 0113    }
 114
 115    public static T? GetObject<T>(this JsonElement jsonElement) where T : class
 116    {
 117        try
 118        {
 20119            return jsonElement.Deserialize<T>(_jsonOptions);
 120        }
 0121        catch
 122        {
 0123            return null;
 124        }
 20125    }
 126}