Skip to content

Commit

Permalink
Implement Write(...) method for Geraetemerkmal System.Text.Json Con…
Browse files Browse the repository at this point in the history
…verters (#447)
  • Loading branch information
hf-kklein authored Jun 11, 2024
1 parent 3b4f1f5 commit f7dc51a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace BO4E.meta.LenientConverters
{
Expand Down Expand Up @@ -35,12 +36,25 @@ public override Geraetemerkmal Read(ref Utf8JsonReader reader, Type typeToConver
}
}

/// <summary>
/// https://regex101.com/r/dAUAHL/1
/// </summary>
private static readonly Regex GasPrefixRegex = new(@"^(?<praefix>(?:GAS_)?)(?<rest>.+)$", RegexOptions.Compiled);

/// <summary>
/// <inheritdoc cref="System.Text.Json.Serialization.JsonConverter{T}.Write"/>
/// </summary>
public override void Write(Utf8JsonWriter writer, Geraetemerkmal value, JsonSerializerOptions options)
{
throw new NotImplementedException();
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);
}
}

Expand Down Expand Up @@ -78,12 +92,33 @@ public class LenientSystemTextNullableGeraetemerkmalGasConverter : System.Text.J
}
}

/// <summary>
/// https://regex101.com/r/dAUAHL/1
/// </summary>
private static readonly Regex GasPrefixRegex = new(@"^(?<praefix>(?:GAS_)?)(?<rest>.+)$", RegexOptions.Compiled);

/// <summary>
/// <inheritdoc cref="System.Text.Json.Serialization.JsonConverter{T}.Write"/>
/// </summary>
public override void Write(Utf8JsonWriter writer, Geraetemerkmal? value, JsonSerializerOptions options)
{
throw new NotImplementedException();
if (value.HasValue)
{
// Remove the "GAS_" prefix if it exists
var stringValue = value.Value.ToString();
var match = GasPrefixRegex.Match(stringValue);
if (!match.Success)
{
writer.WriteStringValue(stringValue);
return;
}
var rest = match.Groups["rest"].Value;
writer.WriteStringValue(rest);
}
else
{
writer.WriteNullValue();
}
}
}
}
37 changes: 36 additions & 1 deletion BO4ETestProject/TestGeraetemerkmalConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,41 @@ public void TestSystemText_Nullable_Success_GPointSomething()
result.Merkmal.Should().Be(Geraetemerkmal.GAS_G2P5);
}

// todo: someone should write the system.text equivalent once there's time.

[TestMethod]
public void TestSystemText_Write_NonNullable()
{
var settings = new System.Text.Json.JsonSerializerOptions()
{
Converters = { new LenientSystemTextGeraetemerkmalGasConverter() }
};
var instance = new SomethingWithAGeraetemerkmal { Merkmal = Geraetemerkmal.GAS_G4 };
var json = System.Text.Json.JsonSerializer.Serialize(instance, settings);
json.Should().Be("{\"merkmal\":\"G4\"}");
}

[TestMethod]
public void TestSystemText_Write_Nullable()
{
var settings = new System.Text.Json.JsonSerializerOptions()
{
Converters = { new LenientSystemTextNullableGeraetemerkmalGasConverter() }
};
var instance = new SomethingWithANullableGeraetemerkmal { Merkmal = Geraetemerkmal.GAS_G2P5 };
var json = System.Text.Json.JsonSerializer.Serialize(instance, settings);
json.Should().Be("{\"merkmal\":\"G2P5\"}");
}

[TestMethod]
public void TestSystemText_Write_Nullable_Null()
{
var settings = new System.Text.Json.JsonSerializerOptions()
{
Converters = { new LenientSystemTextNullableGeraetemerkmalGasConverter() }
};
var instance = new SomethingWithANullableGeraetemerkmal { Merkmal = null };
var json = System.Text.Json.JsonSerializer.Serialize(instance, settings);
json.Should().Be("{\"merkmal\":null}");
}
}
}

0 comments on commit f7dc51a

Please sign in to comment.