2022-11-20 12:18:14 +03:00
|
|
|
import x.json2 as json
|
2020-09-10 13:05:40 +03:00
|
|
|
|
|
|
|
enum JobTitle {
|
|
|
|
manager
|
|
|
|
executive
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Employee {
|
2020-11-29 16:54:45 +03:00
|
|
|
pub mut:
|
2020-09-10 13:05:40 +03:00
|
|
|
name string
|
|
|
|
age int
|
|
|
|
salary f32
|
|
|
|
title JobTitle
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:11:55 +03:00
|
|
|
fn test_fast_raw_decode() {
|
|
|
|
s := '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
|
2022-11-20 12:18:14 +03:00
|
|
|
o := json.fast_raw_decode(s) or {
|
2020-10-09 17:11:55 +03:00
|
|
|
assert false
|
2022-11-20 12:18:14 +03:00
|
|
|
json.Any(json.null)
|
2020-10-09 17:11:55 +03:00
|
|
|
}
|
|
|
|
str := o.str()
|
|
|
|
assert str == '{"name":"Peter","age":"28","salary":"95000.5","title":"2"}'
|
|
|
|
}
|
|
|
|
|
2020-11-15 15:58:17 +03:00
|
|
|
fn test_character_unescape() {
|
2021-02-26 09:36:02 +03:00
|
|
|
message := r'{
|
|
|
|
"newline": "new\nline",
|
|
|
|
"tab": "\ttab",
|
|
|
|
"backslash": "back\\slash",
|
|
|
|
"quotes": "\"quotes\"",
|
|
|
|
"slash":"\/dev\/null"
|
|
|
|
}'
|
2022-11-20 12:18:14 +03:00
|
|
|
mut obj := json.raw_decode(message) or {
|
2021-02-26 09:36:02 +03:00
|
|
|
println(err)
|
2020-11-15 15:58:17 +03:00
|
|
|
assert false
|
2021-02-26 09:36:02 +03:00
|
|
|
return
|
2020-11-15 15:58:17 +03:00
|
|
|
}
|
|
|
|
lines := obj.as_map()
|
2021-09-10 16:07:39 +03:00
|
|
|
assert lines['newline'] or { 0 }.str() == 'new\nline'
|
|
|
|
assert lines['tab'] or { 0 }.str() == '\ttab'
|
|
|
|
assert lines['backslash'] or { 0 }.str() == 'back\\slash'
|
|
|
|
assert lines['quotes'] or { 0 }.str() == '"quotes"'
|
|
|
|
assert lines['slash'] or { 0 }.str() == '/dev/null'
|
2020-11-15 15:58:17 +03:00
|
|
|
}
|