File tree Expand file tree Collapse file tree 2 files changed +10
-14
lines changed
examples/03-working-with-json Expand file tree Collapse file tree 2 files changed +10
-14
lines changed Original file line number Diff line number Diff line change 1
1
name = " app"
2
2
version = " 1.0.0"
3
3
description = " A Wisp example"
4
- gleam = " >= 0.32 .0"
4
+ gleam = " >= 1.4 .0"
5
5
6
6
[dependencies ]
7
- gleam_stdlib = " >= 0.30 .0 and < 2.0.0"
7
+ gleam_stdlib = " >= 0.51 .0 and < 2.0.0"
8
8
wisp = { path = " ../.." }
9
- gleam_json = " >= 2.0 .0 and < 3.0.0"
9
+ gleam_json = " >= 2.2 .0 and < 3.0.0"
10
10
gleam_erlang = " >= 0.23.0 and < 2.0.0"
11
11
mist = " >= 4.0.0 and < 5.0.0"
12
12
gleam_http = " >= 3.5.0 and < 4.0.0"
Original file line number Diff line number Diff line change 1
1
import app/web
2
- import gleam/dynamic . { type Dynamic }
2
+ import gleam/dynamic/decode
3
3
import gleam/http . { Post }
4
4
import gleam/json
5
5
import gleam/result
@@ -15,14 +15,10 @@ pub type Person {
15
15
// dynamic values [1].
16
16
//
17
17
// [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 : ) )
26
22
}
27
23
28
24
pub fn handle_request ( req : Request ) -> Response {
@@ -36,8 +32,8 @@ pub fn handle_request(req: Request) -> Response {
36
32
use json <- wisp . require_json ( req )
37
33
38
34
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 ( ) ) )
41
37
42
38
// And then a JSON response can be created from the person.
43
39
let object =
You can’t perform that action at this time.
0 commit comments