aeson-parsec-picky: Alternative JSON parser based on Parsec and Aeson

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]

JSON parser with nice error messages and little more strict syntax (whitespace-wise).


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.1.0.1
Change log CHANGES.md
Dependencies aeson (>=0.10), base (>=4.7 && <5.0), parsec (>=3.0), scientific, text, unordered-containers, vector [details]
License BSD-3-Clause
Copyright (c) 2015, Matej Kollar
Author Matej Kollar
Maintainer 208115@mail.muni.cz
Category Text, JSON
Home page https://github.com/FPBrno/aeson-parsec-picky
Source repo head: git clone https://github.com/FPBrno/aeson-parsec-picky
this: git clone https://github.com/FPBrno/aeson-parsec-picky(tag v0.1.0.0)
Uploaded by MatejKollar at 2015-11-09T22:10:42Z

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for aeson-parsec-picky-0.1.0.0

[back to package description]

Picky JSON Parser

JSON parser with nice error messages and little more strict syntax (whitespace-wise). Based on Aeson and Parsec.

Interacting with user

JSON being nice readable text-based format seems good candidate for occasionally being created by a user. While Aeson provides really super-optimized parsers, their error messages are not very helpful. Creating larger JSON object by hand can be frustrating (especially) when you make even a small mistake.

While this parser is not optimized for speed, it tries to produce nice and helpful error messages. (This library uses Parsec library.)

Another way to help your user is not allowing him or her to learn wrong habbits. Just look at the following piece of code (be warned - there are trailing spaces there):

{ "name"   :   
   ,   

"Hal"
}

That (in my opinion) is something one would not like to see in files users of his or hers tool produces. So why not forbid that? This library does not allow such things while still allowing to make the input more airy.

Composability

This library was written with re-usability in mind. Parsers it provides do not consume any spaces before of after corresponding values and therefore are more easily reusable for your own projects.

Parsing to Aeson data types

Aeson library is nice to work with with large ecosystem of useful libraries. So why not join them and avoid reinventing the wheel?

Example Use

Script

{-# LANGUAGE DeriveGeneric #-}
module Main (main) where

import GHC.Generics
import System.Environment (getArgs)

import Data.Aeson hiding (eitherDecode)
import Data.Aeson.Parser.Parsec.Picky (eitherDecode)

import Data.Text.IO as Text (readFile)

data Contact = Contact
    { name :: String
    , address :: String
    } deriving (Generic, Show)

instance FromJSON Contact where

printContacts :: [Contact] -> IO ()
printContacts = mapM_ print

main' :: [String] -> IO ()
main' [filename] = Text.readFile filename
    >>= process . eitherDecode filename
    where
    process = either putStrLn printContacts
main' _ = print "Usage: script [CONTACTS_FILE]"

main :: IO ()
main = getArgs >>= main'

Input file

[ { "name": "Alice"
  , "address": "Kansas"
  }
]

Motivation

Why another JSON parser? Some internal tool for JSON RPC testing used simple format that re-used JSON parsers. It was already re-written few times and reasons were:

No parser I was aware of seemed to solve these issues.