✅ Serializing/Deserializing a Dictionary with a struct key not working
I have been pulling my hair out to support this library that uses a struct key. Since the Dictionary uses a weird key, I have to change the serialization. The class is as follows:
public sealed class BCCCodebase{ public Collection<FeatureBase> Features { get; } = []; public Dictionary<BCCNamespace, BCCCodebase> Namespaces { get; } = [];}public readonly struct BCCNamespace( bool isStrict, string? name) : IEquatable<BCCNamespace>{ public bool IsStrict { get; } = isStrict; public string? Name { get; } = name; // Equality methods follow}// This class is additionally inherited with various features.public abstract class BCCFeatureBase( bool isPrivate) : FeatureBase{ public bool IsPrivate { get; } = isPrivate;}public abstract class FeatureBase{ public JavadocComment? Comment { get; internal set; }}
public sealed class BCCCodebase{ public Collection<FeatureBase> Features { get; } = []; public Dictionary<BCCNamespace, BCCCodebase> Namespaces { get; } = [];}public readonly struct BCCNamespace( bool isStrict, string? name) : IEquatable<BCCNamespace>{ public bool IsStrict { get; } = isStrict; public string? Name { get; } = name; // Equality methods follow}// This class is additionally inherited with various features.public abstract class BCCFeatureBase( bool isPrivate) : FeatureBase{ public bool IsPrivate { get; } = isPrivate;}public abstract class FeatureBase{ public JavadocComment? Comment { get; internal set; }}
My test is written as followed (some code removed for readability):
internal sealed class BCCParserTests{ private JsonSerializerOptions _jsonSerializerOptions; [OneTimeSetUp] public void SetUp() { this._jsonSerializerOptions = new(); this._jsonSerializerOptions.Converters.Add(new BCCNamespaceJsonConverter()); } [Test] [TestCase("Parsing_File_Returns_Features_Input1")] public async Task Parsing_File_Returns_Features(string fileName) { var parser = ActivatorUtilities.CreateInstance<BCCParser>(this._serviceProvider); await parser.ParseFileAsync($"TestFiles/BCC/{fileName}.acs"); var featuresJson = JsonSerializer.Serialize(parser.BaseCodebase, this._jsonSerializerOptions); _ = await VerifyJson(featuresJson, this._verifySettings); }}
internal sealed class BCCParserTests{ private JsonSerializerOptions _jsonSerializerOptions; [OneTimeSetUp] public void SetUp() { this._jsonSerializerOptions = new(); this._jsonSerializerOptions.Converters.Add(new BCCNamespaceJsonConverter()); } [Test] [TestCase("Parsing_File_Returns_Features_Input1")] public async Task Parsing_File_Returns_Features(string fileName) { var parser = ActivatorUtilities.CreateInstance<BCCParser>(this._serviceProvider); await parser.ParseFileAsync($"TestFiles/BCC/{fileName}.acs"); var featuresJson = JsonSerializer.Serialize(parser.BaseCodebase, this._jsonSerializerOptions); _ = await VerifyJson(featuresJson, this._verifySettings); }}
The test here fails because
Features
Features
is incorrectly filled with just the comment from
FeatureBase
FeatureBase
and the rest seems to be skipped. Before this I didn't serialize the object to JSON so it even breaks without