2020-11-29 16:54:45 +03:00
|
|
|
import x.json2
|
|
|
|
|
|
|
|
fn test_raw_decode_string() {
|
|
|
|
str := json2.raw_decode('"Hello!"') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
assert str.str() == 'Hello!'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_number() {
|
|
|
|
num := json2.raw_decode('123') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
assert num.int() == 123
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_array() {
|
|
|
|
raw_arr := json2.raw_decode('["Foo", 1]') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
arr := raw_arr.arr()
|
|
|
|
assert arr[0].str() == 'Foo'
|
|
|
|
assert arr[1].int() == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_bool() {
|
|
|
|
bol := json2.raw_decode('false') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
assert bol.bool() == false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_map() {
|
|
|
|
raw_mp := json2.raw_decode('{"name":"Bob","age":20}') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
mp := raw_mp.as_map()
|
|
|
|
assert mp['name'].str() == 'Bob'
|
|
|
|
assert mp['age'].int() == 20
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_null() {
|
|
|
|
nul := json2.raw_decode('null') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
assert nul is json2.Null
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_invalid() {
|
|
|
|
json2.raw_decode('1z') or {
|
2021-02-28 22:24:29 +03:00
|
|
|
assert err.msg == '[x.json2] invalid token `z` (0:17)'
|
2020-11-29 16:54:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert false
|
|
|
|
}
|
2021-02-26 09:36:02 +03:00
|
|
|
|
|
|
|
fn test_raw_decode_string_with_dollarsign() {
|
|
|
|
str := json2.raw_decode(r'"Hello $world"') or {
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2021-02-26 09:36:02 +03:00
|
|
|
}
|
|
|
|
assert str.str() == r'Hello $world'
|
|
|
|
}
|
2021-03-30 10:40:20 +03:00
|
|
|
|
|
|
|
fn test_raw_decode_map_with_whitespaces() {
|
|
|
|
raw_mp := json2.raw_decode(' \n\t{"name":"Bob","age":20}\n\t') or {
|
|
|
|
eprintln(err.msg)
|
|
|
|
assert false
|
2021-04-11 11:28:52 +03:00
|
|
|
json2.Any(json2.null)
|
2021-03-30 10:40:20 +03:00
|
|
|
}
|
|
|
|
mp := raw_mp.as_map()
|
|
|
|
assert mp['name'].str() == 'Bob'
|
|
|
|
assert mp['age'].int() == 20
|
|
|
|
}
|