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

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Un-/parse JSON with roundtrip invertible syntax definitions.


[Skip to Readme]

Properties

Versions 0.2.0.0, 0.2.0.1, 0.2.0.1, 0.2.0.2, 0.3.0.2
Change log ChangeLog
Dependencies aeson, base (>=4.7 && <6), bytestring, containers (>=0.5 && <0.6), lens, lens-aeson, roundtrip (>=0.2 && <0.3), scientific, text (>=1.2 && <1.3), unordered-containers, vector [details]
License BSD-3-Clause
Copyright Copyright 2014-2015 Anchor Systems and others.
Author Thomas Sutton <me@thomas-sutton.id.au>, Christian Marie <christian@ponies.io>
Maintainer Christian Marie
Category Data
Home page https://github.com/anchor/roundtrip-aeson
Source repo head: git clone https://github.com/anchor/roundtrip-aeson
Uploaded by ChristianMarie at 2018-10-09T09:16:52Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for roundtrip-aeson-0.2.0.1

[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.

Example

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 =
    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.