Skip to content

Commit

Permalink
Make Metric JSON format backward compatible (#2318)
Browse files Browse the repository at this point in the history
The change in #2312 mistakenly broke scenario "save" (best score) compatibility.
This customizes the JSON format and supports the original one.
  • Loading branch information
xsebek authored Feb 9, 2025
1 parent 08afe6a commit 29e9105
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/swarm-engine/Swarm/Game/Scenario/Scoring/GenericMetrics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ module Swarm.Game.Scenario.Scoring.GenericMetrics (
chooseBetter,
) where

import Control.Applicative ((<|>))
import Control.Lens
import Data.Aeson
import Data.List.Extra (dropPrefix)
import Data.Ord (Down (Down))
import GHC.Generics (Generic)
import Swarm.Util (maxOn)
import Swarm.Util.JSON (optionsUntagged)
import Swarm.Util.JSON (optionsMinimize, optionsUntagged)
import Swarm.Util.Lens (makeLensesNoSigs)

-- | This is a subset of the "ScenarioStatus" type
Expand All @@ -38,7 +40,18 @@ data Metric a = Metric
{ _metricProgress :: Progress
, _metricData :: a
}
deriving (Eq, Ord, Show, Read, Generic, FromJSON, ToJSON)
deriving (Eq, Ord, Show, Read, Generic)

metricSerializeOptions :: Options
metricSerializeOptions = optionsMinimize {fieldLabelModifier = camelTo2 '_' . dropPrefix "_metric"}

instance FromJSON a => FromJSON (Metric a) where
parseJSON v =
(uncurry Metric <$> parseJSON v) -- parse saves from time when metric did not have named fields
<|> genericParseJSON metricSerializeOptions v

instance ToJSON a => ToJSON (Metric a) where
toJSON = genericToJSON metricSerializeOptions

makeLensesNoSigs ''Metric

Expand Down

0 comments on commit 29e9105

Please sign in to comment.