Skip to content

Commit

Permalink
fix: Support Deserializing Geraetemerkmal "G2.5", not only `"G2Peri…
Browse files Browse the repository at this point in the history
…od5"` (#584)

fix: Support Deserializing Geraetemerkmal`"G2.5"`, not only `"G2Period5"`

Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
hf-kklein and Konstantin authored Nov 14, 2024
1 parent 26f05f9 commit a367fbd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ JsonSerializer serializer
}
catch (ArgumentException) when (rawValue.StartsWith("G"))
{
if (rawValue == "G2Period5")
switch (rawValue)
{
return Geraetemerkmal.GAS_G2P5;
case "G2Period5":
case "G2.5":
return Geraetemerkmal.GAS_G2P5;
default:
return Enums.Parse<Geraetemerkmal>("GAS_" + rawValue);
}
return Enums.Parse<Geraetemerkmal>("GAS_" + rawValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ JsonSerializerOptions options
}
catch (ArgumentException) when (rawString.StartsWith("G"))
{
if (rawString == "G2Period5")
switch (rawString)
{
return Geraetemerkmal.GAS_G2P5;
case "G2Period5":
case "G2.5":
return Geraetemerkmal.GAS_G2P5;
default:
return Enums.Parse<Geraetemerkmal>("GAS_" + rawString);
}
return Enums.Parse<Geraetemerkmal>("GAS_" + rawString);
}
}

Expand Down
41 changes: 41 additions & 0 deletions BO4ETestProject/TestGeraetemerkmalConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class TestGeraeteerkmalDeserialization
{
private const string JsonString = "{\"merkmal\":\"G4\"}";
private const string JsonStringWithPeriod = "{\"merkmal\":\"G2Period5\"}";
private const string JsonStringWithLiteralFullstop = "{\"merkmal\":\"G2.5\"}";
private const string JsonStringWasser = "{\"merkmal\":\"WASSER_MWZW\"}";

internal class SomethingWithAGeraetemerkmal
Expand Down Expand Up @@ -163,6 +164,46 @@ public void TestSystemText_Nullable_Success_GPointSomething()
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G2P5);
}

[TestMethod]
public void TestNewtonsoft_Success_Nullable_With_Literal_Fullstop()
{
var result =
Newtonsoft.Json.JsonConvert.DeserializeObject<SomethingWithANullableGeraetemerkmal>(
JsonStringWithLiteralFullstop,
new LenientGeraetemerkmalGasConverter()
);
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G2P5);
}

[TestMethod]
public void TestSystemText_Success_With_Literal_Fullstop()
{
var settings = new System.Text.Json.JsonSerializerOptions()
{
Converters = { new LenientSystemTextGeraetemerkmalGasConverter() },
};
var result = System.Text.Json.JsonSerializer.Deserialize<SomethingWithAGeraetemerkmal>(
JsonStringWithLiteralFullstop,
settings
);
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G2P5);
}

[TestMethod]
public void TestSystemText_Success_With_Nullable_Literal_Fullstop()
{
var settings = new System.Text.Json.JsonSerializerOptions()
{
Converters = { new LenientSystemTextGeraetemerkmalGasConverter() },
};
var result =
System.Text.Json.JsonSerializer.Deserialize<SomethingWithANullableGeraetemerkmal>(
JsonStringWithLiteralFullstop,
settings
);
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G2P5);
}

[TestMethod]
public void TestSystemText_Write_NonNullable()
{
Expand Down

0 comments on commit a367fbd

Please sign in to comment.