Add more JsonIgnores

This commit is contained in:
Tim Van Baak 2024-08-13 08:17:34 -07:00
parent fd8c725286
commit 984676f587
5 changed files with 61 additions and 11 deletions

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace MultiversalDiplomacy.Model;
/// <summary>
@ -9,8 +11,12 @@ public class Location
{
/// <summary>
/// The province to which this location belongs.
/// </summary>
[JsonIgnore]
public Province Province { get; }
public string ProvinceName => Province.Name;
/// <summary>
/// The location's full human-readable name.
/// </summary>
@ -29,6 +35,7 @@ public class Location
/// <summary>
/// The locations that border this location.
/// </summary>
[JsonIgnore]
public IEnumerable<Location> Adjacents => this.AdjacentList;
private List<Location> AdjacentList { get; set; }

View File

@ -5,7 +5,13 @@ namespace MultiversalDiplomacy.Model;
/// <summary>
/// Encapsulation of the world map and playable powers constituting a Diplomacy variant.
/// </summary>
public class Map {
public class Map
{
/// <summary>
/// The map type.
/// </summary>
public MapType Type { get; }
/// <summary>
/// The game map.
/// </summary>
@ -16,7 +22,7 @@ public class Map {
/// </summary>
public ReadOnlyCollection<Power> Powers { get; }
private Map(IEnumerable<Province> provinces, IEnumerable<Power> powers)
private Map(MapType type, IEnumerable<Province> provinces, IEnumerable<Power> powers)
{
Provinces = new(provinces.ToList());
Powers = new(powers.ToList());
@ -102,7 +108,7 @@ public class Map {
Power a = new("Alpha");
Power b = new("Beta");
return new([lef, cen, rig], [a, b]);
return new(MapType.Test, [lef, cen, rig], [a, b]);
});
public static Map Classical => _Classical.Value;
@ -553,7 +559,7 @@ public class Map {
new("Turkey"),
];
return new(provinces, powers);
return new(MapType.Classical, provinces, powers);
});
#endregion Variants

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace MultiversalDiplomacy.Model;
/// <summary>
@ -18,6 +20,7 @@ public class Unit
/// <summary>
/// The province where the unit is.
/// </summary>
[JsonIgnore]
public Province Province => this.Location.Province;
/// <summary>
@ -59,11 +62,11 @@ public class Unit
/// method after accepting a build order.
/// </summary>
public static Unit Build(Location location, Season season, Power power, UnitType type)
=> new Unit(past: null, location, season, power, type);
=> new(past: null, location, season, power, type);
/// <summary>
/// Advance this unit's timeline to a new location and season.
/// </summary>
public Unit Next(Location location, Season season)
=> new Unit(past: this, location, season, this.Power, this.Type);
=> new(past: this, location, season, this.Power, this.Type);
}

View File

@ -1,6 +1,5 @@
using System.Collections.ObjectModel;
using MultiversalDiplomacy.Orders;
using System.Text.Json.Serialization;
namespace MultiversalDiplomacy.Model;
@ -12,16 +11,24 @@ public class World
/// <summary>
/// The map variant of the game.
/// </summary>
[JsonIgnore]
public Map Map { get; }
/// <summary>
/// The map variant of the game.
/// </summary>
public MapType MapType => this.Map.Type;
/// <summary>
/// The game map.
/// </summary>
[JsonIgnore]
public ReadOnlyCollection<Province> Provinces => this.Map.Provinces;
/// <summary>
/// The game powers.
/// </summary>
[JsonIgnore]
public ReadOnlyCollection<Power> Powers => this.Map.Powers;
/// <summary>
@ -32,11 +39,13 @@ public class World
/// <summary>
/// Lookup for seasons by designation.
/// </summary>
[JsonIgnore]
public ReadOnlyDictionary<string, Season> SeasonLookup { get; }
/// <summary>
/// The first season of the game.
/// </summary>
[JsonIgnore]
public Season RootSeason => GetSeason("a0");
/// <summary>
@ -219,9 +228,7 @@ public class World
/// <summary>
/// A standard Diplomacy game setup.
/// </summary>
public static World Standard => World
.WithStandardMap()
.AddStandardUnits();
public static World Standard => WithStandardMap().AddStandardUnits();
/// <summary>
/// Get a season by coordinate. Throws if the season is not found.

View File

@ -9,6 +9,21 @@ namespace MultiversalDiplomacyTests;
public class SerializationTest
{
[Test]
public void SerializeRoundTrip_NewGame()
{
JsonSerializerOptions options = new() {
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
World world = World.WithStandardMap();
string serialized = JsonSerializer.Serialize(world, options);
Console.WriteLine(serialized);
World? deserialized = JsonSerializer.Deserialize<World>(serialized, options);
Assert.That(deserialized, Is.Not.Null, "Failed to deserialize");
}
[Test]
public void SerializeRoundTrip_MDATC_3_A_2()
{
@ -32,9 +47,21 @@ public class SerializationTest
// Serialize the world
JsonSerializerOptions options = new() {
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
JsonElement serialized = JsonSerializer.SerializeToDocument(setup.World, options).RootElement;
Assert.That(
serialized.EnumerateObject().Select(prop => prop.Name),
Is.EquivalentTo(new List<string> {
"mapType",
"seasons",
"units",
"retreatingUnits",
"orderHistory",
"options",
}));
// Deserialize the world
World reserialized = JsonSerializer.Deserialize<World>(serialized)
?? throw new AssertionException("Failed to reserialize world");