1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/x/json2/json2_test.v

47 lines
961 B
V
Raw Normal View History

import x.json2 as json
2020-09-10 13:05:40 +03:00
enum JobTitle {
manager
executive
worker
}
struct Employee {
pub mut:
2020-09-10 13:05:40 +03:00
name string
age int
salary f32
title JobTitle
}
fn test_fast_raw_decode() {
s := '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
o := json.fast_raw_decode(s) or {
assert false
json.Any(json.null)
}
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() {
message := r'{
"newline": "new\nline",
"tab": "\ttab",
"backslash": "back\\slash",
"quotes": "\"quotes\"",
"slash":"\/dev\/null"
}'
mut obj := json.raw_decode(message) or {
println(err)
2020-11-15 15:58:17 +03:00
assert false
return
2020-11-15 15:58:17 +03:00
}
lines := obj.as_map()
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
}