| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Deriving.Aeson.Stripped
Synopsis
- newtype StrippedJSON (fds :: [Type]) (opts :: [Type]) a = StrippedJSON {
- unStrippedJSON :: a
- data RField (name :: Symbol) (def :: k)
- data CField (position :: Nat) (def :: k)
- class RecoverableValue (x :: k) (a :: Type) where
- recoverValue :: Proxy x -> a
- data Coerce (x :: k) (a :: Type)
- data FromString (s :: Symbol)
- data FromList (xs :: [k])
- data Mempty
- data Pure (x :: k)
- module Deriving.Aeson
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 recordTestJust (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 nonRecordTestJust (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
| (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 # | |
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 # | |
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.
Instances
data Coerce (x :: k) (a :: Type) Source #
Recovers a value by coerce-ing it from another RecoverableValue.
>>>recoverValue (Proxy @(Coerce 7 Int)) :: WrappedIntWrappedInt 7
Instances
| (Coercible a b, RecoverableValue x a) => RecoverableValue (Coerce x a :: Type) b Source # | |
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
| (KnownSymbol s, IsString a) => RecoverableValue (FromString s :: Type) a Source # | |
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
| (IsList a, RecoverableValue xs [Item a]) => RecoverableValue (FromList xs :: Type) a Source # | |
Defined in Deriving.Aeson.Stripped Methods recoverValue :: Proxy (FromList xs) -> a Source # | |
Instances
| Monoid m => RecoverableValue Mempty m Source # | |
Defined in Deriving.Aeson.Stripped Methods recoverValue :: Proxy Mempty -> m Source # | |
Recovers an Applicative value using pure.
>>>recoverValue (Proxy @(Pure 1)) :: Maybe IntJust 1
Instances
| (Applicative f, RecoverableValue x a) => RecoverableValue (Pure x :: Type) (f a) Source # | |
Defined in Deriving.Aeson.Stripped Methods recoverValue :: Proxy (Pure x) -> f a Source # | |
Re-exports
module Deriving.Aeson