< Summary

Information
Class: RaidLoop.Core.ItemJsonConverter
Assembly: RaidLoop.Core
File(s): /home/runner/work/RaidLoop/RaidLoop/src/RaidLoop.Core/ItemJsonConverter.cs
Line coverage
100%
Covered lines: 43
Uncovered lines: 0
Coverable lines: 43
Total lines: 84
Line coverage: 100%
Branch coverage
84%
Covered branches: 22
Total branches: 26
Branch coverage: 84.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)66.66%1212100%
Write(...)100%11100%
TryGetString(...)100%44100%
TryGetInt(...)100%66100%
TryGetProperty(...)100%44100%

File(s)

/home/runner/work/RaidLoop/RaidLoop/src/RaidLoop.Core/ItemJsonConverter.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4
 5namespace RaidLoop.Core;
 6
 7internal sealed class ItemJsonConverter : JsonConverter<Item>
 8{
 9    public override Item Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 10    {
 2411        using var document = JsonDocument.ParseValue(ref reader);
 2412        var root = document.RootElement;
 13
 2414        var itemDefId = TryGetInt(root, "itemDefId");
 2415        var itemKey = TryGetString(root, "itemKey");
 2416        var itemName = TryGetString(root, "name");
 17
 2418        if (ItemCatalog.TryResolveAuthoredItem(itemDefId, itemKey, itemName, out var authoredItem) && authoredItem is no
 19        {
 2120            return authoredItem with { };
 21        }
 22
 323        return new Item(
 324            Name: itemName ?? itemKey ?? (itemDefId > 0 ? itemDefId.ToString(CultureInfo.InvariantCulture) : string.Empt
 325            Type: TryGetInt(root, "type", TryGetInt(root, "Type")) is var typeValue ? (ItemType)typeValue : ItemType.Wea
 326            Weight: TryGetInt(root, "weight", TryGetInt(root, "Weight")),
 327            Value: TryGetInt(root, "value", TryGetInt(root, "Value", 1)),
 328            Slots: TryGetInt(root, "slots", TryGetInt(root, "Slots", 1)),
 329            Rarity: (Rarity)TryGetInt(root, "rarity", TryGetInt(root, "Rarity")),
 330            DisplayRarity: (DisplayRarity)TryGetInt(root, "displayRarity", TryGetInt(root, "DisplayRarity")))
 331        {
 332            ItemDefId = itemDefId,
 333            Key = itemKey ?? string.Empty
 334        };
 2435    }
 36
 37    public override void Write(Utf8JsonWriter writer, Item value, JsonSerializerOptions options)
 38    {
 1639        writer.WriteStartObject();
 1640        writer.WriteNumber("itemDefId", value.ItemDefId);
 1641        writer.WriteNumber("type", (int)value.Type);
 1642        writer.WriteNumber("value", value.Value);
 1643        writer.WriteNumber("slots", value.Slots);
 1644        writer.WriteNumber("rarity", (int)value.Rarity);
 1645        writer.WriteNumber("displayRarity", (int)value.DisplayRarity);
 1646        writer.WriteNumber("weight", value.Weight);
 1647        writer.WriteEndObject();
 1648    }
 49
 50    private static string? TryGetString(JsonElement root, string propertyName)
 51    {
 4852        if (TryGetProperty(root, propertyName, out var value) && value.ValueKind == JsonValueKind.String)
 53        {
 1554            return value.GetString();
 55        }
 56
 3357        return null;
 58    }
 59
 60    private static int TryGetInt(JsonElement root, string propertyName, int defaultValue = 0)
 61    {
 6062        if (TryGetProperty(root, propertyName, out var value) && value.ValueKind == JsonValueKind.Number && value.TryGet
 63        {
 3764            return number;
 65        }
 66
 2367        return defaultValue;
 68    }
 69
 70    private static bool TryGetProperty(JsonElement root, string propertyName, out JsonElement value)
 71    {
 103872        foreach (var property in root.EnumerateObject())
 73        {
 43774            if (string.Equals(property.Name, propertyName, StringComparison.OrdinalIgnoreCase))
 75            {
 5276                value = property.Value;
 5277                return true;
 78            }
 79        }
 80
 5681        value = default;
 5682        return false;
 5283    }
 84}