Last active
January 29, 2019 17:12
-
-
Save chessai/a7ed82a06fb3ca187bc6f8695a0ca353 to your computer and use it in GitHub Desktop.
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
-- | JSON parse helper. | |
-- | |
-- data Foo = Foo { fooIntBar :: Int, fooStringBar :: String } | |
-- deriving (Generic) | |
-- | |
-- -- This will make 'Foo''s fields get parsed as | |
-- -- 'int_bar', 'string_bar', etc. | |
-- instance FromJSON Foo where | |
-- parseJSON = parseJSONFielded | |
parseJSONFielded :: forall a. (Generic a, Typeable a, AE.GFromJSON AE.Z | |
ero (Rep a)) => AE.Value -> AE.Parser a | |
parseJSONFielded = | |
let tyConLength = List.length | |
. Typeable.tyConName | |
. Typeable.typeRepTyCon | |
$ (Typeable.typeRep (Proxy @a)) | |
in AE.genericParseJSON AE.defaultOptions | |
{ AE.fieldLabelModifier = AE.camelTo2 '_' . List.drop tyConLengt | |
h } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment