about summary refs log tree commit diff
path: root/src/Xanthous/Data/EntityMap.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Xanthous/Data/EntityMap.hs')
-rw-r--r--src/Xanthous/Data/EntityMap.hs15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/Xanthous/Data/EntityMap.hs b/src/Xanthous/Data/EntityMap.hs
index a068828a157c..9ca915553594 100644
--- a/src/Xanthous/Data/EntityMap.hs
+++ b/src/Xanthous/Data/EntityMap.hs
@@ -42,9 +42,13 @@ import Xanthous.Orphans ()
 import Xanthous.Util (EqEqProp(..))
 --------------------------------------------------------------------------------
 import Data.Monoid (Endo(..))
-import Test.QuickCheck (Arbitrary(..))
+import Test.QuickCheck (Arbitrary(..), CoArbitrary, Function)
 import Test.QuickCheck.Checkers (EqProp)
+import Test.QuickCheck.Instances.UnorderedContainers ()
+import Test.QuickCheck.Instances.Vector ()
+import Data.Aeson
 --------------------------------------------------------------------------------
+
 type EntityID = Word32
 type NonNullVector a = NonNull (Vector a)
 
@@ -55,9 +59,16 @@ data EntityMap a where
     , _lastID     :: EntityID
     } -> EntityMap a
   deriving stock (Functor, Foldable, Traversable, Generic)
+  deriving anyclass (NFData, CoArbitrary, Function)
 deriving via (EqEqProp (EntityMap a)) instance Eq a => EqProp (EntityMap a)
 makeLenses ''EntityMap
 
+instance ToJSON a => ToJSON (EntityMap a) where
+  toJSON = toJSON . toEIDsAndPositioned
+
+instance FromJSON a => FromJSON (EntityMap a) where
+  parseJSON = fmap (fromEIDsAndPositioned @[_]) . parseJSON
+
 byIDInvariantError :: forall a. a
 byIDInvariantError = error $ "Invariant violation: All EntityIDs in byPosition "
   <> "must point to entityIDs in byID"
@@ -180,7 +191,7 @@ atPositionWithIDs pos em =
   in (id &&& Positioned pos . getEIDAssume em) <$> eids
 
 fromEIDsAndPositioned
-  :: (MonoFoldable mono, Element mono ~ (EntityID, Positioned a))
+  :: forall mono a. (MonoFoldable mono, Element mono ~ (EntityID, Positioned a))
   => mono
   -> EntityMap a
 fromEIDsAndPositioned eps = newLastID $ alaf Endo foldMap insert' eps mempty