Skip to content

Commit

Permalink
fix: Also support h_GAS with lower case h in custom JSON converte…
Browse files Browse the repository at this point in the history
…rs (#529)

* fix: Also support`h_GAS` with lower case `h` in custom JSON converters

fixes
System.Text.Json.JsonException: Invalid value for System.Nullable`1[BO4E.ENUM.Gasqualitaet]: h_GAS
   at BO4E.meta.LenientConverters.SystemTextGasqualitaetStringEnumConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in D:\a\BO4E-dotnet\BO4E-dotnet\BO4E\meta\LenientConverters\GasqualitaetStringEnumConverter.cs:line 29


---------

Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
hf-kklein and Konstantin authored Sep 23, 2024
1 parent 1617747 commit 7cd7e2a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ System.Text.Json.JsonSerializerOptions options
}
string enumString = reader.GetString();

return enumString switch
return enumString?.ToUpper() switch
{
"HGAS" => Gasqualitaet.H_GAS,
"LGAS" => Gasqualitaet.L_GAS,
_ => Enum.TryParse(enumString, out Gasqualitaet result)
_ => Enum.TryParse(enumString?.ToUpper(), out Gasqualitaet result)
? result
: throw new System.Text.Json.JsonException(
$"Invalid value for {typeToConvert}: {enumString}"
Expand Down Expand Up @@ -96,11 +96,11 @@ Newtonsoft.Json.JsonSerializer serializer
{
string enumString = reader.Value.ToString();

return enumString switch
return enumString?.ToUpper() switch
{
"HGAS" => Gasqualitaet.H_GAS,
"LGAS" => Gasqualitaet.L_GAS,
_ => Enum.TryParse(enumString, out Gasqualitaet result)
_ => Enum.TryParse(enumString?.ToUpper(), out Gasqualitaet result)
? result
: throw new Newtonsoft.Json.JsonSerializationException(
$"Invalid value for {objectType}: {enumString}"
Expand Down
4 changes: 4 additions & 0 deletions BO4ETestProject/TestStringEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class ClassWithNonNullableGasqualitaet

[TestMethod]
[DataRow("HGAS", Gasqualitaet.H_GAS)]
[DataRow("h_GAS", Gasqualitaet.H_GAS)]
[DataRow("H_GAS", Gasqualitaet.H_GAS)]
[DataRow("LGAS", Gasqualitaet.L_GAS)]
[DataRow("L_GAS", Gasqualitaet.L_GAS)]
Expand Down Expand Up @@ -148,6 +149,7 @@ public void Test_System_Text_Gasqualitaet_Legacy_Converter_With_Nullable_Gasqual

[TestMethod]
[DataRow("HGAS", Gasqualitaet.H_GAS)]
[DataRow("h_GAS", Gasqualitaet.H_GAS)]
[DataRow("H_GAS", Gasqualitaet.H_GAS)]
[DataRow("LGAS", Gasqualitaet.L_GAS)]
[DataRow("L_GAS", Gasqualitaet.L_GAS)]
Expand Down Expand Up @@ -183,6 +185,7 @@ Gasqualitaet expectedGasqualitaet
[TestMethod]
[DataRow("HGAS", Gasqualitaet.H_GAS)]
[DataRow("H_GAS", Gasqualitaet.H_GAS)]
[DataRow("h_GAS", Gasqualitaet.H_GAS)]
[DataRow("LGAS", Gasqualitaet.L_GAS)]
[DataRow("L_GAS", Gasqualitaet.L_GAS)]
[DataRow(null, null)]
Expand Down Expand Up @@ -223,6 +226,7 @@ public void Test_Newtonsoft_Gasqualitaet_Legacy_Converter_With_Nullable_Gasquali
[TestMethod]
[DataRow("HGAS", Gasqualitaet.H_GAS)]
[DataRow("H_GAS", Gasqualitaet.H_GAS)]
[DataRow("h_GAS", Gasqualitaet.H_GAS)]
[DataRow("LGAS", Gasqualitaet.L_GAS)]
[DataRow("L_GAS", Gasqualitaet.L_GAS)]
public void Test_Newtonsoft_Gasqualitaet_Legacy_Converter_With_Non_Nullable_Gasqualitaet(
Expand Down

0 comments on commit 7cd7e2a

Please sign in to comment.