Repository navigation
Expand file tree
/
Copy pathjson.echo
More file actions
80 lines (73 loc) · 1.52 KB
/
Copy pathjson.echo
File metadata and controls
80 lines (73 loc) · 1.52 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
; std/json — parse/stringify product values.
; Echo values are untagged integers: JSON true parses to 1, false and null to 0,
; and 0 and 1 stringify as numbers. A top-level 0, false, or null cannot be
; parsed because 0 also signals failure (docs/stdlib.md).
/ runtime
/ std/test
/ std/str
$ parse = (s) {
$ v = runtime.json_parse(s)
? v == 0 {
! "json parse failed"
}
^ v
}
$ stringify = (v) {
$ s = runtime.json_stringify(v)
? s == 0 {
! "json stringify failed"
}
^ s
}
test.it("json_object", () {
$ raw = '{"n":1}'
| parse(raw) {
$ o {
| stringify(o) {
$ s {
test.true(str.contains(s, "n"))
}
! e {
test.fail(e)
}
}
}
! e {
test.fail(e)
}
}
})
; Stringify a string produced on a task worker (`+`), joined on this thread.
test.it("stringify_task_string", () {
+ job = {
^ "hello"
}
- s = job
| stringify(s) {
$ out {
test.eq(out, "\"hello\"")
}
! e {
test.fail(e)
}
}
})
; Benchmarks: `xo test --bench std/json.echo`
test.bench("parse", () {
| parse('{"n":1,"s":"hi"}') {
$ o {}
! e {}
}
})
test.bench("roundtrip", () {
| parse('{"n":1}') {
$ o {
| stringify(o) {
$ s {}
! e {}
}
}
! e {}
}
})
\ parse, stringify