deriving-aeson

Type driven generic aeson instance customisation

Version on this page:0.2.6
LTS Haskell 22.14:0.2.9@rev:1
Stackage Nightly 2024-03-28:0.2.9@rev:1
Latest on Hackage:0.2.9@rev:1

See all snapshots deriving-aeson appears in

BSD-3-Clause licensed by Fumiaki Kinoshita
Maintained by [email protected]
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):

deriving-aeson

Hackage Haskell CI Discord

logo

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

Changes

Revision history for deriving-aeson

0.2.6

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

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 to ConstructorTagModifier
  • Added toEncoding implementation to CustomJSON

0.1.2

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

0.1.1

  • Added Deriving.Aeson.Stock

0 – 2020-02-26

  • First version. Released on an unsuspecting world.