-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from chickensoft-games/feat/better-derived-types
fix: serialize based on runtime type to support interfaces for serializable model property types
- Loading branch information
Showing
5 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace Chickensoft.Serialization.Tests; | ||
|
||
using System.Text.Json; | ||
using Chickensoft.Collections; | ||
using Chickensoft.Introspection; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
public partial class InterfaceTest { | ||
public interface IAliasedModel { | ||
int Value { get; } | ||
} | ||
|
||
[Meta, Id("aliased_model")] | ||
public partial class AliasedModel : IAliasedModel { | ||
[Save("value")] | ||
public int Value { get; set; } | ||
} | ||
|
||
[Meta, Id("interface_test_model")] | ||
public partial class TestModel { | ||
[Save("aliased_model")] | ||
public required IAliasedModel AliasedModel { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void SerializesAsInterface() { | ||
var model = new TestModel { | ||
AliasedModel = new AliasedModel { Value = 10 } | ||
}; | ||
|
||
var options = new JsonSerializerOptions { | ||
WriteIndented = true, | ||
TypeInfoResolver = new SerializableTypeResolver(), | ||
Converters = { new SerializableTypeConverter(new Blackboard()) } | ||
}; | ||
|
||
var serialized = JsonSerializer.Serialize(model, options); | ||
|
||
serialized.ShouldBe( | ||
/*lang=json,strict*/ | ||
""" | ||
{ | ||
"$type": "interface_test_model", | ||
"$v": 1, | ||
"aliased_model": { | ||
"$type": "aliased_model", | ||
"$v": 1, | ||
"value": 10 | ||
} | ||
} | ||
""", | ||
StringCompareShould.IgnoreLineEndings | ||
); | ||
|
||
var deserialized = JsonSerializer.Deserialize<TestModel>( | ||
serialized, options | ||
); | ||
|
||
deserialized.ShouldNotBeNull(); | ||
deserialized.AliasedModel.ShouldBeEquivalentTo(model.AliasedModel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters