2 clippy
::match_like_matches_macro
,
3 clippy
::needless_pass_by_value
,
4 clippy
::uninlined_format_args
7 use serde_json
::{json, Value}
;
9 fn to_json(toml
: Value
) -> Value
{
10 fn doit(s
: &str, json
: Value
) -> Value
{
11 json
!({ "type": s, "value": json }
)
15 Value
::Null
=> unreachable
!(),
16 Value
::String(s
) => doit("string", Value
::String(s
)),
18 let repr
= n
.to_string();
19 if repr
.contains('
.'
) {
20 let float
: f64 = repr
.parse().unwrap();
21 let mut repr
= format
!("{:.15}", float
);
22 repr
.truncate(repr
.trim_end_matches('
0'
).len());
23 if repr
.ends_with('
.'
) {
26 doit("float", Value
::String(repr
))
28 doit("integer", Value
::String(repr
))
31 Value
::Bool(b
) => doit("bool", Value
::String(format
!("{}", b
))),
32 Value
::Array(arr
) => {
33 let is_table
= match arr
.first() {
34 Some(&Value
::Object(_
)) => true,
37 let json
= Value
::Array(arr
.into_iter().map(to_json
).collect());
44 Value
::Object(table
) => {
45 let mut map
= serde_json
::Map
::new();
47 map
.insert(k
, to_json(v
));
54 fn run(toml_raw
: &str, json_raw
: &str) {
55 println
!("parsing:\n{}", toml_raw
);
56 let toml
: Value
= basic_toml
::from_str(toml_raw
).unwrap();
57 let json
: Value
= serde_json
::from_str(json_raw
).unwrap();
59 // Assert toml == json
60 let toml_json
= to_json(toml
.clone());
63 "expected\n{}\ngot\n{}\n",
64 serde_json
::to_string_pretty(&json
).unwrap(),
65 serde_json
::to_string_pretty(&toml_json
).unwrap()
69 println
!("round trip parse: {}", toml
);
70 let toml2
: Value
= basic_toml
::from_str(&basic_toml
::to_string(&toml
).unwrap()).unwrap();
71 assert_eq
!(toml
, toml2
);
74 macro_rules
! test( ($name
:ident
, $toml
:expr
, $json
:expr
) => (
76 fn $
name() { run($toml, $json); }
81 include_str
!("valid/array-empty.toml"),
82 include_str
!("valid/array-empty.json")
86 include_str
!("valid/array-nospaces.toml"),
87 include_str
!("valid/array-nospaces.json")
91 include_str
!("valid/arrays-hetergeneous.toml"),
92 include_str
!("valid/arrays-hetergeneous.json")
97 include_str
!("valid/arrays.toml"),
98 include_str
!("valid/arrays.json")
102 include_str
!("valid/arrays-nested.toml"),
103 include_str
!("valid/arrays-nested.json")
106 array_mixed_types_ints_and_floats
,
107 include_str
!("valid/array-mixed-types-ints-and-floats.toml"),
108 include_str
!("valid/array-mixed-types-ints-and-floats.json")
111 array_mixed_types_arrays_and_ints
,
112 include_str
!("valid/array-mixed-types-arrays-and-ints.toml"),
113 include_str
!("valid/array-mixed-types-arrays-and-ints.json")
116 array_mixed_types_strings_and_ints
,
117 include_str
!("valid/array-mixed-types-strings-and-ints.toml"),
118 include_str
!("valid/array-mixed-types-strings-and-ints.json")
122 include_str
!("valid/empty.toml"),
123 include_str
!("valid/empty.json")
127 include_str
!("valid/bool.toml"),
128 include_str
!("valid/bool.json")
132 include_str
!("valid/comments-everywhere.toml"),
133 include_str
!("valid/comments-everywhere.json")
138 include_str
!("valid/datetime.toml"),
139 include_str
!("valid/datetime.json")
144 include_str
!("valid/example.toml"),
145 include_str
!("valid/example.json")
149 include_str
!("valid/float.toml"),
150 include_str
!("valid/float.json")
154 implicit_and_explicit_after
,
155 include_str
!("valid/implicit-and-explicit-after.toml"),
156 include_str
!("valid/implicit-and-explicit-after.json")
160 implicit_and_explicit_before
,
161 include_str
!("valid/implicit-and-explicit-before.toml"),
162 include_str
!("valid/implicit-and-explicit-before.json")
166 include_str
!("valid/implicit-groups.toml"),
167 include_str
!("valid/implicit-groups.json")
171 include_str
!("valid/integer.toml"),
172 include_str
!("valid/integer.json")
176 include_str
!("valid/key-equals-nospace.toml"),
177 include_str
!("valid/key-equals-nospace.json")
181 include_str
!("valid/key-space.toml"),
182 include_str
!("valid/key-space.json")
186 include_str
!("valid/key-special-chars.toml"),
187 include_str
!("valid/key-special-chars.json")
191 include_str
!("valid/key-with-pound.toml"),
192 include_str
!("valid/key-with-pound.json")
196 include_str
!("valid/key-empty.toml"),
197 include_str
!("valid/key-empty.json")
201 include_str
!("valid/long-float.toml"),
202 include_str
!("valid/long-float.json")
206 include_str
!("valid/long-integer.toml"),
207 include_str
!("valid/long-integer.json")
211 include_str
!("valid/multiline-string.toml"),
212 include_str
!("valid/multiline-string.json")
215 raw_multiline_string
,
216 include_str
!("valid/raw-multiline-string.toml"),
217 include_str
!("valid/raw-multiline-string.json")
221 include_str
!("valid/raw-string.toml"),
222 include_str
!("valid/raw-string.json")
226 include_str
!("valid/string-empty.toml"),
227 include_str
!("valid/string-empty.json")
231 include_str
!("valid/string-escapes.toml"),
232 include_str
!("valid/string-escapes.json")
236 include_str
!("valid/string-simple.toml"),
237 include_str
!("valid/string-simple.json")
241 include_str
!("valid/string-with-pound.toml"),
242 include_str
!("valid/string-with-pound.json")
245 table_array_implicit
,
246 include_str
!("valid/table-array-implicit.toml"),
247 include_str
!("valid/table-array-implicit.json")
251 include_str
!("valid/table-array-many.toml"),
252 include_str
!("valid/table-array-many.json")
256 include_str
!("valid/table-array-nest.toml"),
257 include_str
!("valid/table-array-nest.json")
261 include_str
!("valid/table-array-one.toml"),
262 include_str
!("valid/table-array-one.json")
266 include_str
!("valid/table-empty.toml"),
267 include_str
!("valid/table-empty.json")
271 include_str
!("valid/table-sub-empty.toml"),
272 include_str
!("valid/table-sub-empty.json")
276 include_str
!("valid/table-multi-empty.toml"),
277 include_str
!("valid/table-multi-empty.json")
281 include_str
!("valid/table-whitespace.toml"),
282 include_str
!("valid/table-whitespace.json")
286 include_str
!("valid/table-with-pound.toml"),
287 include_str
!("valid/table-with-pound.json")
291 include_str
!("valid/unicode-escape.toml"),
292 include_str
!("valid/unicode-escape.json")
296 include_str
!("valid/unicode-literal.toml"),
297 include_str
!("valid/unicode-literal.json")
302 include_str
!("valid/hard_example.toml"),
303 include_str
!("valid/hard_example.json")
308 include_str
!("valid/example2.toml"),
309 include_str
!("valid/example2.json")
314 include_str
!("valid/example-v0.3.0.toml"),
315 include_str
!("valid/example-v0.3.0.json")
320 include_str
!("valid/example-v0.4.0.toml"),
321 include_str
!("valid/example-v0.4.0.json")
326 include_str
!("valid/example-bom.toml"),
327 include_str
!("valid/example.json")
333 include_str
!("valid/datetime-truncate.toml"),
334 include_str
!("valid/datetime-truncate.json")
338 include_str
!("valid/key-quote-newline.toml"),
339 include_str
!("valid/key-quote-newline.json")
342 table_array_nest_no_keys
,
343 include_str
!("valid/table-array-nest-no-keys.toml"),
344 include_str
!("valid/table-array-nest-no-keys.json")
348 include_str
!("valid/dotted-keys.toml"),
349 include_str
!("valid/dotted-keys.json")
353 quote_surrounded_value
,
354 include_str
!("valid/quote-surrounded-value.toml"),
355 include_str
!("valid/quote-surrounded-value.json")
360 include_str
!("valid/float-exponent.toml"),
361 include_str
!("valid/float-exponent.json")
366 include_str
!("valid/string-delim-end.toml"),
367 include_str
!("valid/string-delim-end.json")