-- | JSON-specific deserialization utilities.
{-# LANGUAGE AllowAmbiguousTypes   #-}
{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeApplications      #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeInType            #-}
{-# LANGUAGE UndecidableInstances  #-}
module Versioning.JSON
  ( -- * Types
    Applied
  , JsonDecodableTo
  , JsonDecodableToFrom
    -- * Decoding and upgrading
  , fromJsonAnyVersion
  , fromJsonAnyVersionStrict
  , fromJsonAnyVersionEither
  , fromJsonAnyVersionEitherStrict
  , fromJsonAnyVersionFrom
    -- * Decoding and appyling an action
  , withJsonAnyVersion
  , withJsonAnyVersionStrict
  , withJsonAnyVersionEither
  , withJsonAnyVersionEitherStrict
  , withJsonAnyVersionFrom
  , withJsonAnyVersionM
  , withJsonAnyVersionStrictM
  , withJsonAnyVersionEitherM
  , withJsonAnyVersionEitherStrictM
  , withJsonAnyVersionFromM
  )
where

import           Data.Aeson                   (FromJSON, decode, decodeStrict,
                                               eitherDecode, eitherDecodeStrict)
import qualified Data.ByteString              as StrictBS
import qualified Data.ByteString.Lazy         as LazyBS

import           Versioning.Base
import           Versioning.Internal.Decoding

-- | Decode a JSON string by trying all the versions decrementally
--   and upgrade the decoded object to the newest version.
fromJsonAnyVersion
  :: forall v a . JsonDecodableTo v a => LazyBS.ByteString -> Maybe (a v)
fromJsonAnyVersion :: ByteString -> Maybe (a v)
fromJsonAnyVersion = Decoder FromJSON ByteString Maybe a -> ByteString -> Maybe (a v)
forall (v :: V) (a :: V -> *) (dec :: * -> Constraint) enc
       (t :: * -> *).
(Alt t, Applicative t, DecodableTo dec v a) =>
Decoder dec enc t a -> enc -> t (a v)
decodeAnyVersion Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecode

-- | Like 'fromJsonAnyVersion' but it reads from a strict 'ByteString'
fromJsonAnyVersionStrict
  :: forall v a . JsonDecodableTo v a => StrictBS.ByteString -> Maybe (a v)
fromJsonAnyVersionStrict :: ByteString -> Maybe (a v)
fromJsonAnyVersionStrict = Decoder FromJSON ByteString Maybe a -> ByteString -> Maybe (a v)
forall (v :: V) (a :: V -> *) (dec :: * -> Constraint) enc
       (t :: * -> *).
(Alt t, Applicative t, DecodableTo dec v a) =>
Decoder dec enc t a -> enc -> t (a v)
decodeAnyVersion Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecodeStrict

-- | Like 'fromJsonAnyVersion' but returns a message when decoding fails
fromJsonAnyVersionEither
  :: forall v a
   . JsonDecodableTo v a
  => LazyBS.ByteString
  -> Either String (a v)
fromJsonAnyVersionEither :: ByteString -> Either String (a v)
fromJsonAnyVersionEither = Decoder FromJSON ByteString (Either String) a
-> ByteString -> Either String (a v)
forall (v :: V) (a :: V -> *) (dec :: * -> Constraint) enc
       (t :: * -> *).
(Alt t, Applicative t, DecodableTo dec v a) =>
Decoder dec enc t a -> enc -> t (a v)
decodeAnyVersion Decoder FromJSON ByteString (Either String) a
forall k (a :: k -> *).
Decoder FromJSON ByteString (Either String) a
jsonEitherDecode

-- | Like 'fromJsonAnyVersionStrict' but returns a message when decoding fails
fromJsonAnyVersionEitherStrict
  :: forall v a
   . JsonDecodableTo v a
  => StrictBS.ByteString
  -> Either String (a v)
fromJsonAnyVersionEitherStrict :: ByteString -> Either String (a v)
fromJsonAnyVersionEitherStrict = Decoder FromJSON ByteString (Either String) a
-> ByteString -> Either String (a v)
forall (v :: V) (a :: V -> *) (dec :: * -> Constraint) enc
       (t :: * -> *).
(Alt t, Applicative t, DecodableTo dec v a) =>
Decoder dec enc t a -> enc -> t (a v)
decodeAnyVersion Decoder FromJSON ByteString (Either String) a
forall k (a :: k -> *).
Decoder FromJSON ByteString (Either String) a
jsonEitherDecodeStrict

-- | Like 'fromJsonAnyVersion', with an additional type-parameter
--   indicating the oldest version you want to be able to decode
fromJsonAnyVersionFrom
  :: forall from v a
   . JsonDecodableToFrom from v a
  => LazyBS.ByteString
  -> Maybe (a v)
fromJsonAnyVersionFrom :: ByteString -> Maybe (a v)
fromJsonAnyVersionFrom = Decoder FromJSON ByteString Maybe a -> ByteString -> Maybe (a v)
forall (from :: V) (v :: V) (a :: V -> *) (dec :: * -> Constraint)
       enc (t :: * -> *).
(Alt t, Applicative t, DecodableToFrom from dec v a) =>
Decoder dec enc t a -> enc -> t (a v)
decodeAnyVersionFrom @from Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecode

-- | Decode a JSON string by trying all the versions decrementally
--   and apply an action to the decoded object at its original version.
withJsonAnyVersionM
  :: forall c a v m
   . (WithAnyVersion v a c FromJSON, Applicative m, c (a v))
  => ApplyM m a c
  -> LazyBS.ByteString
  -> m (Maybe (Applied c a))
withJsonAnyVersionM :: ApplyM m a c -> ByteString -> m (Maybe (Applied c a))
withJsonAnyVersionM = Decoder FromJSON ByteString Maybe a
-> ApplyM m a c -> ByteString -> m (Maybe (Applied c a))
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (m :: * -> *) (t :: * -> *).
(WithAnyVersion v a c dec, Alt t, Applicative t, Traversable t,
 Applicative m, c (a v)) =>
Decoder dec enc t a -> ApplyM m a c -> enc -> m (t (Applied c a))
withAnyVersionM @v @c @a Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecode

-- | Like 'withJsonAnyVersionM' but it reads from a strict 'ByteString'
withJsonAnyVersionStrictM
  :: forall c a v m
   . (WithAnyVersion v a c FromJSON, Applicative m, c (a v))
  => ApplyM m a c
  -> StrictBS.ByteString
  -> m (Maybe (Applied c a))
withJsonAnyVersionStrictM :: ApplyM m a c -> ByteString -> m (Maybe (Applied c a))
withJsonAnyVersionStrictM = Decoder FromJSON ByteString Maybe a
-> ApplyM m a c -> ByteString -> m (Maybe (Applied c a))
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (m :: * -> *) (t :: * -> *).
(WithAnyVersion v a c dec, Alt t, Applicative t, Traversable t,
 Applicative m, c (a v)) =>
Decoder dec enc t a -> ApplyM m a c -> enc -> m (t (Applied c a))
withAnyVersionM @v @c @a Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecodeStrict

-- | Like 'withJsonAnyVersionM' but returns a message when decoding fails
withJsonAnyVersionEitherM
  :: forall c a v m
   . (WithAnyVersion v a c FromJSON, Applicative m, c (a v))
  => ApplyM m a c
  -> LazyBS.ByteString
  -> m (Either String (Applied c a))
withJsonAnyVersionEitherM :: ApplyM m a c -> ByteString -> m (Either String (Applied c a))
withJsonAnyVersionEitherM = Decoder FromJSON ByteString (Either String) a
-> ApplyM m a c -> ByteString -> m (Either String (Applied c a))
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (m :: * -> *) (t :: * -> *).
(WithAnyVersion v a c dec, Alt t, Applicative t, Traversable t,
 Applicative m, c (a v)) =>
Decoder dec enc t a -> ApplyM m a c -> enc -> m (t (Applied c a))
withAnyVersionM @v @c @a Decoder FromJSON ByteString (Either String) a
forall k (a :: k -> *).
Decoder FromJSON ByteString (Either String) a
jsonEitherDecode

-- | Like 'withJsonAnyVersionStrictM' but returns a message when decoding fails
withJsonAnyVersionEitherStrictM
  :: forall c a v m
   . (WithAnyVersion v a c FromJSON, Applicative m, c (a v))
  => ApplyM m a c
  -> StrictBS.ByteString
  -> m (Either String (Applied c a))
withJsonAnyVersionEitherStrictM :: ApplyM m a c -> ByteString -> m (Either String (Applied c a))
withJsonAnyVersionEitherStrictM =
  Decoder FromJSON ByteString (Either String) a
-> ApplyM m a c -> ByteString -> m (Either String (Applied c a))
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (m :: * -> *) (t :: * -> *).
(WithAnyVersion v a c dec, Alt t, Applicative t, Traversable t,
 Applicative m, c (a v)) =>
Decoder dec enc t a -> ApplyM m a c -> enc -> m (t (Applied c a))
withAnyVersionM @v @c @a Decoder FromJSON ByteString (Either String) a
forall k (a :: k -> *).
Decoder FromJSON ByteString (Either String) a
jsonEitherDecodeStrict

-- | Like 'withJsonAnyVersionM', with an additional type-parameter
--   indicating the oldest version you want to be able to decode
withJsonAnyVersionFromM
  :: forall from c a v m
   . (WithAnyVersionFrom from v a c FromJSON, Applicative m, c (a v))
  => ApplyM m a c
  -> LazyBS.ByteString
  -> m (Maybe (Applied c a))
withJsonAnyVersionFromM :: ApplyM m a c -> ByteString -> m (Maybe (Applied c a))
withJsonAnyVersionFromM = Decoder FromJSON ByteString Maybe a
-> ApplyM m a c -> ByteString -> m (Maybe (Applied c a))
forall (from :: V) (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (m :: * -> *) (t :: * -> *).
(WithAnyVersionFrom from v a c dec, Alt t, Applicative t,
 Traversable t, Applicative m, c (a v)) =>
Decoder dec enc t a -> ApplyM m a c -> enc -> m (t (Applied c a))
withAnyVersionFromM @from @v @c @a Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecode

-- | Decode a JSON string by trying all the versions decrementally
--   and apply a pure function to the decoded object at its original version.
withJsonAnyVersion
  :: forall c a v
   . (WithAnyVersion v a c FromJSON, c (a v))
  => Apply a c
  -> LazyBS.ByteString
  -> Maybe (Applied c a)
withJsonAnyVersion :: Apply a c -> ByteString -> Maybe (Applied c a)
withJsonAnyVersion = Decoder FromJSON ByteString Maybe a
-> Apply a c -> ByteString -> Maybe (Applied c a)
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (t :: * -> *).
(WithAnyVersion v a c dec, c (a v), Alt t, Applicative t,
 Traversable t) =>
Decoder dec enc t a -> Apply a c -> enc -> t (Applied c a)
withAnyVersion @v @c @a Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecode

-- | Like 'withJsonAnyVersion' but it reads from a strict 'ByteString'
withJsonAnyVersionStrict
  :: forall c a v
   . (WithAnyVersion v a c FromJSON, c (a v))
  => Apply a c
  -> StrictBS.ByteString
  -> Maybe (Applied c a)
withJsonAnyVersionStrict :: Apply a c -> ByteString -> Maybe (Applied c a)
withJsonAnyVersionStrict = Decoder FromJSON ByteString Maybe a
-> Apply a c -> ByteString -> Maybe (Applied c a)
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (t :: * -> *).
(WithAnyVersion v a c dec, c (a v), Alt t, Applicative t,
 Traversable t) =>
Decoder dec enc t a -> Apply a c -> enc -> t (Applied c a)
withAnyVersion @v @c @a Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecodeStrict

-- | Like 'withJsonAnyVersion' but returns a message when decoding fails
withJsonAnyVersionEither
  :: forall c a v
   . (WithAnyVersion v a c FromJSON, c (a v))
  => Apply a c
  -> LazyBS.ByteString
  -> Either String (Applied c a)
withJsonAnyVersionEither :: Apply a c -> ByteString -> Either String (Applied c a)
withJsonAnyVersionEither = Decoder FromJSON ByteString (Either String) a
-> Apply a c -> ByteString -> Either String (Applied c a)
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (t :: * -> *).
(WithAnyVersion v a c dec, c (a v), Alt t, Applicative t,
 Traversable t) =>
Decoder dec enc t a -> Apply a c -> enc -> t (Applied c a)
withAnyVersion @v @c @a Decoder FromJSON ByteString (Either String) a
forall k (a :: k -> *).
Decoder FromJSON ByteString (Either String) a
jsonEitherDecode

-- | Like 'withJsonAnyVersionStrict' but returns a message when decoding fails
withJsonAnyVersionEitherStrict
  :: forall c a v
   . (WithAnyVersion v a c FromJSON, c (a v))
  => Apply a c
  -> StrictBS.ByteString
  -> Either String (Applied c a)
withJsonAnyVersionEitherStrict :: Apply a c -> ByteString -> Either String (Applied c a)
withJsonAnyVersionEitherStrict = Decoder FromJSON ByteString (Either String) a
-> Apply a c -> ByteString -> Either String (Applied c a)
forall (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (t :: * -> *).
(WithAnyVersion v a c dec, c (a v), Alt t, Applicative t,
 Traversable t) =>
Decoder dec enc t a -> Apply a c -> enc -> t (Applied c a)
withAnyVersion @v @c @a Decoder FromJSON ByteString (Either String) a
forall k (a :: k -> *).
Decoder FromJSON ByteString (Either String) a
jsonEitherDecodeStrict

-- | Like 'withJsonAnyVersion', with an additional type-parameter
--   indicating the oldest version you want to be able to decode
withJsonAnyVersionFrom
  :: forall from c a v
   . (WithAnyVersionFrom from v a c FromJSON, c (a v))
  => Apply a c
  -> LazyBS.ByteString
  -> Maybe (Applied c a)
withJsonAnyVersionFrom :: Apply a c -> ByteString -> Maybe (Applied c a)
withJsonAnyVersionFrom = Decoder FromJSON ByteString Maybe a
-> Apply a c -> ByteString -> Maybe (Applied c a)
forall (from :: V) (v :: V) (c :: * -> Constraint) (a :: V -> *)
       (dec :: * -> Constraint) enc (t :: * -> *).
(WithAnyVersionFrom from v a c dec, c (a v), Alt t, Applicative t,
 Traversable t) =>
Decoder dec enc t a -> Apply a c -> enc -> t (Applied c a)
withAnyVersionFrom @from @v @c @a Decoder FromJSON ByteString Maybe a
forall k (a :: k -> *). Decoder FromJSON ByteString Maybe a
jsonDecode

-- | Decode with the aeson 'decode' function
jsonDecode :: Decoder FromJSON LazyBS.ByteString Maybe a
jsonDecode :: Decoder FromJSON ByteString Maybe a
jsonDecode = (forall (v :: k). FromJSON (a v) => ByteString -> Maybe (a v))
-> Decoder FromJSON ByteString Maybe a
forall k k (dec :: k -> Constraint) enc (t :: k -> *)
       (a :: k -> k).
(forall (v :: k). dec (a v) => enc -> t (a v))
-> Decoder dec enc t a
Decoder forall (v :: k). FromJSON (a v) => ByteString -> Maybe (a v)
forall a. FromJSON a => ByteString -> Maybe a
decode

-- | Decode with the aeson 'decodeStrict' function
jsonDecodeStrict :: Decoder FromJSON StrictBS.ByteString Maybe a
jsonDecodeStrict :: Decoder FromJSON ByteString Maybe a
jsonDecodeStrict = (forall (v :: k). FromJSON (a v) => ByteString -> Maybe (a v))
-> Decoder FromJSON ByteString Maybe a
forall k k (dec :: k -> Constraint) enc (t :: k -> *)
       (a :: k -> k).
(forall (v :: k). dec (a v) => enc -> t (a v))
-> Decoder dec enc t a
Decoder forall (v :: k). FromJSON (a v) => ByteString -> Maybe (a v)
forall a. FromJSON a => ByteString -> Maybe a
decodeStrict

-- | Decode with the aeson 'eitherDecode' function
jsonEitherDecode :: Decoder FromJSON LazyBS.ByteString (Either String) a
jsonEitherDecode :: Decoder FromJSON ByteString (Either String) a
jsonEitherDecode = (forall (v :: k).
 FromJSON (a v) =>
 ByteString -> Either String (a v))
-> Decoder FromJSON ByteString (Either String) a
forall k k (dec :: k -> Constraint) enc (t :: k -> *)
       (a :: k -> k).
(forall (v :: k). dec (a v) => enc -> t (a v))
-> Decoder dec enc t a
Decoder forall (v :: k).
FromJSON (a v) =>
ByteString -> Either String (a v)
forall a. FromJSON a => ByteString -> Either String a
eitherDecode

-- | Decode with the aeson 'eitherDecodeStrict' function
jsonEitherDecodeStrict :: Decoder FromJSON StrictBS.ByteString (Either String) a
jsonEitherDecodeStrict :: Decoder FromJSON ByteString (Either String) a
jsonEitherDecodeStrict = (forall (v :: k).
 FromJSON (a v) =>
 ByteString -> Either String (a v))
-> Decoder FromJSON ByteString (Either String) a
forall k k (dec :: k -> Constraint) enc (t :: k -> *)
       (a :: k -> k).
(forall (v :: k). dec (a v) => enc -> t (a v))
-> Decoder dec enc t a
Decoder forall (v :: k).
FromJSON (a v) =>
ByteString -> Either String (a v)
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict

-- | Handy constraint synonym to be used with 'fromJsonAnyVersion'
type JsonDecodableTo v a = JsonDecodableToFrom V0 v a

-- | Like 'JsonDecodableTo', with an additional type-parameter
--   indicating the oldest version you want to be able to decode
type JsonDecodableToFrom from v a = DecodableToFrom from FromJSON v a