summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/34_json
blob: ba8ad9f62c79955355abbe4a0105aa4ca15f91fe (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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
The `json()` function parses the given string value as JSON.

Throws an exception if the given input value is not a string.
Throws an exception if the given input string cannot be parsed as JSON.

Returns the resulting value.

-- Testcase --
{%
	print(join("\n", [
		json("null"),
		json("true"),
		json("false"),
		json("123"),
		json("456.7890000"),
		json("-1.4E10"),
		json("1e309"),
		json('"A string \u2600"'),
		json("[ 1, 2, 3 ]"),
		json('{ "test": [ 1, 2, 3 ] }'),

		// surrounding white space is ignored
		json('    [ 1, 2, 3 ]    ')
	]), "\n");
%}
-- End --

-- Expect stdout --
null
true
false
123
456.789
-1.4e+10
Infinity
A string ☀
[ 1, 2, 3 ]
{ "test": [ 1, 2, 3 ] }
[ 1, 2, 3 ]
-- End --


Passing a non-string value throws an exception.

-- Testcase --
{%
	json(true);
%}
-- End --

-- Expect stderr --
Type error: Passed value is not a string
In line 2, byte 11:

 `    json(true);`
  Near here ---^


-- End --


Unparseable JSON throws exceptions.

-- Testcase --
{%
	json('[ "incomplete", "array" ');
%}
-- End --

-- Expect stderr --
Syntax error: Failed to parse JSON string: unexpected end of data
In line 2, byte 33:

 `    json('[ "incomplete", "array" ');`
  Near here -------------------------^


-- End --

-- Testcase --
{%
	json('invalid syntax');
%}
-- End --

-- Expect stderr --
Syntax error: Failed to parse JSON string: unexpected character
In line 2, byte 23:

 `    json('invalid syntax');`
  Near here ---------------^


-- End --

-- Testcase --
{%
	json('[] trailing garbage');
%}
-- End --

-- Expect stderr --
Syntax error: Trailing garbage after JSON data
In line 2, byte 28:

 `    json('[] trailing garbage');`
  Near here --------------------^


-- End --