/purescript-argonaut-codecs

JSON serialization using Argonaut.

Primary LanguagePureScriptMIT LicenseMIT

purescript-argonaut-codecs

Latest release Build status Pursuit Maintainer: garyb Maintainer: thomashoneyman

EncodeJson and DecodeJson classes and instances, useful combinators for encoding and decoding Json values.

Installation

bower install purescript-argonaut-codecs

Documentation

Module documentation is published on Pursuit.

Example

Using purescript-argonaut-core we can build a simple Json object:

someObject =
  let 
    objects = 
      [ jsonSingletonObject "bar" (fromString "a")
      , jsonSingletonObject "bar" (fromString "b")
      ]
  in
    fromObject $ Object.fromFoldable [ Tuple "foo" (fromArray objects) ]

The decodeJson and .? functions provided in this module make it straightforward to interrogate the Json object:

main =
  log $ show $ getBars someObject

getBars :: Json -> Either String (Array String)
getBars json = do
  obj <- decodeJson json
  foo <- obj .? "foo"
  for foo \itemJson -> do
    itemObj <- decodeJson itemJson
    itemObj .? "bar"

Contributing

Read the contribution guidelines to get started and see helpful related resources.