Add more JsonIgnores
This commit is contained in:
parent
fd8c725286
commit
984676f587
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace MultiversalDiplomacy.Model;
|
namespace MultiversalDiplomacy.Model;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -9,8 +11,12 @@ public class Location
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The province to which this location belongs.
|
/// The province to which this location belongs.
|
||||||
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public Province Province { get; }
|
public Province Province { get; }
|
||||||
|
|
||||||
|
public string ProvinceName => Province.Name;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The location's full human-readable name.
|
/// The location's full human-readable name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -29,6 +35,7 @@ public class Location
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The locations that border this location.
|
/// The locations that border this location.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public IEnumerable<Location> Adjacents => this.AdjacentList;
|
public IEnumerable<Location> Adjacents => this.AdjacentList;
|
||||||
private List<Location> AdjacentList { get; set; }
|
private List<Location> AdjacentList { get; set; }
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,13 @@ namespace MultiversalDiplomacy.Model;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Encapsulation of the world map and playable powers constituting a Diplomacy variant.
|
/// Encapsulation of the world map and playable powers constituting a Diplomacy variant.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Map {
|
public class Map
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The map type.
|
||||||
|
/// </summary>
|
||||||
|
public MapType Type { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The game map.
|
/// The game map.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -16,7 +22,7 @@ public class Map {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ReadOnlyCollection<Power> Powers { get; }
|
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());
|
Provinces = new(provinces.ToList());
|
||||||
Powers = new(powers.ToList());
|
Powers = new(powers.ToList());
|
||||||
|
@ -102,7 +108,7 @@ public class Map {
|
||||||
Power a = new("Alpha");
|
Power a = new("Alpha");
|
||||||
Power b = new("Beta");
|
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;
|
public static Map Classical => _Classical.Value;
|
||||||
|
@ -553,7 +559,7 @@ public class Map {
|
||||||
new("Turkey"),
|
new("Turkey"),
|
||||||
];
|
];
|
||||||
|
|
||||||
return new(provinces, powers);
|
return new(MapType.Classical, provinces, powers);
|
||||||
});
|
});
|
||||||
|
|
||||||
#endregion Variants
|
#endregion Variants
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace MultiversalDiplomacy.Model;
|
namespace MultiversalDiplomacy.Model;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -18,6 +20,7 @@ public class Unit
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The province where the unit is.
|
/// The province where the unit is.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public Province Province => this.Location.Province;
|
public Province Province => this.Location.Province;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -59,11 +62,11 @@ public class Unit
|
||||||
/// method after accepting a build order.
|
/// method after accepting a build order.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static Unit Build(Location location, Season season, Power power, UnitType type)
|
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>
|
/// <summary>
|
||||||
/// Advance this unit's timeline to a new location and season.
|
/// Advance this unit's timeline to a new location and season.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Unit Next(Location location, Season season)
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using MultiversalDiplomacy.Orders;
|
|
||||||
|
|
||||||
namespace MultiversalDiplomacy.Model;
|
namespace MultiversalDiplomacy.Model;
|
||||||
|
|
||||||
|
@ -12,16 +11,24 @@ public class World
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The map variant of the game.
|
/// The map variant of the game.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public Map Map { get; }
|
public Map Map { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The map variant of the game.
|
||||||
|
/// </summary>
|
||||||
|
public MapType MapType => this.Map.Type;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The game map.
|
/// The game map.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public ReadOnlyCollection<Province> Provinces => this.Map.Provinces;
|
public ReadOnlyCollection<Province> Provinces => this.Map.Provinces;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The game powers.
|
/// The game powers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public ReadOnlyCollection<Power> Powers => this.Map.Powers;
|
public ReadOnlyCollection<Power> Powers => this.Map.Powers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -32,11 +39,13 @@ public class World
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lookup for seasons by designation.
|
/// Lookup for seasons by designation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public ReadOnlyDictionary<string, Season> SeasonLookup { get; }
|
public ReadOnlyDictionary<string, Season> SeasonLookup { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The first season of the game.
|
/// The first season of the game.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonIgnore]
|
||||||
public Season RootSeason => GetSeason("a0");
|
public Season RootSeason => GetSeason("a0");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -219,9 +228,7 @@ public class World
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A standard Diplomacy game setup.
|
/// A standard Diplomacy game setup.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static World Standard => World
|
public static World Standard => WithStandardMap().AddStandardUnits();
|
||||||
.WithStandardMap()
|
|
||||||
.AddStandardUnits();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a season by coordinate. Throws if the season is not found.
|
/// Get a season by coordinate. Throws if the season is not found.
|
||||||
|
|
|
@ -9,6 +9,21 @@ namespace MultiversalDiplomacyTests;
|
||||||
|
|
||||||
public class SerializationTest
|
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]
|
[Test]
|
||||||
public void SerializeRoundTrip_MDATC_3_A_2()
|
public void SerializeRoundTrip_MDATC_3_A_2()
|
||||||
{
|
{
|
||||||
|
@ -32,9 +47,21 @@ public class SerializationTest
|
||||||
// Serialize the world
|
// Serialize the world
|
||||||
JsonSerializerOptions options = new() {
|
JsonSerializerOptions options = new() {
|
||||||
WriteIndented = true,
|
WriteIndented = true,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
};
|
};
|
||||||
JsonElement serialized = JsonSerializer.SerializeToDocument(setup.World, options).RootElement;
|
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
|
// Deserialize the world
|
||||||
World reserialized = JsonSerializer.Deserialize<World>(serialized)
|
World reserialized = JsonSerializer.Deserialize<World>(serialized)
|
||||||
?? throw new AssertionException("Failed to reserialize world");
|
?? throw new AssertionException("Failed to reserialize world");
|
||||||
|
|
Loading…
Reference in New Issue