Skip to content

Instantly share code, notes, and snippets.

@maksbotan
Created December 26, 2019 13:24
Show Gist options
  • Save maksbotan/e6c7cf16821e4bc9ee6157f26acb0f79 to your computer and use it in GitHub Desktop.
Save maksbotan/e6c7cf16821e4bc9ee6157f26acb0f79 to your computer and use it in GitHub Desktop.
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 =
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