Popularity
7.6
Stable
Activity
0.0
Stable
17
12
1

Monthly Downloads: 11
Programming language: Haskell
License: BSD 3-clause "New" or "Revised" License
Tags: Data    
Latest version: v0.2.0.0

roundtrip-aeson alternatives and similar packages

Based on the "Data" category.
Alternatively, view roundtrip-aeson alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of roundtrip-aeson or a related project?

Add another 'Data' Package

README

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.