stripped-aeson-1.0.0: Type-driven Aeson instance deriving with the ability to strip fields from the output.
Safe HaskellNone
LanguageHaskell2010

Deriving.Aeson.Stripped

Synopsis

Documentation

A layer around the deriving-aeson package, with the ability to strip one or more fields from the JSON output, and recover them when decoding using some specified defaults.

Examples

Setup

All the examples on this page are based on the following setup code:

>>> :set -XDataKinds
>>> :set -XDeriveGeneric
>>> :set -XDerivingVia
>>> :set -XGeneralizedNewtypeDeriving
>>> :set -XOverloadedStrings
>>> :set -XTypeApplications
>>> import Data.Aeson
>>> import Deriving.Aeson.Stripped
>>> import qualified Data.Set as Set
>>> import Data.Text (Text)
>>> :{
 newtype WrappedInt
     = WrappedInt Int
     deriving (FromJSON, Show, ToJSON)
:}
>>> :{
 data RecordTest = RecordTest
     { testBool     :: !Bool
     , testNumber   :: {-# UNPACK #-} !Int
     , testNewtype  :: !WrappedInt
     , testString   :: String
     , testIsString :: !Text
     , testList     :: ![Int]
     , testIsList   :: Set.Set Int
     , testMonoid   :: !Ordering
     , testValue    :: Double
     }
     deriving (Generic, Show)
     deriving (FromJSON, ToJSON)
         via StrippedJSON
             '[ RField "testBool"     'False
              , RField "testIsList"   (FromList '[ 13, 14, 13 ])
              , RField "testIsString" (FromString "text")
              , RField "testList"     '[ 10, 11, 12 ]
              , RField "testMonoid"   Mempty
              , RField "testNewtype"  (Coerce 42 Int)
              , RField "testNumber"   7
              , RField "testString"   "string"
              ]
             '[]
             RecordTest
:}

Note that the order of the RField instructions does not matter ..

>>> let recordTest = RecordTest True 1 (WrappedInt 2) "s" "t" [1..3] (Set.fromList [4..6]) GT 3.14
>>> :{
 data NonRecordTest
     = NonRecordTest () (Either String Int) (Maybe Int) ![Int] (Bool, Char, Int)
     deriving (Generic, Show)
     deriving (FromJSON, ToJSON)
         via StrippedJSON
             '[ CField 0 '()
              , CField 1 ('Left "test")
              , CField 3 (Pure 7)
              , CField 2 'Nothing
              , CField 4 '( 'False, "z", 42 )
              ]
             '[]
             NonRecordTest
:}

.. nor does the order of the CField instructions.

>>> let nonRecordTest = NonRecordTest () (Right 1) (Just 2) [3..5] (True, 'a', 6)

Stripping fields in a record value: ..

>>> encode recordTest
"{\"testValue\":3.14}"

.. and recovering them when decoding using the specified defaults:

>>> decode @RecordTest $ encode recordTest
Just (RecordTest {testBool = False, testNumber = 7, testNewtype = WrappedInt 42, testString = "string", testIsString = "text", testList = [10,11,12], testIsList = fromList [13,14], testMonoid = EQ, testValue = 3.14})

Stripping fields in a non-record value: ..

>>> encode nonRecordTest
"[]"

.. and recovering them when decoding using the specified defaults:

>>> decode @NonRecordTest $ encode nonRecordTest
Just (NonRecordTest () (Left "test") Nothing [7] (False,'z',42))

Specifying encoding / decoding options:

The second parameter to StrippedJSON works exactly the same as the only parameter to CustomJSON from the deriving-aeson package.

>>> encode $ StrippedJSON @'[] @'[ FieldLabelModifier CamelToSnake ] recordTest
"{\"test_bool\":true,\"test_number\":1,\"test_newtype\":2,\"test_string\":\"s\",\"test_is_string\":\"t\",\"test_list\":[1,2,3],\"test_is_list\":[4,5,6],\"test_monoid\":\"GT\",\"test_value\":3.14}"

Core type

newtype StrippedJSON (fds :: [Type]) (opts :: [Type]) a Source #

A newtype wrapper which provides FromJSON / ToJSON instances based on a specific set of AesonOptions (see the Deriving.Aeson module), and the ability to strip one or more fields from the JSON output, recovered when decoding using some default RecoverableValues.

Constructors

StrippedJSON 

Fields

Instances

Instances details
(AesonOptions opts, StripFields lt fds l, Generic a, FromOR f l, ToORRepLazy a lt, Functor (Arborify l), Contravariant (Arborify l), GToJSON Zero (Arborify l), GToEncoding Zero (Arborify l)) => ToJSON (StrippedJSON fds opts a) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

toJSON :: StrippedJSON fds opts a -> Value #

toEncoding :: StrippedJSON fds opts a -> Encoding #

toJSONList :: [StrippedJSON fds opts a] -> Value #

toEncodingList :: [StrippedJSON fds opts a] -> Encoding #

(AesonOptions opts, StripFields lt fds l, Generic a, ToOR f l, FromORRepLazy a lt, Functor (Arborify l), Contravariant (Arborify l), GFromJSON Zero (Arborify l)) => FromJSON (StrippedJSON fds opts a) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

parseJSON :: Value -> Parser (StrippedJSON fds opts a) #

parseJSONList :: Value -> Parser [StrippedJSON fds opts a] #

Stripping fields

data RField (name :: Symbol) (def :: k) Source #

A field to be stripped from record values, identified by its name.

The def value is used to recover the field when decoding (see RecoverableValue).

data CField (position :: Nat) (def :: k) Source #

