Lines Matching refs:json
1 # Tests of json module.
4 load("json.star", "json")
6 assert.eq(dir(json), ["decode", "encode", "indent"])
10 ## json.encode
12 assert.eq(json.encode(None), "null")
13 assert.eq(json.encode(True), "true")
14 assert.eq(json.encode(False), "false")
15 assert.eq(json.encode(-123), "-123")
16 assert.eq(json.encode(12345*12345*12345*12345*12345*12345), "3539537889086624823140625")
17 assert.eq(json.encode(float(12345*12345*12345*12345*12345*12345)), "3.539537889086625e+24")
18 assert.eq(json.encode(12.345e67), "1.2345e+68")
19 assert.eq(json.encode("hello"), '"hello"')
20 assert.eq(json.encode([1, 2, 3]), "[1,2,3]")
21 assert.eq(json.encode((1, 2, 3)), "[1,2,3]")
22 assert.eq(json.encode(range(3)), "[0,1,2]") # a built-in iterable
23 assert.eq(json.encode(dict(x = 1, y = "two")), '{"x":1,"y":"two"}')
24 assert.eq(json.encode(dict(y = "two", x = 1)), '{"x":1,"y":"two"}') # key, not insertion, order
25 assert.eq(json.encode(struct(x = 1, y = "two")), '{"x":1,"y":"two"}') # a user-defined HasAttrs
26 assert.eq(json.encode(""[:1]), '"\\ufffd"') # invalid UTF-8 -> replacement char
29 assert.fails(lambda: json.encode(expr), error)
31 encode_error(float("NaN"), "json.encode: cannot encode non-finite float nan")
40 ## json.decode
42 assert.eq(json.decode("null"), None)
43 assert.eq(json.decode("true"), True)
44 assert.eq(json.decode("false"), False)
45 assert.eq(json.decode("-123"), -123)
46 assert.eq(json.decode("-0"), -0)
47 assert.eq(json.decode("3539537889086624823140625"), 3539537889086624823140625)
48 assert.eq(json.decode("3539537889086624823140625.0"), float(3539537889086624823140625))
49 assert.eq(json.decode("3.539537889086625e+24"), 3.539537889086625e+24)
50 assert.eq(json.decode("0e+1"), 0)
51 assert.eq(json.decode("-0.0"), -0.0)
52 assert.eq(json.decode(
55 assert.eq(json.decode('[]'), [])
56 assert.eq(json.decode('[1]'), [1])
57 assert.eq(json.decode('[1,2,3]'), [1, 2, 3])
58 assert.eq(json.decode('{"one": 1, "two": 2}'), dict(one=1, two=2))
59 assert.eq(json.decode('{"foo\\u0000bar": 42}'), {"foo\x00bar": 42})
60 assert.eq(json.decode('"\\ud83d\\ude39\\ud83d\\udc8d"'), "")
61 assert.eq(json.decode('"\\u0123"'), 'ģ')
62 assert.eq(json.decode('"\x7f"'), "\x7f")
65 assert.fails(lambda: json.decode(expr), error)
68 "json.decode: at offset 4, unexpected character 'f' after value")
103 return json.decode(json.encode(x))
124 ## json.indent
126 s = json.encode(dict(x = 1, y = ["one", "two"]))
128 assert.eq(json.indent(s), '''{
136 assert.eq(json.decode(json.indent(s)), {"x": 1, "y": ["one", "two"]})
138 assert.eq(json.indent(s, prefix='¶', indent='–––'), '''{
146 assert.fails(lambda: json.indent("!@#$%^& this is not json"), 'invalid character')