From 07d4c5e55453450013a0e398b9cc85e13c27d10d Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Sun, 9 Jan 2022 22:32:31 +0000 Subject: decode takes a dynamic decoder --- README.md | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'README.md') 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 { - try data = - json.decode(encoded) - |> result.map_error(InvalidJson) +pub fn cat_from_json(json_string: String) -> Result { + 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) } ``` -- cgit v1.2.3