2022-11-20 12:18:14 +03:00
|
|
|
import x.json2 as json
|
2022-03-04 14:39:23 +03:00
|
|
|
import strings
|
2021-03-01 12:22:36 +03:00
|
|
|
|
|
|
|
fn test_json_string_characters() {
|
2022-11-20 12:18:14 +03:00
|
|
|
text := json.raw_decode(r'"\n\r\b\f\t\\\"\/"') or { '' }
|
2022-03-04 14:39:23 +03:00
|
|
|
assert text.json_str() == '"\\n\\r\\b\\f\\t\\\\\\"\\/"'
|
2021-03-01 12:22:36 +03:00
|
|
|
}
|
|
|
|
|
2021-11-18 08:34:00 +03:00
|
|
|
fn test_json_escape_low_chars() {
|
|
|
|
esc := '\u001b'
|
|
|
|
assert esc.len == 1
|
2022-11-20 12:18:14 +03:00
|
|
|
text := json.Any(esc)
|
2022-03-04 14:39:23 +03:00
|
|
|
assert text.json_str() == r'"\u001b"'
|
2021-11-18 08:34:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:22:36 +03:00
|
|
|
fn test_json_string() {
|
2022-11-20 12:18:14 +03:00
|
|
|
text := json.Any('te✔st')
|
2022-03-04 14:39:23 +03:00
|
|
|
assert text.json_str() == r'"te\u2714st"'
|
2022-11-20 12:18:14 +03:00
|
|
|
boolean := json.Any(true)
|
2021-12-15 13:20:05 +03:00
|
|
|
assert boolean.json_str() == 'true'
|
2022-11-20 12:18:14 +03:00
|
|
|
integer := json.Any(int(-5))
|
2021-12-15 13:20:05 +03:00
|
|
|
assert integer.json_str() == '-5'
|
2022-11-20 12:18:14 +03:00
|
|
|
u64integer := json.Any(u64(5000))
|
2021-12-15 13:20:05 +03:00
|
|
|
assert u64integer.json_str() == '5000'
|
2022-11-20 12:18:14 +03:00
|
|
|
i64integer := json.Any(i64(-17))
|
2021-12-15 13:20:05 +03:00
|
|
|
assert i64integer.json_str() == '-17'
|
2021-03-01 12:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_json_string_emoji() {
|
2022-11-20 12:18:14 +03:00
|
|
|
text := json.Any('🐈')
|
2022-03-04 14:39:23 +03:00
|
|
|
assert text.json_str() == r'" "'
|
2021-03-01 12:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_json_string_non_ascii() {
|
2022-11-20 12:18:14 +03:00
|
|
|
text := json.Any('ひらがな')
|
2022-03-04 14:39:23 +03:00
|
|
|
assert text.json_str() == r'"\u3072\u3089\u304c\u306a"'
|
2021-03-01 12:22:36 +03:00
|
|
|
}
|
2021-06-29 11:40:44 +03:00
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_utf8_strings_are_not_modified() {
|
2021-06-29 11:40:44 +03:00
|
|
|
original := '{"s":"Schilddrüsenerkrankungen"}'
|
2022-11-20 12:18:14 +03:00
|
|
|
deresult := json.raw_decode(original)!
|
2021-06-29 11:40:44 +03:00
|
|
|
assert deresult.str() == original
|
|
|
|
}
|
2022-03-04 14:39:23 +03:00
|
|
|
|
2022-11-18 12:09:24 +03:00
|
|
|
fn test_encoder_unescaped_utf32() ! {
|
2022-11-20 12:18:14 +03:00
|
|
|
jap_text := json.Any('ひらがな')
|
|
|
|
enc := json.Encoder{
|
2022-03-04 14:39:23 +03:00
|
|
|
escape_unicode: false
|
|
|
|
}
|
|
|
|
|
|
|
|
mut sb := strings.new_builder(20)
|
2022-11-18 12:09:24 +03:00
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
enc.encode_value(jap_text, mut sb)!
|
2022-03-04 14:39:23 +03:00
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
assert sb.str() == '"${jap_text}"'
|
2022-03-04 14:39:23 +03:00
|
|
|
sb.go_back_to(0)
|
|
|
|
|
2022-11-20 12:18:14 +03:00
|
|
|
emoji_text := json.Any('🐈')
|
2022-10-16 09:28:57 +03:00
|
|
|
enc.encode_value(emoji_text, mut sb)!
|
2022-11-15 16:53:13 +03:00
|
|
|
assert sb.str() == '"${emoji_text}"'
|
2022-03-04 14:39:23 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_encoder_prettify() {
|
2022-03-04 14:39:23 +03:00
|
|
|
obj := {
|
2022-11-20 12:18:14 +03:00
|
|
|
'hello': json.Any('world')
|
|
|
|
'arr': [json.Any('im a string'), [json.Any('3rd level')]]
|
2022-03-04 14:39:23 +03:00
|
|
|
'obj': {
|
2022-11-20 12:18:14 +03:00
|
|
|
'map': json.Any('map inside a map')
|
2022-03-04 14:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-20 12:18:14 +03:00
|
|
|
enc := json.Encoder{
|
2022-03-04 14:39:23 +03:00
|
|
|
newline: `\n`
|
|
|
|
newline_spaces_count: 2
|
|
|
|
}
|
|
|
|
mut sb := strings.new_builder(20)
|
2022-11-18 12:09:24 +03:00
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
enc.encode_value(obj, mut sb)!
|
2022-03-04 14:39:23 +03:00
|
|
|
assert sb.str() == '{
|
|
|
|
"hello": "world",
|
|
|
|
"arr": [
|
|
|
|
"im a string",
|
|
|
|
[
|
|
|
|
"3rd level"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"obj": {
|
|
|
|
"map": "map inside a map"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
}
|
2022-11-18 12:09:24 +03:00
|
|
|
|
|
|
|
pub struct Test {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_struct() {
|
2022-11-20 12:18:14 +03:00
|
|
|
enc := json.encode(Test{'hello!'})
|
2022-11-18 12:09:24 +03:00
|
|
|
assert enc == '{"val":"hello!"}'
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Uri {
|
|
|
|
protocol string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (u Uri) json_str() string {
|
|
|
|
return '"${u.protocol}://${u.path}"'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_encodable() {
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.encode(Uri{'file', 'path/to/file'}) == '"file://path/to/file"'
|
2022-11-18 12:09:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_array() {
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.encode([1, 2, 3]) == '[1,2,3]'
|
2022-11-18 12:09:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_simple() {
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.encode('hello!') == '"hello!"'
|
|
|
|
assert json.encode(1) == '1'
|
2022-11-18 12:09:24 +03:00
|
|
|
}
|