Created
December 26, 2019 13:24
-
-
Save maksbotan/e6c7cf16821e4bc9ee6157f26acb0f79 to your computer and use it in GitHub Desktop.
aeson deriving via
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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