Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
aeson deriving via
{-# LANGUAGE DeriveGeneric, DerivingVia, FlexibleInstances, UndecidableInstances #-}
import Data.Aeson
import Data.Aeson.Casing
import GHC.Generics
newtype CamelCaseJson a = CamelCaseJson a
deriving (Eq, Show, Generic)
instance (GToJSON Zero (Rep a), Generic a) => ToJSON (CamelCaseJson a) where
toJSON (CamelCaseJson a) = genericToJSON (aesonPrefix camelCase) a
data Foo =
{ fooBarBaz :: Int
, fooLaLa :: String
deriving Generic
deriving ToJSON via (CamelCaseJson Foo)
-- encode $ Foo 42 "lala"
-- {"barBaz":42,"laLa":"lala"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment