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

pull/12481/head
Larpon 2021-11-16 07:41:37 +01:00 committed by GitHub
parent 7b9cca7524
commit 2f75ce0d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 7 deletions

View File

@ -5,6 +5,7 @@ module toml
import time import time
import toml.util import toml.util
import x.json2
// Pretty much all the same builtin types as the `json2.Any` type plus `time.Time` // Pretty much all the same builtin types as the `json2.Any` type plus `time.Time`
pub type Any = Null pub type Any = Null
@ -188,16 +189,23 @@ pub fn (a Any) to_json() string {
Null { Null {
return 'null' return 'null'
} }
time.Time {
json_text := json2.Any(a.format_ss_micro())
return '"$json_text.json_str()"'
}
string { string {
return '"$a.str()"' json_text := json2.Any(a.str())
return '"$json_text.json_str()"'
} }
bool, f32, f64, i64, int, u64 { bool, f32, f64, i64, int, u64 {
return a.str() json_text := json2.Any(a.str())
return json_text.json_str()
} }
map[string]Any { map[string]Any {
mut str := '{' mut str := '{'
for key, val in a { 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 = str.trim_right(',')
str += ' }' str += ' }'
@ -212,8 +220,54 @@ pub fn (a Any) to_json() string {
str += ' ]' str += ' ]'
return 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 { 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
} }
} }
} }

View File

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

View 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
}

View File

@ -0,0 +1 @@
{ "\u3072\u3089\u304c\u306a": "\u3072\u3089" }

View File

@ -0,0 +1 @@
"ひらがな" = 'ひら'