Skip to content

toml::format incorrectly formats an empty array of tables which fails to be parsed later on #262

@ifexception

Description

@ifexception

I have a scenario where I am using Array of Tables and it can be empty in certain cases. When this occurs, toml11 will correctly parse the file and my program will initialize as the array of tables is formatted according to the toml specification.

However, during the operation of the program, the array of tables can be reset. When this occurs toml11 does not format an empty array of tables as I would expect and then throws a bad_format error when I later try to parse the toml file.

Expected Behavior

Given the following toml structure in code:

toml::value output(
	toml::table{
		{
			"array-of-tables",
			toml::array{}
		},
		{
			"subtable",
			toml::table{}
		},
	}
);

toml::format should return:

[[array-of-tables]]

[subtable]

Which should align to the spec

Current Behavior

Given the following toml structure in code:

toml::value output(
	toml::table{
		{
			"array-of-tables",
			toml::array{}
		},
		{
			"subtable",
			toml::table{}
		},
	}
);

toml::format returns:

array-of-tables = 

[subtable]

Which throws a bad format error when parsing:

[error] bad format: unknown value appeared
--> test.toml
|
1 | array-of-tables =0x0d(CARRIAGE RETURN)
| ^-- here

Steps to Reproduce

test.toml:

array-of-tables =

[subtable]

C++ MRE:

#include <iostream>
#include <string>

#include <toml.hpp>

int main()
{
    toml::value output(
        toml::table{
            {
                "array-of-tables",
                toml::array{}
            },
            {
                "subtable",
                toml::table{}
            },
        }
    );

    auto& aot = output.at("array-of-tables");
    aot.as_array_fmt().fmt = toml::array_format::array_of_tables;
    aot.as_array_fmt().body_indent = 4;
    aot.as_array_fmt().closing_indent = 2;

    std::string asString = toml::format(output);

    std::cout << asString << std::endl;

    try
    {
        auto p = toml::parse("test.toml");
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << "\n";
    }

    std::cin.get();
    return 0;
}

Context (Environment)

Visual Studio 2022 CMake project
toml11 installed via vcpkg
toml11 version 4.0.0
64 bits

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions