diff --git a/vlib/toml/any.v b/vlib/toml/any.v index f9b5756fdb..4c4a251393 100644 --- a/vlib/toml/any.v +++ b/vlib/toml/any.v @@ -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 } } } diff --git a/vlib/toml/ast/types.v b/vlib/toml/ast/types.v index 8e3ac873e0..fd260c1ccc 100644 --- a/vlib/toml/ast/types.v +++ b/vlib/toml/ast/types.v @@ -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 += ' }' diff --git a/vlib/toml/tests/json_encoding_test.v b/vlib/toml/tests/json_encoding_test.v new file mode 100644 index 0000000000..b38cc2de4c --- /dev/null +++ b/vlib/toml/tests/json_encoding_test.v @@ -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 +} diff --git a/vlib/toml/tests/testdata/json_encoding_test.out b/vlib/toml/tests/testdata/json_encoding_test.out new file mode 100644 index 0000000000..63e8d36c7a --- /dev/null +++ b/vlib/toml/tests/testdata/json_encoding_test.out @@ -0,0 +1 @@ +{ "\u3072\u3089\u304c\u306a": "\u3072\u3089" } \ No newline at end of file diff --git a/vlib/toml/tests/testdata/json_encoding_test.toml b/vlib/toml/tests/testdata/json_encoding_test.toml new file mode 100644 index 0000000000..2033916db7 --- /dev/null +++ b/vlib/toml/tests/testdata/json_encoding_test.toml @@ -0,0 +1 @@ +"ひらがな" = 'ひら'