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

toml: use json2 to encode output from .to_json() (#12470)

This commit is contained in:
Larpon
2021-11-16 07:41:37 +01:00
committed by GitHub
parent 7b9cca7524
commit 2f75ce0d4c
5 changed files with 86 additions and 7 deletions

View File

@ -4,6 +4,7 @@
module ast
import toml.token
import x.json2
// Key is a sumtype representing all types of keys that
// can be found in a TOML document.
@ -28,15 +29,18 @@ pub type Value = Bool
pub fn (v Value) to_json() string {
match v {
Quoted, Date, DateTime, Time {
return '"$v.text"'
json_text := json2.Any(v.text)
return '"$json_text.json_str()"'
}
Bool, Null, Number {
return v.text
json_text := json2.Any(v.text)
return json_text.json_str()
}
map[string]Value {
mut str := '{'
for key, val in v {
str += ' "$key": $val.to_json(),'
json_key := json2.Any(key)
str += ' "$json_key.json_str()": $val.to_json(),'
}
str = str.trim_right(',')
str += ' }'