< Summary

Information
Class: UIBlazor.Utils.JsonUtils
Assembly: UIBlazor
File(s): /home/runner/work/InvAit/InvAit/UIBlazor/Utils/JsonUtils.cs
Tag: 71_26091983037
Line coverage
91%
Covered lines: 44
Uncovered lines: 4
Coverable lines: 48
Total lines: 105
Line coverage: 91.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%
PrettyPrintFormat(...)100%11100%
DeserializeParameters(...)50%22100%
GetValue(...)100%22100%
GetString(...)100%22100%
GetBool(...)87.5%1616100%
GetInt(...)75%44100%
GetObject(...)50%2266.66%
GetObject(...)100%1150%

File(s)

/home/runner/work/InvAit/InvAit/UIBlazor/Utils/JsonUtils.cs

#LineLine coverage
 1using System.Text.Encodings.Web;
 2
 3namespace UIBlazor.Utils;
 4
 5public static class JsonUtils
 6{
 17    private static readonly JsonSerializerOptions _jsonOptions = new()
 18    {
 19        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 110        WriteIndented = true,
 111        PropertyNameCaseInsensitive = true,
 112        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
 113    };
 14
 115    private static readonly JsonSerializerOptions _compactOptions = new()
 116    {
 117        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 118        WriteIndented = false,
 119        PropertyNameCaseInsensitive = true,
 120        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
 121    };
 22
 823    public static string Serialize(object value) => JsonSerializer.Serialize(value, _jsonOptions);
 24
 425    public static string SerializeCompact(object value) => JsonSerializer.Serialize(value, _compactOptions);
 26
 16027    public static T? Deserialize<T>(string json) => JsonSerializer.Deserialize<T>(json, _jsonOptions);
 28
 29    public static string PrettyPrintFormat(string minifiedJson)
 30    {
 131        using var document = JsonDocument.Parse(minifiedJson);
 132        return JsonSerializer.Serialize(document.RootElement, new JsonSerializerOptions { WriteIndented = true });
 133    }
 34
 35    public static IReadOnlyDictionary<string, object> DeserializeParameters(string json)
 36    {
 37        try
 38        {
 339            return JsonSerializer.Deserialize<Dictionary<string, object>>(json, _jsonOptions) ?? [];
 40        }
 141        catch
 42        {
 143            return new Dictionary<string, object>();
 44        }
 345    }
 46
 47    public static object? GetValue(this IReadOnlyDictionary<string, object> parameters, string key)
 48    {
 1849        return parameters.TryGetValue(key, out var value) ? value : null;
 50    }
 51
 52    public static string? GetString(this IReadOnlyDictionary<string, object> parameters, string key)
 53    {
 354        return parameters.GetValue(key)?.ToString();
 55    }
 56
 57    public static bool GetBool(this IReadOnlyDictionary<string, object> parameters, string key, bool defaultValue = fals
 58    {
 859        var value = parameters.GetValue(key);
 860        return value?.ToString()?.ToLowerInvariant() switch
 861        {
 362            "true" or "1" or "yes" => true,
 363            "false" or "0" or "no" => false,
 264            _ => defaultValue
 865        };
 66    }
 67
 68    public static int GetInt(this IReadOnlyDictionary<string, object> parameters, string key, int defaultValue = 0)
 69    {
 470        var value = parameters.GetValue(key);
 471        if (value == null) return defaultValue;
 72
 473        if (int.TryParse(value.ToString(), out int result))
 274            return result;
 75
 276        return defaultValue;
 77    }
 78
 79    public static T? GetObject<T>(this IReadOnlyDictionary<string, object> parameters, string key) where T : class
 80    {
 181        var value = parameters.GetValue(key);
 182        if (value == null) return null;
 83
 84        try
 85        {
 186            return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value, _jsonOptions), _jsonOptions);
 87        }
 088        catch
 89        {
 090            return null;
 91        }
 192    }
 93
 94    public static T? GetObject<T>(this JsonElement jsonElement) where T : class
 95    {
 96        try
 97        {
 198            return jsonElement.Deserialize<T>(_jsonOptions);
 99        }
 0100        catch
 101        {
 0102            return null;
 103        }
 1104    }
 105}