    {-# LANGUAGE FlexibleInstances     #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE TypeFamilies          #-}

    {-# OPTIONS_GHC -fno-warn-orphans #-}

    -- |
    -- Module    :  Lens.Micro.Aeson.Internal
    -- Copyright :  (c) Colin Woodbury 2015-2024, (c) Edward Kmett 2013-2014, (c) Paul Wilson 2012
    -- License   :  BSD3
    -- Maintainer:  Colin Woodbury <colin@fosskers.ca>
    --
    -- These are stolen from `Lens.Micro.Platform` to avoid its dependencies.
    -- They're altered to be specific to the Aeson context.
    -- Creating instances for `microlens` typeclasses is generally warned
    -- against, hence these instances are hidden here.

    module Lens.Micro.Aeson.Internal where

    import           Data.Aeson (Value(..))
    import qualified Data.Aeson.Key as Key
    import qualified Data.Aeson.KeyMap as KM
    import           Lens.Micro.Internal

    ---

    type instance Index Value = Key.Key

    type instance IxValue Value = Value

    -- | Can only index into the contents of an `Object`,
    -- which is a `KM.KeyMap` `Value`.
    instance Ixed Value where
      ix :: Index Value -> Traversal' Value (IxValue Value)
    ix Index Value
    i IxValue Value -> f (IxValue Value)
    f (Object Object
    o) = Object -> Value
    Object (Object -> Value) -> f Object -> f Value
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Index Object -> Traversal' Object (IxValue Object)
    forall m. Ixed m => Index m -> Traversal' m (IxValue m)
    ix Index Object
    Index Value
    i IxValue Object -> f (IxValue Object)
    IxValue Value -> f (IxValue Value)
    f Object
    o
      ix Index Value
    _ IxValue Value -> f (IxValue Value)
    _ Value
    v          = Value -> f Value
    forall a. a -> f a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Value
    v
      {-# INLINE ix #-}

    -- Thu Oct 21 11:49:16 2021
    -- Adapted from lens-aeson to account for the aeson-2 update.

    type instance Index (KM.KeyMap v) = Key.Key

    type instance IxValue (KM.KeyMap v) = v

    instance Ixed (KM.KeyMap v) where
      ix :: Index (KeyMap v) -> Traversal' (KeyMap v) (IxValue (KeyMap v))
    ix = Index (KeyMap v)
    -> (IxValue (KeyMap v) -> f (IxValue (KeyMap v)))
    -> KeyMap v
    -> f (KeyMap v)
    Index (KeyMap v) -> Traversal' (KeyMap v) (IxValue (KeyMap v))
    forall m. At m => Index m -> Traversal' m (IxValue m)
    ixAt

    instance At (KM.KeyMap v) where
      at :: Index (KeyMap v) -> Lens' (KeyMap v) (Maybe (IxValue (KeyMap v)))
    at Index (KeyMap v)
    k Maybe (IxValue (KeyMap v)) -> f (Maybe (IxValue (KeyMap v)))
    f = (Maybe v -> f (Maybe v)) -> Key -> KeyMap v -> f (KeyMap v)
    forall (f :: * -> *) v.
    Functor f =>
    (Maybe v -> f (Maybe v)) -> Key -> KeyMap v -> f (KeyMap v)
    KM.alterF Maybe v -> f (Maybe v)
    Maybe (IxValue (KeyMap v)) -> f (Maybe (IxValue (KeyMap v)))
    f Key
    Index (KeyMap v)
    k

    instance Each (KM.KeyMap a) (KM.KeyMap b) a b where
      each :: Traversal (KeyMap a) (KeyMap b) a b
    each = (a -> f b) -> KeyMap a -> f (KeyMap b)
    Traversal (KeyMap a) (KeyMap b) a b
    forall (f :: * -> *) a b.
    Traversable f =>
    Traversal (f a) (f b) a b
    traversed
      {-# INLINE each #-}