roundtrip-aeson: Un-/parse JSON with roundtrip invertible syntax definitions.

[ bsd3, data, library ] [ Propose Tags ]

Un-/parse JSON with roundtrip invertible syntax definitions.

[Skip to Readme]
Dependencies aeson, base (==4.7.*), bytestring, containers (==0.5.*), lens, lens-aeson, roundtrip (==0.2.*), scientific, text (==1.2.*), unordered-containers, vector [details]
License BSD-3-Clause
Copyright Copyright 2014-2015 Anchor Systems and others.
Author Thomas Sutton <>, Christian Marie <>
Maintainer Anchor Engineering <>
Category Data
Home page
Source repo head: git clone
this: git clone release-
Uploaded by ThomasSutton at Wed Jul 29 01:01:55 UTC 2015
Distributions NixOS:
Downloads 344 total (21 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-07-29 [all 1 reports]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for roundtrip-aeson-

[back to package description]

Roundtrip Aeson

Build Status

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.


Using roundtrip-aeson is relatively straightforward:

  1. Define your data type;

  2. Define partial isomorphisms for the constructors (probably using the template haskell);

  3. Describe the syntax of its JSON representation; and

  4. Use that representation to build and parse JSON.

import Data.Aeson.RoundTrip

data Invoice
    = Unpaid Bool Integer Bool
    | Paid Double
  deriving (Show)

defineIsomorphisms ''Invoice

invoiceSyntax :: JsonSyntax s => s Invoice
invoiceSyntax =
        <$> 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.