waargonaut: JSON wrangling

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]

Flexible, precise, and efficient JSON decoding/encoding library.


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.2.0.0, 0.2.0.1, 0.2.0.2, 0.2.1.0, 0.3.0.0, 0.4.0.0, 0.4.1.0, 0.4.2.0, 0.5.0.0, 0.5.1.0, 0.5.2.0, 0.5.2.1, 0.5.2.2, 0.6.0.0, 0.6.1.0, 0.6.2.0, 0.8.0.0, 0.8.0.1, 0.8.0.2, 0.8.0.2
Change log changelog.md
Dependencies attoparsec (>=0.13 && <0.15), base (>=4.9 && <5), bifunctors (>=5 && <5.6), bytestring (>=0.10.6 && <0.12), containers (>=0.5.6 && <0.7), contravariant (>=1.4 && <1.6), digit (>=0.7 && <1), distributive (>=0.5 && <0.7), errors (>=2.2 && <2.4), generics-sop (>=0.4 && <0.6), hoist-error (>=0.2 && <0.3), hw-balancedparens (>=0.2 && <0.5), hw-bits (>=0.7 && <0.8), hw-json-standard-cursor (>=0.2.1.1 && <0.3), hw-prim (>=0.6 && <0.7), hw-rankselect (>=0.13 && <0.14), lens (>=4.15 && <4.20), mmorph (>=1.1 && <1.2), mtl (>=2.2.2 && <2.3), nats (>=1 && <1.2), natural (>=0.3 && <0.4), parsers (>=0.12 && <0.13), records-sop (>=0.1 && <0.2), scientific (>=0.3 && <0.4), semigroupoids (>=5.2.2 && <5.4), semigroups (>=0.8.4 && <0.20), tagged (>=0.8.5 && <0.9), text (>=1.2 && <1.3), transformers (>=0.4 && <0.6), unordered-containers (>=0.2.9 && <0.3), vector (>=0.12 && <0.13), witherable (>=0.2 && <0.4), wl-pprint-annotated (>=0.1 && <0.2), zippers (>=0.2 && <0.4) [details]
License BSD-3-Clause
Copyright Copyright (C) 2018 Commonwealth Scientific and Industrial Research Organisation (CSIRO)
Author HASKELL-WAARGONAUT @ Data61
Maintainer Sean Chalmers <oᴉ˙ldɟb@uɐǝs> , Emily Pillmore <emilypi@cohomolo.gy> , George Wils <george@wils.online> , Tony Morris <tonymorris+github@gmail.com>
Category Parser, Web, JSON
Home page https://github.com/haskell-waargonaut/waargonaut
Bug tracker https://github.com/haskell-waargonaut/waargonaut/issues
Source repo head: git clone git@github.com/haskell-waargonaut/waargonaut.git
Uploaded by topos at 2021-01-20T21:12:57Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for waargonaut-0.8.0.2

[back to package description]

CSIRO's Data61 Logo

Build Status

Waargonaut

Flexible, precise, and efficient JSON decoding/encoding library. This package provides a plethora of tools for decoding, encoding, and manipulating JSON data.

Features

Example

data Image = Image
  { _imageWidth    :: Int
  , _imageHeight   :: Int
  , _imageTitle    :: Text
  , _imageAnimated :: Bool
  , _imageIDs      :: [Int]
  }
encodeImage :: Applicative f => Encoder f Image
encodeImage = E.mapLikeObj $ \img ->
    E.intAt "Width" (_imageWidth img)
  . E.intAt "Height" (_imageHeight img)
  . E.textAt "Title" (_imageTitle img)
  . E.boolAt "Animated" (_imageAnimated img)
  . E.listAt E.int "IDs" (_imageIDs img)
imageDecoder :: Monad f => D.Decoder f Image
imageDecoder = D.withCursor $ \curs -> do
  -- Move down into the JSON object.
  io <- D.down curs
  -- We need individual values off of our object,
  Image
    <$> D.fromKey "Width" D.int io
    <*> D.fromKey "Height" D.int io
    <*> D.fromKey "Title" D.text io
    <*> D.fromKey "Animated" D.bool io
    <*> D.fromKey "IDs" (D.list D.int) io

Zippers

Waargonaut uses zippers for its decoding which allows for precise control in how you interrogate your JSON input. Take JSON structures and decode them precisely as you require:

Input:
["a","fred",1,2,3,4]
Data Structure:
data Foo = Foo (Char,String,[Int])
Decoder:

The zipper starts the very root of the JSON input, we tell it to move 'down' into the first element.

fooDecoder :: Monad f => Decoder f Foo
fooDecoder = D.withCursor $ \cursor -> do
  fstElem <- D.down cursor

From the first element we can then decode the focus of the zipper using a specific decoder:

  aChar <- D.focus D.unboundedChar fstElem

The next thing we want to decode is the second element of the array, so we move right one step or tooth, and then attempt to decode a string at the focus.

  aString <- D.moveRight1 fstElem >>= D.focus D.string

Finally we want to take everything else in the list and combine them into a single list of Int values. Starting from the first element, we move right two positions (over the char and the string elements), then we use one of the provided decoder functions that will repeatedly move in a direction and combine all of the elements it can until it can no longer move.

  aIntList <- D.moveRightN 2 fstElem >>= D.rightwardSnoc [] D.int

Lastly, we build the Foo using the decoded values.

  pure $ Foo (aChar, aString, aIntList)

The zipper stores the history of your movements, so any errors provide information about the path they took prior to encountering an error. Making debugging precise and straight-forward.

Property Driven Development

This library is built to parse and produce JSON in accordance with the RFC 8259 standard. The data structures, parser, and printer are built to satify the Round Trip Property:

Which may be expressed using the following pseudocode:

parse . print = id

This indicates that any JSON produced by this library will be parsed back in as the exact data structure that produced it. This includes whitespace such as carriage returns and trailing whitespace. There is no loss of information.

There is also this property, again in pseudocode:

print . parse . print = print

This states that the printed form of the JSON will not change will be identical after parsing and then re-printing. There is no loss of information.

This provides a solid foundation to build upon.

NB: The actual code will of course return values that account for the possibility of failure. Computers being what they are.

TODO(s)

In no particular order...