Skip to content

Commit

Permalink
fix: Serialize Geraetemerkmal WITH GAS_ prefix in STJ (instead of w…
Browse files Browse the repository at this point in the history
…/o) (on v0.8.x legacy branch) (#619)

fix: Serialize Geraetemerkmal WITH `GAS_` prefix in STJ (instead of w/o)

Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
hf-kklein and Konstantin authored Dec 10, 2024
1 parent 2f47e9b commit 1ed5ec1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ JsonSerializerOptions options
)
{
var stringValue = value.ToString();
var match = GasPrefixRegex.Match(stringValue);
if (!match.Success)
{
writer.WriteStringValue(stringValue);
return;
}
var rest = match.Groups["rest"].Value;
writer.WriteStringValue(rest);
writer.WriteStringValue(stringValue);
}
}
39 changes: 38 additions & 1 deletion BO4ETestProject/TestGeraetemerkmalConverter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using BO4E.ENUM;
using BO4E.meta.LenientConverters;
Expand Down Expand Up @@ -84,6 +85,27 @@ public void TestNewtonsoft_Success_Nullable()
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G4);
}

[TestMethod]
public void TestNewtonsoft_Success_Nullable_Serialization()
{
var myInstance = new SomethingWithANullableGeraetemerkmal()
{
Merkmal = Geraetemerkmal.GAS_G4,
};
var result = JsonConvert.SerializeObject(
myInstance,
new JsonSerializerSettings()
{
Converters = new List<Newtonsoft.Json.JsonConverter>()
{
new Newtonsoft.Json.Converters.StringEnumConverter(),
new LenientGeraetemerkmalGasConverter(),
},
}
);
result.Should().NotBeNullOrWhiteSpace().And.Contain("\"GAS_G4\"").And.NotContain("\"G4\"");
}

[TestMethod]
public void TestNewtonsoft_Success_Nullable_WASSER_MWZW()
{
Expand Down Expand Up @@ -120,6 +142,21 @@ public void TestSystemText_Success_NonNullable()
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G4);
}

[TestMethod]
public void TestSystemText_Success_NonNullable_Serialization()
{
var myInstance = new SomethingWithANullableGeraetemerkmal()
{
Merkmal = Geraetemerkmal.GAS_G4,
};
var settings = new System.Text.Json.JsonSerializerOptions()
{
Converters = { new LenientSystemTextGeraetemerkmalGasConverter() },
};
var result = System.Text.Json.JsonSerializer.Serialize(myInstance, settings);
result.Should().NotBeNullOrWhiteSpace().And.Contain("\"GAS_G4\"").And.NotContain("\"G4\"");
}

[TestMethod]
public void TestSystemText_Success_NonNullable_WASSER_MWZW()
{
Expand Down Expand Up @@ -172,7 +209,7 @@ public void TestSystemText_Write_NonNullable()
};
var instance = new SomethingWithAGeraetemerkmal { Merkmal = Geraetemerkmal.GAS_G4 };
var json = System.Text.Json.JsonSerializer.Serialize(instance, settings);
json.Should().Be("{\"merkmal\":\"G4\"}");
json.Should().Be($"{{\"merkmal\":\"{Geraetemerkmal.GAS_G4.ToString()}\"}}");
}

[TestMethod]
Expand Down

0 comments on commit 1ed5ec1

Please sign in to comment.