Skip to content

Commit 7e9af05

Browse files
committed
Provided a simple example in the documentation to orient new users.
1 parent dc778b2 commit 7e9af05

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,31 @@ bower install purescript-argonaut-codecs
1515
## Documentation
1616

1717
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-argonaut-codecs).
18+
19+
## Example
20+
21+
Using [purescript-argonaut-core](https://github.com/purescript-contrib/purescript-argonaut-core) we can build a simple `Json` object:
22+
23+
```purescript
24+
someObject = fromObject (StrMap.fromFoldable [
25+
Tuple "foo" (fromArray [
26+
jsonSingletonObject "bar" (fromString "a"),
27+
jsonSingletonObject "bar" (fromString "b")
28+
])
29+
])
30+
```
31+
32+
The `decodeJson` and `.?` functions provided in this module make it straightforward to interrogate the `Json` object:
33+
34+
```purescript
35+
main =
36+
log $ show $ getBars someObject
37+
38+
getBars :: Json -> Either String (Array String)
39+
getBars json = do
40+
obj <- decodeJson json
41+
foo <- obj .? "foo"
42+
for foo \itemJson -> do
43+
itemObj <- decodeJson itemJson
44+
itemObj .? "bar"
45+
```

0 commit comments

Comments
 (0)