module Hedgehog.Extras.Stock.Aeson
  ( rewriteObject
  , rewriteArrayElements
  ) where

import           Data.Aeson
import           Data.Aeson.KeyMap (KeyMap)
import           Data.Functor

-- | Rewrite a JSON object to another JSON object using the function 'f'.
--
-- All other JSON values are preserved.
rewriteObject :: (KeyMap Value -> KeyMap Value) -> Value -> Value
rewriteObject :: (KeyMap Value -> KeyMap Value) -> Value -> Value
rewriteObject KeyMap Value -> KeyMap Value
f (Object KeyMap Value
hm) = KeyMap Value -> Value
Object (KeyMap Value -> KeyMap Value
f KeyMap Value
hm)
rewriteObject KeyMap Value -> KeyMap Value
_ Value
v = Value
v

-- | Rewrite each element of a JSON array using the function 'f'.
--
-- All other JSON values are preserved.
rewriteArrayElements :: (Value -> Value) -> Value -> Value
rewriteArrayElements :: (Value -> Value) -> Value -> Value
rewriteArrayElements Value -> Value
f (Array Array
hm) = Array -> Value
Array ((Value -> Value) -> Array -> Array
forall a b. (a -> b) -> Vector a -> Vector b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> Value
f Array
hm)
rewriteArrayElements Value -> Value
_ Value
v = Value
v