aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-01-09 22:32:31 +0000
committerLouis Pilfold <louis@lpil.uk>2022-01-09 22:32:31 +0000
commit07d4c5e55453450013a0e398b9cc85e13c27d10d (patch)
tree79e309880b1620d6525fed29bdc0c1f876f0c262 /README.md
parent850d26eaaf1c3de9bed14b249b406b19e3b437a4 (diff)
downloadgleam_json-07d4c5e55453450013a0e398b9cc85e13c27d10d.tar.gz
gleam_json-07d4c5e55453450013a0e398b9cc85e13c27d10d.zip
decode takes a dynamic decoder
Diffstat (limited to 'README.md')
-rw-r--r--README.md32
1 files changed, 11 insertions, 21 deletions
diff --git a/README.md b/README.md
index 9a0c9fc..8e6a35a 100644
--- a/README.md
+++ b/README.md
@@ -23,9 +23,9 @@ import gleam/json.{object, string, list, int, null}
pub fn cat_to_json(cat: Cat) -> String {
object([
#("name", string(cat.name)),
- #("lives", int(9),
+ #("lives", int(cat.lives),
#("flaws", null()),
- #("nicknames", array(["Kitty", "Sweetie"], of: string)),
+ #("nicknames", array(cat.nicknames, of: string)),
])
|> json.to_string
}
@@ -39,27 +39,17 @@ JSON is decoded into a `Dynamic` value which can be decoded using the
```rust
import myapp.{Cat}
import gleam/json
-import gleam/dynamic
+import gleam/dynamic.{field, list, int, string}
import gleam/result
-pub fn cat_from_json(json: String) -> Result<Cat, MyError> {
- try data =
- json.decode(encoded)
- |> result.map_error(InvalidJson)
+pub fn cat_from_json(json_string: String) -> Result<Cat, json.DecodeError> {
+ let cat_decoder = dynamic.decode2(
+ Cat,
+ field("name", of: string),
+ field("lives", of: int),
+ field("nicknames", of: list(string)),
+ )
- let data = dynamic.from(data)
- try cat = {
- try name = dynamic.field(data, "name")
- try name = dynamic.string(name)
- Ok(Cat(name))
- }
- |> result.map_error(InvalidFormat)
-
- Ok(cat)
-}
-
-pub type MyError {
- InvalidJson(json.DecodeError)
- InvalidFormat(dynamic.DecodeError)
+ json.decode(from: json_string, using: cat_decoder)
}
```