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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
# We test some ground values for json import/export.
def test_parse_error(name, f, msg) =
error_caught = ref(false)
try
print(f())
catch err : [error.json] do
if
err.kind != "json"
then
print(
"parse error test #{name} failed: wrong error kind, got: #{err.kind}, \
expected: json"
)
test.fail()
end
if
err.message != msg
then
print(
"parse error test #{name} failed: wrong error message, got: #{
err.message
}, expected: #{msg}"
)
test.fail()
end
error_caught := true
end
if
not error_caught()
then
print(
"parse error test #{name} failed: no error caught"
)
test.fail()
end
end
def f() =
test.equal(json.stringify(()), '[]')
test.equal(json.stringify("aa'bb"), "\"aa'bb\"")
test.equal(json.stringify("a"), '"a"')
test.equal(json.stringify("©"), '"©"')
test.equal(json.stringify('"'), '"\\""')
test.equal(json.stringify('\\'), '"\\\\"')
test.equal(json.stringify(json5=true, infinity), 'Infinity')
test.equal(json.stringify(json5=true, (0. - infinity)), '-Infinity')
test.equal(json.stringify(json5=true, nan), 'NaN')
let b = json.object()
b.add("b", 1)
s = json.stringify({a=null({a=1}), b=null(b)})
test.equal(
s,
"{ \"a\": { \"a\": 1 }, \"b\": { \"b\": 1 } }"
)
data = "123"
let json.parse (x : int) = data
test.equal(x, 123)
data =
'{
"foo": 34.24,
"gni gno": true,
"nested": {
"tuple": [123, 3.14, false],
"list": [44.0, 55, 66.12],
"nullable_list": [12.33, 23, "aabb"],
"object_as_list": {
"foo": 123,
"gni": 456.0,
"gno": 3.14
},
"arbitrary object key ✨": true
},
"extra": "ignored"
}'
let json.parse (x :
{
foo: float,
"gni gno" as gni_gno: bool,
nested: {
tuple: (_ * float),
list: [float],
nullable_list: [int?],
object_as_list: [(string*float)] as json.object,
"arbitrary object key ✨" as arbitrary_object_key: bool,
not_present: bool?
}
}
) = data
test.equal(
x,
{
foo=34.24,
gni_gno=true,
nested=
{
tuple=(null, 3.14),
list=[44., 55., 66.12],
nullable_list=[null, 23, null],
object_as_list=[("foo", 123.), ("gni", 456.0), ("gno", 3.14)],
arbitrary_object_key=true,
not_present=null
}
}
)
# Pattern extraction with json parsing
let json.parse {
foo,
nested = {tuple = (t1, t2, t3), nullable_list = [l1, ...tl]}
} = data
test.equal(foo, 34.24)
test.equal(t1, 123)
test.equal(t2, 3.14)
test.equal(t3, false)
test.equal(l1, null)
test.equal(tl, [23, null])
let json.parse x = data
ignore(x.foo + 1.0)
let (x, y, _) = x.nested.tuple
ignore(x + 1)
ignore(y + 3.14)
def failed_array() =
data = "[]"
let json.parse (val : {x: int}) = data
print(val.x)
end
test_parse_error(
"failed array parsing",
failed_array,
"Parsing error: json value cannot be parsed as type {x : int}"
)
def failed_runtime() =
let json.parse x = data
ignore(x.foo + 1.0)
let (x, _, _) = x.nested.tuple
ignore(x ^ "foo")
end
test_parse_error(
"failed runtime",
failed_runtime,
"Parsing error: json value cannot be parsed as type {nested: {tuple: \
(string,_,_), _}, _}"
)
def nested_tuple() =
let json.parse (x :
{
nested: {
tuple: (int * float * int * bool),
list: [float],
nullable_list: [int?],
object_as_list: [(string*float)] as json.object,
"arbitrary object key ✨" as arbitrary_object_key: bool,
not_present: bool?
}
}
) = data
ignore(data)
end
test_parse_error(
"nested tuple",
nested_tuple,
"Parsing error: json value cannot be parsed as type {nested: {tuple: \
(_,_,int,_), _}, _}"
)
def nested_list() =
let json.parse (x :
{
nested: {
tuple: (int * float * bool),
list: [int],
nullable_list: [int?],
object_as_list: [(string*float)] as json.object,
"arbitrary object key ✨" as arbitrary_object_key: bool,
not_present: bool?
}
}
) = data
ignore(data)
end
test_parse_error(
"nested list",
nested_list,
"Parsing error: json value cannot be parsed as type {nested: {list: [int], \
_}, _}"
)
def nested_object() =
let json.parse (x :
{
nested: {
tuple: (int * float * bool),
list: [float],
nullable_list: [int],
object_as_list: [(string*float)] as json.object,
"arbitrary object key ✨" as arbitrary_object_key: bool,
not_present: bool?
}
}
) = data
ignore(data)
end
test_parse_error(
"nested object",
nested_object,
'Parsing error: json value cannot be parsed as type {nested: {nullable_list: \
[int], _}, _}'
)
data =
'{"aabbcc": 34, "ddeerr": 54 }'
let json.parse (x : [(string*int)] as json.object) = data
test.equal(list.assoc("aabbcc", x), 34)
test.equal(list.assoc("ddeerr", x), 54)
data =
'{ "foo": 123 }'
let json.parse (x : {foo: string}?) = data
test.equal(x, null)
data =
'[ "gni", 123 ]'
let json.parse (x : [int]?) = data
test.equal(x, null)
let json.parse (x : (string * int * bool)?) = data
test.equal(x, null)
data =
'[ "gni", 123, "gno" ]'
let json.parse (x : (string * int)) = data
test.equal(x, ("gni", 123))
data = '{
"foo": {
"gni": {
"bla": 123
}
}
}'
let json.parse x = data
test.equal(x?.foo.gni?.bla, null(123))
data = '{
"foo": {}
}'
let json.parse x = data
test.equal(x?.foo.gni?.bla, null)
data = '{}'
let json.parse x = data
test.equal(x?.foo?.gni?.bla, null)
# Test escaping of invalid utf8 strings.
s =
"S\x00e\x00k\x00e\x00r\x00n\x00u\x00p\x00 \
\x00K\x00i\x00n\x00g\x00e\x00r\x00p\x00\xE2\x00"
test.equal(
json.stringify(s),
"\"S\\u0000e\\u0000k\\u0000e\\u0000r\\u0000n\\u0000u\\u0000p\\u0000 \
\\u0000K\\u0000i\\u0000n\\u0000g\\u0000e\\u0000r\\u0000p\\u0000\\uFFFD\""
)
j = json.object()
j.add("foo", 1)
j.add("bla", "bar")
j.add("baz", 3.14)
j.add("key_with_methods", "value".{method=123})
j.add("record", {a=1, b="ert"})
j.remove("foo")
j = json.stringify(j)
test.equal(
j,
'{
"record": { "a": 1, "b": "ert" },
"key_with_methods": "value",
"bla": "bar",
"baz": 3.14
}'
)
e = ref(null)
def f(data) =
try
d = json.stringify(data)
ignore(d)
catch err do
e := err
end
end
f(infinity)
if not null.defined(e()) then test.fail() end
test.equal(json.stringify(json.value(123)), "123")
test.equal(json.stringify(json.value(true)), "true")
test.pass()
end
test.check(f)
|