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:
parent
7b9cca7524
commit
2f75ce0d4c
@ -5,6 +5,7 @@ module toml
|
||||
|
||||
import time
|
||||
import toml.util
|
||||
import x.json2
|
||||
|
||||
// Pretty much all the same builtin types as the `json2.Any` type plus `time.Time`
|
||||
pub type Any = Null
|
||||
@ -188,16 +189,23 @@ pub fn (a Any) to_json() string {
|
||||
Null {
|
||||
return 'null'
|
||||
}
|
||||
time.Time {
|
||||
json_text := json2.Any(a.format_ss_micro())
|
||||
return '"$json_text.json_str()"'
|
||||
}
|
||||
string {
|
||||
return '"$a.str()"'
|
||||
json_text := json2.Any(a.str())
|
||||
return '"$json_text.json_str()"'
|
||||
}
|
||||
bool, f32, f64, i64, int, u64 {
|
||||
return a.str()
|
||||
json_text := json2.Any(a.str())
|
||||
return json_text.json_str()
|
||||
}
|
||||
map[string]Any {
|
||||
mut str := '{'
|
||||
for key, val in a {
|
||||
str += ' "$key": $val.to_json(),'
|
||||
json_key := json2.Any(key)
|
||||
str += ' "$json_key.json_str()": $val.to_json(),'
|
||||
}
|
||||
str = str.trim_right(',')
|
||||
str += ' }'
|
||||
@ -212,8 +220,54 @@ pub fn (a Any) to_json() string {
|
||||
str += ' ]'
|
||||
return str
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// to_json_any returns `Any` as a `x.json2.Any` type.
|
||||
pub fn (a Any) to_json_any() json2.Any {
|
||||
match a {
|
||||
Null {
|
||||
return json2.Null{}
|
||||
}
|
||||
time.Time {
|
||||
return '"$a.format_ss_micro()"'
|
||||
return json2.Any(a.format_ss_micro())
|
||||
}
|
||||
string {
|
||||
return json2.Any(a.str())
|
||||
}
|
||||
bool {
|
||||
return json2.Any(bool(a))
|
||||
}
|
||||
int {
|
||||
return json2.Any(int(a))
|
||||
}
|
||||
f32 {
|
||||
return json2.Any(f32(a))
|
||||
}
|
||||
f64 {
|
||||
return json2.Any(f64(a))
|
||||
}
|
||||
i64 {
|
||||
return json2.Any(i64(a))
|
||||
}
|
||||
u64 {
|
||||
return json2.Any(u64(a))
|
||||
}
|
||||
map[string]Any {
|
||||
mut jmap := map[string]json2.Any{}
|
||||
for key, val in a {
|
||||
jmap[key] = val.to_json_any()
|
||||
}
|
||||
return jmap
|
||||
}
|
||||
[]Any {
|
||||
mut jarr := []json2.Any{}
|
||||
|
||||
for val in a {
|
||||
jarr << val.to_json_any()
|
||||
}
|
||||
|
||||
return jarr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 += ' }'
|
||||
|
19
vlib/toml/tests/json_encoding_test.v
Normal file
19
vlib/toml/tests/json_encoding_test.v
Normal file
@ -0,0 +1,19 @@
|
||||
import os
|
||||
import toml
|
||||
|
||||
fn test_parse() {
|
||||
toml_file :=
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.toml'
|
||||
toml_doc := toml.parse(toml_file) or { panic(err) }
|
||||
|
||||
toml_json := toml_doc.to_json()
|
||||
out_file :=
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.out'
|
||||
out_file_json := os.read_file(out_file) or { panic(err) }
|
||||
println(toml_json)
|
||||
assert toml_json == out_file_json
|
||||
|
||||
// assert false
|
||||
}
|
1
vlib/toml/tests/testdata/json_encoding_test.out
vendored
Normal file
1
vlib/toml/tests/testdata/json_encoding_test.out
vendored
Normal file
@ -0,0 +1 @@
|
||||
{ "\u3072\u3089\u304c\u306a": "\u3072\u3089" }
|
1
vlib/toml/tests/testdata/json_encoding_test.toml
vendored
Normal file
1
vlib/toml/tests/testdata/json_encoding_test.toml
vendored
Normal file
@ -0,0 +1 @@
|
||||
"ひらがな" = 'ひら'
|
Loading…
Reference in New Issue
Block a user