< 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: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 82
Line coverage: 100%
Branch coverage
86%
Covered branches: 19
Total branches: 22
Branch coverage: 86.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)62.5%88100%
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 itemName = TryGetString(root, "name");
 16
 2417        if (ItemCatalog.TryResolveAuthoredItem(itemDefId, out var authoredItem) && authoredItem is not null)
 18        {
 1119            return authoredItem with { };
 20        }
 21
 1322        return new Item(
 1323            Name: itemName ?? (itemDefId > 0 ? itemDefId.ToString(CultureInfo.InvariantCulture) : string.Empty),
 1324            Type: TryGetInt(root, "type", TryGetInt(root, "Type")) is var typeValue ? (ItemType)typeValue : ItemType.Wea
 1325            Weight: TryGetInt(root, "weight", TryGetInt(root, "Weight")),
 1326            Value: TryGetInt(root, "value", TryGetInt(root, "Value", 1)),
 1327            Slots: TryGetInt(root, "slots", TryGetInt(root, "Slots", 1)),
 1328            Rarity: (Rarity)TryGetInt(root, "rarity", TryGetInt(root, "Rarity")),
 1329            DisplayRarity: (DisplayRarity)TryGetInt(root, "displayRarity", TryGetInt(root, "DisplayRarity")))
 1330        {
 1331            ItemDefId = itemDefId
 1332        };
 2433    }
 34
 35    public override void Write(Utf8JsonWriter writer, Item value, JsonSerializerOptions options)
 36    {
 1637        writer.WriteStartObject();
 1638        writer.WriteNumber("itemDefId", value.ItemDefId);
 1639        writer.WriteNumber("type", (int)value.Type);
 1640        writer.WriteNumber("value", value.Value);
 1641        writer.WriteNumber("slots", value.Slots);
 1642        writer.WriteNumber("rarity", (int)value.Rarity);
 1643        writer.WriteNumber("displayRarity", (int)value.DisplayRarity);
 1644        writer.WriteNumber("weight", value.Weight);
 1645        writer.WriteEndObject();
 1646    }
 47
 48    private static string? TryGetString(JsonElement root, string propertyName)
 49    {
 2450        if (TryGetProperty(root, propertyName, out var value) && value.ValueKind == JsonValueKind.String)
 51        {
 1452            return value.GetString();
 53        }
 54
 1055        return null;
 56    }
 57
 58    private static int TryGetInt(JsonElement root, string propertyName, int defaultValue = 0)
 59    {
 18060        if (TryGetProperty(root, propertyName, out var value) && value.ValueKind == JsonValueKind.Number && value.TryGet
 61        {
 13162            return number;
 63        }
 64
 4965        return defaultValue;
 66    }
 67
 68    private static bool TryGetProperty(JsonElement root, string propertyName, out JsonElement value)
 69    {
 189970        foreach (var property in root.EnumerateObject())
 71        {
 81872            if (string.Equals(property.Name, propertyName, StringComparison.OrdinalIgnoreCase))
 73            {
 14574                value = property.Value;
 14575                return true;
 76            }
 77        }
 78
 5979        value = default;
 5980        return false;
 14581    }
 82}