Skip to content

Commit

Permalink
added more enum mapping and more robust int converter
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanstapel committed Jan 11, 2024
1 parent 9fb503d commit d393aec
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion BrickOwlSharp.Client/BrickOwlSharp.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ With BrickOwlSharp, developers can easily integrate BrickOwl into their applicat
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="7.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions BrickOwlSharp.Client/CatalogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public partial class CatalogItem

[JsonPropertyName("color_name")]
public string ColorName { get; set; }

[JsonPropertyName("color_id"), JsonConverter(typeof(IntStringConverter))]
public int ColorId { get; set; }
public int ColorId { get; set; }

[JsonPropertyName("color_hex")]
public string ColorHex { get; set; }
Expand Down
11 changes: 10 additions & 1 deletion BrickOwlSharp.Client/IdType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public enum IdType
LDraw,
BOID,
ItemNo,
SetNumber
SetNumber,
BLItemNo,
UPC,
EAN
}


Expand All @@ -50,6 +53,9 @@ public static IdType FromString(this IdType _, string s)
case "boid": return IdType.BOID;
case "item_no": return IdType.ItemNo;
case "set_number": return IdType.SetNumber;
case "bl_item_no": return IdType.BLItemNo;
case "ean": return IdType.EAN;
case "upc": return IdType.UPC;
}

return IdType.Unknown;
Expand All @@ -65,6 +71,9 @@ public static string EnumToString(this IdType c)
case IdType.BOID: return "boid";
case IdType.ItemNo: return "item_no";
case IdType.SetNumber: return "set_number";
case IdType.BLItemNo: return "bl_item_no";
case IdType.EAN: return "ean";
case IdType.UPC: return "upc";
}

return "";
Expand Down
22 changes: 19 additions & 3 deletions BrickOwlSharp.Client/Json/IntStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,26 @@ internal class IntStringConverter : JsonConverter<int>
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var stringValue = reader.GetString();
if (Int32.TryParse(stringValue, out int value))
try
{
return value;
var stringValue = reader.GetString();
if (Int32.TryParse(stringValue, out int value))
{
return value;
}
}
catch
{
}

try
{
var intValue = reader.GetInt32();
return intValue;
}
catch
{

}
return default(Int32);
}
Expand Down
14 changes: 14 additions & 0 deletions BrickOwlSharp.Client/Json/ItemTypeStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public override ItemType Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
switch (stringValue)
{
case "Part": return ItemType.Part;
case "Minibuild": return ItemType.Minibuild;
case "Packaging": return ItemType.Packaging;
case "Set": return ItemType.Set;
case "Instructions": return ItemType.Instructions;
case "Gear": return ItemType.Gear;
case "Minifigure": return ItemType.Minifigure;
case "Sticker": return ItemType.Sticker;
}

return ItemType.Unknown;
Expand All @@ -51,6 +58,13 @@ public override void Write(Utf8JsonWriter writer, ItemType value, JsonSerializer
switch (value)
{
case ItemType.Part: typeString = "Part"; break;
case ItemType.Minibuild: typeString = "Minibuild"; break;
case ItemType.Packaging: typeString = "Packaging"; break;
case ItemType.Set: typeString = "Set"; break;
case ItemType.Instructions: typeString = "Instructions"; break;
case ItemType.Gear: typeString = "Gear"; break;
case ItemType.Minifigure: typeString = "Minifigure"; break;
case ItemType.Sticker: typeString = "Sticker"; break;
}


Expand Down

0 comments on commit d393aec

Please sign in to comment.