Update json and toml tests

This commit is contained in:
daleclack 2023-01-30 18:09:46 +08:00
parent 59c5979188
commit abd30c88d9
6 changed files with 107 additions and 13 deletions

6
cpp/config.toml Normal file
View File

@ -0,0 +1,6 @@
[main]
height = 720
width = 1280
[others]
backgrounds = [ 'test1.png', 'test2.png', 'test3.png' ]

Binary file not shown.

View File

@ -19,22 +19,30 @@ void read_json_file()
void create_json_file()
{
// Create json data
json data = json::parse(R"(
{
data1 : {},
data2 : {}
}
)");
json data = json::parse(R"({
"error": {
"code": 101,
"message": "operation failed!"
},
"result": {
"backgrounds": ["test1.png", "test2.png", "test3.png"]
},
"data2": {}
})");
// Create json object for a key
// json data2 = json::parse(R"(
// {
// test1:2,
// test2:3
// }
// )");
json data2 = json::parse(R"({
"error": {
"code": 101,
"message": "operation failed!"
},
"result": {
"backgrounds": ["test1.png", "test2.png", "test3.png"]
},
"data2": {}
})");
// data["data2"] = data;
data["data2"] = data2;
// auto data2 = data["data2"];
// std::cout << data2 << std::endl;

View File

@ -0,0 +1,27 @@
{
"data2": {
"data2": {},
"error": {
"code": 101,
"message": "operation failed!"
},
"result": {
"backgrounds": [
"test1.png",
"test2.png",
"test3.png"
]
}
},
"error": {
"code": 101,
"message": "operation failed!"
},
"result": {
"backgrounds": [
"test1.png",
"test2.png",
"test3.png"
]
}
}

BIN
cpp/toml_test Normal file

Binary file not shown.

View File

@ -0,0 +1,53 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include "../toml/toml.hpp"
using namespace std::string_view_literals;
void save_toml_file()
{
static constexpr std::string_view some_toml = R"(
[main]
width = 1280
height = 720
[others]
backgrounds = ["test1.png", "test2.png", "test3.png"]
)"sv;
try
{
// parse directly from a string view:
toml::table tbl = toml::parse(some_toml);
std::cout << tbl << "\n";
// Save data to a file
std::fstream outfile;
outfile.open("config.toml", std::ios_base::out);
if (outfile.is_open())
{
outfile << tbl;
}
outfile.close();
// // parse from a string stream:
// {
// std::stringstream ss{ std::string{ some_toml } };
// toml::table tbl = toml::parse(ss);
// std::cout << tbl << "\n";
// }
}
catch (const toml::parse_error &err)
{
std::cerr << "Parsing failed:\n"
<< err << "\n";
return;
}
}
int main()
{
save_toml_file();
return 0;
}