deriving-aeson
Type driven generic aeson instance customisation
LTS Haskell 23.4: | 0.2.10 |
Stackage Nightly 2025-01-16: | 0.2.10 |
Latest on Hackage: | 0.2.10 |
BSD-3-Clause licensed by Fumiaki Kinoshita
Maintained by [email protected]
This version can be pinned in stack with:
deriving-aeson-0.2.10@sha256:c6f2725d437f352c3267165575c756d18fbb16c1125d3263e3516e6a39098c10,1306
Module documentation for 0.2.10
- Deriving
Depends on 2 packages(full list with versions):
deriving-aeson
This package provides a newtype wrapper where you can customise aeson’s generic methods using a type-level interface, which synergises well with DerivingVia.
{-# LANGUAGE DerivingVia, DataKinds, DeriveGeneric #-}
import Data.Aeson
import Deriving.Aeson
import qualified Data.ByteString.Lazy.Char8 as BL
data User = User
{ userId :: Int
, userName :: String
, userAPIToken :: Maybe String
} deriving Generic
deriving (FromJSON, ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "user", CamelToSnake]] User
testData :: [User]
testData = [User 42 "Alice" Nothing, User 43 "Bob" (Just "xyz")]
main = BL.putStrLn $ encode testData
-- [{"name":"Alice","id":42},{"api_token":"xyz","name":"Bob","id":43}]
Deriving.Aeson.Stock
contains some aliases for even less boilerplates.
Prefixed str
=CustomJSON '[FieldLabelModifier (StripPrefix str)]
PrefixedSnake str
=CustomJSON '[FieldLabelModifier (StripPrefix str, CamelToSnake)]
Suffixed str
=CustomJSON '[FieldLabelModifier (StripSuffix str)]
SuffixedSnake str
=CustomJSON '[FieldLabelModifier (StripSuffix str, CamelToSnake)]
Snake
=CustomJSON '[FieldLabelModifier '[StripPrefix str, CamelToSnake]]
Vanilla
=CustomJSON '[]
How it works
The wrapper type has a phantom type parameter t
, a type-level builder of an Option.
Type-level primitives are reduced to one Option
by the AesonOptions
class.
newtype CustomJSON t a = CustomJSON { unCustomJSON :: a }
class AesonOptions xs where
aesonOptions :: Options
instance AesonOptions xs => AesonOptions (OmitNothingFields ': xs) where
aesonOptions = (aesonOptions @xs) { omitNothingFields = True }
...
You can use any (static) function for name modification by adding an instance of StringModifier
.
data ToLower
instance StringModifier ToLower where
getStringModifier "" = ""
getStringModifier (c : xs) = toLower c : xs
Previous studies
- Type-driven safe derivation of ToJSON and FromJSON, using DerivingVia in GHC 8.6 and some type-level hacks
- Strip prefices from JSON representation
Changes
Revision history for deriving-aeson
0.2.10
- Added
StripSuffix
0.2.9
- Fixed a bug in chaining
ConstructorTagModifier
&FieldLabelModifier
0.2.8
- Supported GHC 9.2
- Supported aeson-2.0
0.2.7
- Added a
StringModifier
instance to a list of types - Added
Rename :: Symbol -> Symbol -> Type
0.2.6
- Added
StringModifier
instances to 3 and 4-tuples - Fixed the bug making
SumTwoElemArray
pointObjectWithSingleField
0.2.5
- Added a generic
CamelTo
constructor
0.2.4
- Added
RejectUnknownFields
0.2.3
- Fixed a bug in
SumTaggedObject
0.2.2
- Added
UnwrapUnaryRecords
0.2.1
- Remove redundant type variables from
Sum*
0.2
- Added
Sum*
for changing the encoding of variants - Added
Vanilla = CustomJSON '[]
- Renamed
ContructorTagModifier
toConstructorTagModifier
- Added
toEncoding
implementation toCustomJSON
0.1.2
- Reexported
CustomJSON(..)
fromDeriving.Aeson.Stock
0.1.1
- Added
Deriving.Aeson.Stock
0 – 2020-02-26
- First version. Released on an unsuspecting world.