-- | This module contains only orphan instances. It is only needed to
-- be imported where you are overriding instances for aeson generic derivation.

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.Override.Aeson where

import Data.Coerce (Coercible, coerce)
import Data.Override.Internal (Override, Overridden(Overridden), Using)
import GHC.Generics (Generic, Rep)
import qualified Data.Aeson as Aeson

instance
  ( Generic (Override a xs)
  , Aeson.GToJSON Aeson.Zero (Rep (Override a xs))
  , Aeson.GToEncoding Aeson.Zero (Rep (Override a xs))
  ) => Aeson.ToJSON (Override a xs)

instance
  ( Coercible a (Using ms a xs)
  , Aeson.ToJSON (Using ms a xs)
  ) => Aeson.ToJSON (Overridden ms a xs)
  where
  toJSON :: Overridden ms a xs -> Value
toJSON = ToJSON (Using ms a xs) => Using ms a xs -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON @(Using ms a xs) (Using ms a xs -> Value)
-> (Overridden ms a xs -> Using ms a xs)
-> Overridden ms a xs
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Overridden ms a xs -> Using ms a xs
coerce
  toEncoding :: Overridden ms a xs -> Encoding
toEncoding = ToJSON (Using ms a xs) => Using ms a xs -> Encoding
forall a. ToJSON a => a -> Encoding
Aeson.toEncoding @(Using ms a xs) (Using ms a xs -> Encoding)
-> (Overridden ms a xs -> Using ms a xs)
-> Overridden ms a xs
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Overridden ms a xs -> Using ms a xs
coerce

instance
  ( Generic (Override a xs)
  , Aeson.GFromJSON Aeson.Zero (Rep (Override a xs))
  ) => Aeson.FromJSON (Override a xs)

instance
  ( Coercible a (Using ms a xs)
  , Aeson.FromJSON (Using ms a xs)
  ) => Aeson.FromJSON (Overridden ms a xs)
  where
  parseJSON :: Value -> Parser (Overridden ms a xs)
parseJSON = Parser (Using ms a xs) -> Parser (Overridden ms a xs)
coerce (Parser (Using ms a xs) -> Parser (Overridden ms a xs))
-> (Value -> Parser (Using ms a xs))
-> Value
-> Parser (Overridden ms a xs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FromJSON (Using ms a xs) => Value -> Parser (Using ms a xs)
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON @(Using ms a xs)