< Summary

Information
Class: UIBlazor.Utils.JsonUtils
Assembly: UIBlazor
File(s): /home/runner/work/InvAit/InvAit/UIBlazor/Utils/JsonUtils.cs
Tag: 14_22728831704
Line coverage
90%
Covered lines: 36
Uncovered lines: 4
Coverable lines: 40
Total lines: 95
Line coverage: 90%
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%
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
 315    public static string Serialize(object value) => JsonSerializer.Serialize(value, _jsonOptions);
 16
 317    public static T? Deserialize<T>(string json) => JsonSerializer.Deserialize<T>(json, _jsonOptions);
 18
 19    public static string PrettyPrintFormat(string minifiedJson)
 20    {
 121        using var document = JsonDocument.Parse(minifiedJson);
 122        return JsonSerializer.Serialize(document.RootElement, new JsonSerializerOptions { WriteIndented = true });
 123    }
 24
 25    public static IReadOnlyDictionary<string, object> DeserializeParameters(string json)
 26    {
 27        try
 28        {
 229            return JsonSerializer.Deserialize<Dictionary<string, object>>(json, _jsonOptions) ?? [];
 30        }
 131        catch
 32        {
 133            return new Dictionary<string, object>();
 34        }
 235    }
 36
 37    public static object? GetValue(this IReadOnlyDictionary<string, object> parameters, string key)
 38    {
 1839        return parameters.TryGetValue(key, out var value) ? value : null;
 40    }
 41
 42    public static string? GetString(this IReadOnlyDictionary<string, object> parameters, string key)
 43    {
 344        return parameters.GetValue(key)?.ToString();
 45    }
 46
 47    public static bool GetBool(this IReadOnlyDictionary<string, object> parameters, string key, bool defaultValue = fals
 48    {
 849        var value = parameters.GetValue(key);
 850        return value?.ToString()?.ToLowerInvariant() switch
 851        {
 352            "true" or "1" or "yes" => true,
 353            "false" or "0" or "no" => false,
 254            _ => defaultValue
 855        };
 56    }
 57
 58    public static int GetInt(this IReadOnlyDictionary<string, object> parameters, string key, int defaultValue = 0)
 59    {
 460        var value = parameters.GetValue(key);
 461        if (value == null) return defaultValue;
 62
 463        if (int.TryParse(value.ToString(), out int result))
 264            return result;
 65
 266        return defaultValue;
 67    }
 68
 69    public static T? GetObject<T>(this IReadOnlyDictionary<string, object> parameters, string key) where T : class
 70    {
 171        var value = parameters.GetValue(key);
 172        if (value == null) return null;
 73
 74        try
 75        {
 176            return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value, _jsonOptions), _jsonOptions);
 77        }
 078        catch
 79        {
 080            return null;
 81        }
 182    }
 83
 84    public static T? GetObject<T>(this JsonElement jsonElement) where T : class
 85    {
 86        try
 87        {
 188            return jsonElement.Deserialize<T>(_jsonOptions);
 89        }
 090        catch
 91        {
 092            return null;
 93        }
 194    }
 95}