fix: check all the elements in an array

while checking if the array is array-of-tables or not (heterogeneous
arrays are allowed, so there might be an array that has a table and
an integer at the same time)
This commit is contained in:
ToruNiina 2020-10-14 18:00:04 +09:00
parent 2e41a26785
commit f7bfcdd7aa

View File

@ -244,7 +244,17 @@ struct serializer
std::string operator()(const array_type& v) const
{
if(!v.empty() && v.front().is_table())// v is an array of tables
if(v.empty())
{
return std::string("[]");
}
// Since TOML v0.5.0, heterogeneous arrays are allowed. So we need to
// check all the element in an array to check if the array is an array
// of tables.
const bool is_array_of_tables = std::all_of(v.begin(), v.end(),
[](const value_type& elem) {return elem.is_table();});
if(is_array_of_tables)
{
// if it's not inlined, we need to add `[[table.key]]`.
// but if it can be inlined,
@ -322,10 +332,6 @@ struct serializer
}
return token;
}
if(v.empty())
{
return std::string("[]");
}
// not an array of tables. normal array.
// first, try to make it inline if none of the elements have a comment.