Type driven generic aeson instance customisation

Version on this page:0.2.6
LTS Haskell 17.4:0.2.6
Stackage Nightly 2021-03-01:
Latest on Hackage:

See all snapshots deriving-aeson appears in

BSD-3-Clause licensed by Fumiaki Kinoshita
Maintained by
This version can be pinned in stack with:deriving-aeson-0.2.6@sha256:f510b7bcd4e2bf714abf439d992da8a2eea82b9ad0461e2dbc9f3c8ccd948c15,1277

Module documentation for 0.2.6

Depends on 2 packages(full list with versions):


Hackage Haskell CI Discord


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)]
  • 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


Revision history for deriving-aeson


  • Added StringModifier instances to 3 and 4-tuples
  • Fixed the bug making SumTwoElemArray point ObjectWithSingleField


  • Added a generic CamelTo constructor


  • Added RejectUnknownFields


  • Fixed a bug in SumTaggedObject


  • Added UnwrapUnaryRecords


  • Remove redundant type variables from Sum*


  • Added Sum* for changing the encoding of variants
  • Added Vanilla = CustomJSON '[]
  • Renamed ContructorTagModifier to ConstructorTagModifier
  • Added toEncoding implementation to CustomJSON


  • Reexported CustomJSON(..) from Deriving.Aeson.Stock


  • Added Deriving.Aeson.Stock

0 – 2020-02-26

  • First version. Released on an unsuspecting world.