From 58f877425a4563f3f17df6508101dbfa51174af1 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 13 Aug 2024 08:17:34 -0700 Subject: [PATCH] Add more JsonIgnores --- MultiversalDiplomacy/Model/Location.cs | 7 +++++ MultiversalDiplomacy/Model/Map.cs | 14 +++++++--- MultiversalDiplomacy/Model/Unit.cs | 7 +++-- MultiversalDiplomacy/Model/World.cs | 17 ++++++++---- .../SerializationTest.cs | 27 +++++++++++++++++++ 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/MultiversalDiplomacy/Model/Location.cs b/MultiversalDiplomacy/Model/Location.cs index fe0aae9..ff4d365 100644 --- a/MultiversalDiplomacy/Model/Location.cs +++ b/MultiversalDiplomacy/Model/Location.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace MultiversalDiplomacy.Model; /// @@ -9,8 +11,12 @@ public class Location { /// /// The province to which this location belongs. + /// + [JsonIgnore] public Province Province { get; } + public string ProvinceName => Province.Name; + /// /// The location's full human-readable name. /// @@ -29,6 +35,7 @@ public class Location /// /// The locations that border this location. /// + [JsonIgnore] public IEnumerable Adjacents => this.AdjacentList; private List AdjacentList { get; set; } diff --git a/MultiversalDiplomacy/Model/Map.cs b/MultiversalDiplomacy/Model/Map.cs index 1ce0c57..ba1ae80 100644 --- a/MultiversalDiplomacy/Model/Map.cs +++ b/MultiversalDiplomacy/Model/Map.cs @@ -5,7 +5,13 @@ namespace MultiversalDiplomacy.Model; /// /// Encapsulation of the world map and playable powers constituting a Diplomacy variant. /// -public class Map { +public class Map +{ + /// + /// The map type. + /// + public MapType Type { get; } + /// /// The game map. /// @@ -16,7 +22,7 @@ public class Map { /// public ReadOnlyCollection Powers { get; } - private Map(IEnumerable provinces, IEnumerable powers) + private Map(MapType type, IEnumerable provinces, IEnumerable 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 diff --git a/MultiversalDiplomacy/Model/Unit.cs b/MultiversalDiplomacy/Model/Unit.cs index aebeb3f..736ec33 100644 --- a/MultiversalDiplomacy/Model/Unit.cs +++ b/MultiversalDiplomacy/Model/Unit.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace MultiversalDiplomacy.Model; /// @@ -18,6 +20,7 @@ public class Unit /// /// The province where the unit is. /// + [JsonIgnore] public Province Province => this.Location.Province; /// @@ -59,11 +62,11 @@ public class Unit /// method after accepting a build order. /// 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); /// /// Advance this unit's timeline to a new location and 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); } diff --git a/MultiversalDiplomacy/Model/World.cs b/MultiversalDiplomacy/Model/World.cs index aa3d716..bce3d82 100644 --- a/MultiversalDiplomacy/Model/World.cs +++ b/MultiversalDiplomacy/Model/World.cs @@ -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 /// /// The map variant of the game. /// + [JsonIgnore] public Map Map { get; } + /// + /// The map variant of the game. + /// + public MapType MapType => this.Map.Type; + /// /// The game map. /// + [JsonIgnore] public ReadOnlyCollection Provinces => this.Map.Provinces; /// /// The game powers. /// + [JsonIgnore] public ReadOnlyCollection Powers => this.Map.Powers; /// @@ -32,11 +39,13 @@ public class World /// /// Lookup for seasons by designation. /// + [JsonIgnore] public ReadOnlyDictionary SeasonLookup { get; } /// /// The first season of the game. /// + [JsonIgnore] public Season RootSeason => GetSeason("a0"); /// @@ -219,9 +228,7 @@ public class World /// /// A standard Diplomacy game setup. /// - public static World Standard => World - .WithStandardMap() - .AddStandardUnits(); + public static World Standard => WithStandardMap().AddStandardUnits(); /// /// Get a season by coordinate. Throws if the season is not found. diff --git a/MultiversalDiplomacyTests/SerializationTest.cs b/MultiversalDiplomacyTests/SerializationTest.cs index 3d64156..aff12e0 100644 --- a/MultiversalDiplomacyTests/SerializationTest.cs +++ b/MultiversalDiplomacyTests/SerializationTest.cs @@ -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(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 { + "mapType", + "seasons", + "units", + "retreatingUnits", + "orderHistory", + "options", + })); + // Deserialize the world World reserialized = JsonSerializer.Deserialize(serialized) ?? throw new AssertionException("Failed to reserialize world");