A field to be stripped from non-record single-constructor values, identified by its zero-based position in the data constructor.

The def value is used to recover the field when decoding (see RecoverableValue).

Recovering fields

class RecoverableValue (x :: k) (a :: Type) where Source #

A default field value which can be recovered when decoding.

Methods

recoverValue :: Proxy x -> a Source #

Recovers a default field value from the type-level.

Instances

Instances details
RecoverableValue 'False Bool Source # 
Instance details

Defined in Deriving.Aeson.Stripped

RecoverableValue 'True Bool Source # 
Instance details

Defined in Deriving.Aeson.Stripped

(KnownNat n, Num a) => RecoverableValue (n :: Nat) a Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy n -> a Source #

KnownSymbol s => RecoverableValue (s :: Symbol) String Source # 
Instance details

Defined in Deriving.Aeson.Stripped

IsValidChar c => RecoverableValue (c :: Symbol) Char Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy c -> Char Source #

RecoverableValue '() () Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '() -> () Source #

Monoid m => RecoverableValue Mempty m Source # 
Instance details

Defined in Deriving.Aeson.Stripped

(KnownSymbol s, IsString a) => RecoverableValue (FromString s :: Type) a Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (FromString s) -> a Source #

RecoverableValue ('[] :: [k]) [a] Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '[] -> [a] Source #

RecoverableValue ('Nothing :: Maybe a1) (Maybe a2) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

(IsList a, RecoverableValue xs [Item a]) => RecoverableValue (FromList xs :: Type) a Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (FromList xs) -> a Source #

RecoverableValue x a2 => RecoverableValue ('Just x :: Maybe a1) (Maybe a2) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy ('Just x) -> Maybe a2 Source #

(Applicative f, RecoverableValue x a) => RecoverableValue (Pure x :: Type) (f a) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (Pure x) -> f a Source #

(Coercible a b, RecoverableValue x a) => RecoverableValue (Coerce x a :: Type) b Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (Coerce x a) -> b Source #

(RecoverableValue x a2, RecoverableValue xs [a2]) => RecoverableValue (x ': xs :: [a1]) [a2] Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (x ': xs) -> [a2] Source #

RecoverableValue x a2 => RecoverableValue ('Right x :: Either a1 b) (Either e a2) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy ('Right x) -> Either e a2 Source #

RecoverableValue x e => RecoverableValue ('Left x :: Either a1 b) (Either e a2) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy ('Left x) -> Either e a2 Source #

(RecoverableValue x a, RecoverableValue y b) => RecoverableValue ('(x, y) :: (k1, k2)) (a, b) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '(x, y) -> (a, b) Source #

(RecoverableValue x a, RecoverableValue y b, RecoverableValue z c) => RecoverableValue ('(x, y, z) :: (k1, k2, k3)) (a, b, c) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '(x, y, z) -> (a, b, c) Source #

(RecoverableValue w a, RecoverableValue x b, RecoverableValue y c, RecoverableValue z d) => RecoverableValue ('(w, x, y, z) :: (k1, k2, k3, k4)) (a, b, c, d) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '(w, x, y, z) -> (a, b, c, d) Source #

(RecoverableValue v a, RecoverableValue w b, RecoverableValue x c, RecoverableValue y d, RecoverableValue z e) => RecoverableValue ('(v, w, x, y, z) :: (k1, k2, k3, k4, k5)) (a, b, c, d, e) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '(v, w, x, y, z) -> (a, b, c, d, e) Source #

(RecoverableValue u a, RecoverableValue v b, RecoverableValue w c, RecoverableValue x d, RecoverableValue y e, RecoverableValue z f) => RecoverableValue ('(u, v, w, x, y, z) :: (k1, k2, k3, k4, k5, k6)) (a, b, c, d, e, f) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy '(u, v, w, x, y, z) -> (a, b, c, d, e, f) Source #

data Coerce (x :: k) (a :: Type) Source #

Recovers a value by coerce-ing it from another RecoverableValue.

>>> recoverValue (Proxy @(Coerce 7 Int)) :: WrappedInt
WrappedInt 7

Instances

Instances details
(Coercible a b, RecoverableValue x a) => RecoverableValue (Coerce x a :: Type) b Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (Coerce x a) -> b Source #

data FromString (s :: Symbol) Source #

Recovers a String-like value using fromString from IsString.

>>> recoverValue (Proxy @(FromString "text")) :: Text
"text"

Instances

Instances details
(KnownSymbol s, IsString a) => RecoverableValue (FromString s :: Type) a Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (FromString s) -> a Source #

data FromList (xs :: [k]) Source #

Recovers a list-like value using fromList from IsList.

>>> recoverValue (Proxy @(FromList '[1,2,3])) :: [Int]
[1,2,3]

Instances

Instances details
(IsList a, RecoverableValue xs [Item a]) => RecoverableValue (FromList xs :: Type) a Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (FromList xs) -> a Source #

data Mempty Source #

Recovers a Monoid value using mempty.

>>> recoverValue (Proxy @Mempty) :: [Int]
[]

Instances

Instances details
Monoid m => RecoverableValue Mempty m Source # 
Instance details

Defined in Deriving.Aeson.Stripped

data Pure (x :: k) Source #

Recovers an Applicative value using pure.

>>> recoverValue (Proxy @(Pure 1)) :: Maybe Int
Just 1

Instances

Instances details
(Applicative f, RecoverableValue x a) => RecoverableValue (Pure x :: Type) (f a) Source # 
Instance details

Defined in Deriving.Aeson.Stripped

Methods

recoverValue :: Proxy (Pure x) -> f a Source #

Re-exports