aboutsummaryrefslogtreecommitdiff
path: root/test/gleam_json_test.gleam
blob: 162fa6e82d356a2863fc1a3aa6850e64fa6787d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import gleam/dynamic
import gleam/option.{None, Some}
import gleam/result
import gleam/json
import gleeunit/should
import gleeunit

pub fn main() {
  gleeunit.main()
}

pub fn decode_test() {
  json.decode("5")
  |> result.map(dynamic.from)
  |> should.equal(Ok(dynamic.from(5)))

  json.decode(".")
  |> result.nil_error()
  |> should.equal(Error(Nil))
}

pub fn encode_test() {
  json.string("hello")
  |> json.encode()
  |> should.equal("\"hello\"")

  json.null()
  |> json.encode()
  |> should.equal("null")

  json.object([#("foo", json.int(5))])
  |> json.encode()
  |> should.equal("{\"foo\":5}")

  json.nullable(Some(5), json.int)
  |> json.encode()
  |> should.equal("5")

  json.nullable(None, json.int)
  |> json.encode()
  |> should.equal("null")
}