Removed empty objects from json serialization with equality instead of post-hoc string replace - kudos @SomeTroglodyte

This commit is contained in:
yairm210 2024-08-07 01:06:50 +03:00
parent 2b81172d20
commit 5eddb505d2
3 changed files with 17 additions and 9 deletions

View File

@ -428,14 +428,7 @@ class UncivFiles(
game.version = GameInfo.CURRENT_COMPATIBILITY_VERSION
if (updateChecksum) game.checksum = game.calculateChecksum()
var plainJson = json().toJson(game)
val removeableStrings = listOf(
",\"history\":{}", // empty history object in tile
",\"promotions\":{}", // empty promotions object in unit
)
for (removableString in removeableStrings)
plainJson = plainJson.replace(removableString,"")
val plainJson = json().toJson(game)
return if (forceZip ?: saveZipped) Gzip.zip(plainJson) else plainJson
}

View File

@ -137,4 +137,10 @@ class UnitPromotions : IsPartOfGameInfoSerialization {
toReturn.unit = unit
return toReturn
}
// For json serialization, to not serialize an empty object
override fun equals(other: Any?): Boolean {
if (other !is UnitPromotions) return false
return XP == other.XP && promotions == other.promotions && numberOfPromotions == other.numberOfPromotions
}
}

View File

@ -31,7 +31,7 @@ class TileHistory(
companion object {
fun deserialize(s: String): CityCenterType =
values().firstOrNull { it.serializedRepresentation == s } ?: None
entries.firstOrNull { it.serializedRepresentation == s } ?: None
}
}
@ -91,4 +91,13 @@ class TileHistory(
@VisibleForTesting
override fun iterator() = history.iterator()
// For json serialization, to not serialize an empty object
override fun equals(other: Any?): Boolean {
if (other !is TileHistory) return false
if (history == other.history) return true
return history.size == other.history.size && history.entries.all { (turn, state) ->
state == other.history[turn]
}
}
}