kenany / json2toml

Convert JSON to TOML
MIT License
15 stars 5 forks source link

Arrays that start with an object followed by values of other types are rendered incorrectly #92

Open dmbaturin opened 3 years ago

dmbaturin commented 3 years ago

For the record: I'm not actively using this library now, so I don't demand a quick fix, this is just FIY.

The new array of tables feature has an unhandled edge case related to mixed-type arrays. The isObjectArrayOfTables function reports a false positive when an array starts with a table but has values of different types in its tail.

$ node
Welcome to Node.js v14.17.0.
Type ".help" for more information.
> var json2toml = require('json2toml');
undefined

// If an array starts with a non-table object work as expected
> console.log(json2toml({"foo": [1, {"bar": false}, 9, true]}))
foo = [1,{"bar":false},9,true]

// Arrays that consist entirely of tables are correctly rendered using the double bracket syntax
> console.log(json2toml({"foo": [{"bar": false}, {"baz": true}]}))
[[foo]]
bar = false
[[foo]]
baz = true

// BUT! Arrays that start with a table followed by something else are rendered with data loss
> console.log(json2toml({"foo": [{"bar": false}, 9, true]}))
[[foo]]
bar = false
[[foo]]
[[foo]]
kenany commented 3 years ago

FYI @mcaruso85

mcaruso85 commented 3 years ago

I created. a PR to fix this issue.

There is no specification for mixed array in toml. So with this FIX when array is mixed, it behaves like before. If array is array of objects then it follows the spec to have the array of objects

So basically :

console.log(json2toml({"foo": [{"bar": false}, 9, true]})) foo = [{"bar":false},9,true]

https://github.com/KenanY/json2toml/pull/93