Working World roundtrip serialization

There are probably still reference equality issues here since Unit still has Season and Power objects. The test case builder also still works on reference equality in some places so the second part of adjudication is broken.
This commit is contained in:
Tim Van Baak 2024-08-14 09:51:18 -07:00
parent 31bd6a45cb
commit 73d849e117
3 changed files with 8 additions and 7 deletions

View File

@ -88,12 +88,12 @@ public class Map
/// </summary> /// </summary>
public Power GetPower(string powerName) public Power GetPower(string powerName)
=> Powers.SingleOrDefault(p => p!.Name == powerName || p.Name.StartsWith(powerName), null) => Powers.SingleOrDefault(p => p!.Name == powerName || p.Name.StartsWith(powerName), null)
?? throw new KeyNotFoundException($"Power {powerName} not found"); ?? throw new KeyNotFoundException($"Power {powerName} not found (powers: {string.Join(", ", Powers)})");
public static Map FromType(MapType type) public static Map FromType(MapType type)
=> type switch { => type switch {
MapType.Test => Test, MapType.Test => Test,
MapType.Classical => Test, MapType.Classical => Classical,
_ => throw new NotImplementedException($"Unknown variant {type}"), _ => throw new NotImplementedException($"Unknown variant {type}"),
}; };

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace MultiversalDiplomacy.Model; namespace MultiversalDiplomacy.Model;
/// <summary> /// <summary>
@ -33,9 +35,10 @@ public class Unit
/// <summary> /// <summary>
/// A unique designation for this unit. /// A unique designation for this unit.
/// </summary> /// </summary>
[JsonIgnore]
public string Designation => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}"; public string Designation => $"{Type.ToShort()} {Season.Timeline}-{Location}@{Season.Turn}";
private Unit(string? past, string location, Season season, Power power, UnitType type) public Unit(string? past, string location, Season season, Power power, UnitType type)
{ {
this.Past = past; this.Past = past;
this.Location = location; this.Location = location;

View File

@ -64,11 +64,9 @@ public class SerializationTest
Assert.That(tyr0, Is.NotDislodged); Assert.That(tyr0, Is.NotDislodged);
setup.UpdateWorld(); setup.UpdateWorld();
// Serialize the world // Serialize and deserialize the world
string serialized = JsonSerializer.Serialize(setup.World, Options); string serialized = JsonSerializer.Serialize(setup.World, Options);
// Console.WriteLine(serialized);
// Deserialize the world
Console.WriteLine(serialized);
World reserialized = JsonSerializer.Deserialize<World>(serialized, Options) World reserialized = JsonSerializer.Deserialize<World>(serialized, Options)
?? throw new AssertionException("Failed to reserialize world"); ?? throw new AssertionException("Failed to reserialize world");