Un-/parse JSON with roundtrip invertible syntax definitions. https://github.com/anchor/roundtrip-aeson
|Latest on Hackage:||0.2.0.0|
This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.
roundtrip allows you to write invertible syntax descriptions -- or, to put it another way, a parser and pretty printer combined -- for String or XML data. This package extends this to support constructing and destructing JSON documents.
roundtrip-aeson is relatively straightforward:
Define your data type;
Describe the syntax of its JSON representation; and
Use that representation to build and parse JSON.
data Invoice = Unpaid Bool Integer Bool | Paid Double deriving (Show)
invoiceSyntax :: JsonSyntax s => s Invoice invoiceSyntax = unpaid <$> jsonField "overdue" jsonBool <> jsonField "total" jsonIntegral <> jsonField "warned" jsonBool <|> paid <$> jsonField "total" jsonRealFrac
main :: IO () main = do -- Build a JSON representation. let Right x = runBuilder invoiceSyntax $ Unpaid False 40 [False] L.putStrLn $ encode x -- Parse a JSON representation. print $ runParser invoiceSyntax x
See tests/demo.hs for the complete source of this example.