fix toml encoder
This commit is contained in:
parent
d9d65a169c
commit
9cd95bb0eb
@ -239,13 +239,52 @@ dv::value toml::parse(std::string_view file, std::string_view source) {
|
||||
return TomlReader(file, source).read();
|
||||
}
|
||||
|
||||
static void to_string(std::stringstream& ss, const dv::value& value);
|
||||
|
||||
static void list_to_string(std::stringstream& ss, const dv::value& list) {
|
||||
ss << "[";
|
||||
int index = 0;
|
||||
for (const auto& value : list) {
|
||||
if (index > 0) {
|
||||
ss << ", ";
|
||||
}
|
||||
to_string(ss, value);
|
||||
index++;
|
||||
}
|
||||
ss << "]";
|
||||
}
|
||||
|
||||
static void object_to_string(std::stringstream& ss, const dv::value& object) {
|
||||
ss << "{";
|
||||
int index = 0;
|
||||
for (const auto& [key, value] : object.asObject()) {
|
||||
if (index > 0) {
|
||||
ss << ", ";
|
||||
}
|
||||
ss << key << " = ";
|
||||
to_string(ss, value);
|
||||
index++;
|
||||
}
|
||||
ss << "}";
|
||||
}
|
||||
|
||||
static void to_string(std::stringstream& ss, const dv::value& value) {
|
||||
if (value.isObject()) {
|
||||
object_to_string(ss, value);
|
||||
} else if (value.isList()) {
|
||||
list_to_string(ss, value);
|
||||
} else {
|
||||
ss << value;
|
||||
}
|
||||
}
|
||||
|
||||
std::string toml::stringify(const dv::value& root, const std::string& name) {
|
||||
std::stringstream ss;
|
||||
if (!name.empty()) {
|
||||
ss << "[" << name << "]\n";
|
||||
}
|
||||
for (const auto& [key, value] : root.asObject()) {
|
||||
if (!value.isObject()) {
|
||||
if (!value.isObject() && !value.isList()) {
|
||||
ss << key << " = " << value << "\n";
|
||||
}
|
||||
}
|
||||
@ -253,6 +292,15 @@ std::string toml::stringify(const dv::value& root, const std::string& name) {
|
||||
if (value.isObject()) {
|
||||
ss << "\n" << toml::stringify(value,
|
||||
name.empty() ? key : name + "." + key);
|
||||
} else if (value.isList()) {
|
||||
ss << (name.empty() ? key : name + "." + key) << " = [";
|
||||
for (size_t i = 0; i < value.size(); i++) {
|
||||
if (i > 0) {
|
||||
ss << ", ";
|
||||
}
|
||||
to_string(ss, value[i]);
|
||||
}
|
||||
ss << "]";
|
||||
}
|
||||
}
|
||||
return ss.str();
|
||||
|
||||
@ -23,4 +23,5 @@ static int l_toml_parse(lua::State* L) {
|
||||
const luaL_Reg tomllib[] = {
|
||||
{"tostring", lua::wrap<l_toml_stringify>},
|
||||
{"parse", lua::wrap<l_toml_parse>},
|
||||
{NULL, NULL}};
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -26,6 +26,9 @@ TEST(TOML, EncodeDecode) {
|
||||
object["score"] = score;
|
||||
object["visible"] = visible;
|
||||
object["data"] = srcBytes;
|
||||
object["values"] = dv::list({
|
||||
5, 3, std::string("hello"), dv::object({{"number", 1234}})
|
||||
});
|
||||
|
||||
text = toml::stringify(object, "");
|
||||
std::cout << text << std::endl;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user