Skip to content

Commit 82efbd5

Browse files
Merge pull request #31 from AidanDelaney/master
Provided a simple example in the documentation to orient new users.
2 parents d196c76 + a4f44e3 commit 82efbd5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@ bower install purescript-argonaut-codecs
1818

1919
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-argonaut-codecs).
2020

21+
## Example
22+
23+
Using [purescript-argonaut-core](https://github.com/purescript-contrib/purescript-argonaut-core) we can build a simple `Json` object:
24+
25+
```purescript
26+
someObject =
27+
let
28+
objects =
29+
[ jsonSingletonObject "bar" (fromString "a")
30+
, jsonSingletonObject "bar" (fromString "b")
31+
]
32+
in
33+
fromObject $ Object.fromFoldable [ Tuple "foo" (fromArray objects) ]
34+
```
35+
36+
The `decodeJson` and `.?` functions provided in this module make it straightforward to interrogate the `Json` object:
37+
38+
```purescript
39+
main =
40+
log $ show $ getBars someObject
41+
42+
getBars :: Json -> Either String (Array String)
43+
getBars json = do
44+
obj <- decodeJson json
45+
foo <- obj .? "foo"
46+
for foo \itemJson -> do
47+
itemObj <- decodeJson itemJson
48+
itemObj .? "bar"
49+
```
50+
2151
## Contributing
2252

2353
Read the [contribution guidelines](https://github.com/purescript-contrib/purescript-argonaut-codecs/blob/master/.github/contributing.md) to get started and see helpful related resources.

0 commit comments

Comments
 (0)