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
2022-12-10 18:44:20 +02:00

48 lines
973 B
V

import x.json2 as json
import time
enum JobTitle {
manager
executive
worker
}
struct Employee {
pub mut:
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"}'
}
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)
assert false
return
}
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'
}