Created
February 26, 2019 18:36
-
-
Save chessai/a5f28486e2f001ccf657d964e706acf5 to your computer and use it in GitHub Desktop.
derive json automatically for the way i usually write my record product types
This file contains hidden or 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
data Settings = Settings | |
{ settingsFoo :: Int | |
, settingsBar :: String | |
, settingsBaz :: [Int] | |
} | |
deriving (stock) Generic | |
instance FromJSON Settings where | |
parseJSON = parseJSONFielded | |
instance ToJSON Settings where | |
toJSON = toJSONFielded | |
parseJSONFielded' :: forall a. | |
( Generic a, GFromJSON Zero (Rep a)) | |
=> Int | |
-> (Value -> Parser a) | |
parseJSONFielded' x = genericParseJSON defaultOptions | |
{ fieldLabelModifier = camelTo2 '_' . drop x } | |
parseJSONFielded :: forall a. | |
( Generic a, Typeable a, GFromJSON Zero (Rep a)) | |
=> (Value -> Parser a) | |
parseJSONFielded = parseJSONFielded' (tyConNameLength (Proxy @a)) | |
toJSONFielded' :: forall a. | |
( Generic a, GFromJSON Zero (Rep a) ) | |
=> Int | |
-> (a -> Value) | |
toJSONFielded' x = genericToJSON defaultOptions | |
{ fieldLabelModifier = camelTo2 '_' . drop x } | |
toJSONFielded :: forall a. | |
( Generic a, Typeable a, GToJSON Zero (Rep a) ) | |
=> (a -> Value) | |
toJSONFielded = toJSONFielded' (tyConNameLength (Proxy @a)) | |
tyConNameLength :: Typeable a => Proxy a -> Int | |
tyConNameLength p = List.length | |
. Typeable.tyConName | |
. Typeable.typeRepTyCon | |
$ Typeable.typeRep p | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment