Skip to content

Commit 07c62e7

Browse files
committed
Update for new decoder API
1 parent 53795cb commit 07c62e7

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

examples/03-working-with-json/gleam.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
name = "app"
22
version = "1.0.0"
33
description = "A Wisp example"
4-
gleam = ">= 0.32.0"
4+
gleam = ">= 1.4.0"
55

66
[dependencies]
7-
gleam_stdlib = ">= 0.30.0 and < 2.0.0"
7+
gleam_stdlib = ">= 0.51.0 and < 2.0.0"
88
wisp = { path = "../.." }
9-
gleam_json = ">= 2.0.0 and < 3.0.0"
9+
gleam_json = ">= 2.2.0 and < 3.0.0"
1010
gleam_erlang = ">= 0.23.0 and < 2.0.0"
1111
mist = ">= 4.0.0 and < 5.0.0"
1212
gleam_http = ">= 3.5.0 and < 4.0.0"

examples/03-working-with-json/src/app/router.gleam

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import app/web
2-
import gleam/dynamic.{type Dynamic}
2+
import gleam/dynamic/decode
33
import gleam/http.{Post}
44
import gleam/json
55
import gleam/result
@@ -15,14 +15,10 @@ pub type Person {
1515
// dynamic values [1].
1616
//
1717
// [1]: https://hexdocs.pm/gleam_stdlib/gleam/dynamic.html
18-
fn decode_person(json: Dynamic) -> Result(Person, dynamic.DecodeErrors) {
19-
let decoder =
20-
dynamic.decode2(
21-
Person,
22-
dynamic.field("name", dynamic.string),
23-
dynamic.field("is-cool", dynamic.bool),
24-
)
25-
decoder(json)
18+
fn person_decoder() -> decode.Decoder(Person) {
19+
use name <- decode.field("name", decode.string)
20+
use is_cool <- decode.field("is-cool", decode.bool)
21+
decode.success(Person(name:, is_cool:))
2622
}
2723

2824
pub fn handle_request(req: Request) -> Response {
@@ -36,8 +32,8 @@ pub fn handle_request(req: Request) -> Response {
3632
use json <- wisp.require_json(req)
3733

3834
let result = {
39-
// The dynamic value can be decoded into a `Person` value.
40-
use person <- result.try(decode_person(json))
35+
// The JSON data can be decoded into a `Person` value.
36+
use person <- result.try(decode.run(json, person_decoder()))
4137

4238
// And then a JSON response can be created from the person.
4339
let object =

0 commit comments

Comments
 (0)