12 std::cout <<
"\nπΉ Parsing YAML from string:\n";
13 std::string yamlInput = R
"(
28 auto nodeYaml = yamlParser.
parse(yamlInput);
29 nodeYaml->printJSON();
32 auto name = std::get<BlockName>(nodeYaml->get(
"name"));
33 std::cout <<
"β
Name: " << name <<
"\n";
34 }
catch (
const std::exception& e) {
35 std::cerr <<
"β Error: " << e.what() <<
"\n";
39 auto city = std::get<BlockName>(nodeYaml->get(
"location",
"city"));
40 std::cout <<
"β
City: " << city <<
"\n";
41 }
catch (
const std::exception& e) {
42 std::cerr <<
"β Error: " << e.what() <<
"\n";
46 auto skill1 = std::get<BlockName>(nodeYaml->get(
"skills",
"0"));
47 auto skill2 = std::get<BlockName>(nodeYaml->get(
"skills",
"1"));
48 auto skill3 = std::get<BlockName>(nodeYaml->get(
"skills",
"2"));
49 std::cout <<
"β
Skills: " << skill1 <<
", " << skill2 <<
", " << skill3 <<
"\n";
50 }
catch (
const std::exception& e) {
51 std::cerr <<
"β Error: " << e.what() <<
"\n";
55 auto group = nodeYaml->getGroup({
"location"});
56 std::cout <<
"β
Group 'location':\n";
57 for (
const auto& [key, val] : group) {
58 std::cout <<
"- " << key <<
" : ";
59 if (std::holds_alternative<BlockName>(val)) {
60 std::cout << std::get<BlockName>(val);
61 }
else if (std::holds_alternative<int>(val)) {
62 std::cout << std::get<int>(val);
66 }
catch (
const std::exception& e) {
67 std::cerr <<
"β Error: " << e.what() <<
"\n";
70 std::map<BlockName, DBNode::Value> newGroup = {
71 {{
"hobby",
"hobbies"},
"cycling"},
74 nodeYaml->setGroup({
"extra"}, newGroup);
75 nodeYaml->printJSON();
76 std::cout <<
"\nβ
Structure YAML after setGroup:\n";
77 auto truc = std::get<BlockName>(nodeYaml->get(
"extra",
"hobby"));
78 std::cout <<
"test BlockName special : " <<truc << std::endl;
79 std::cout <<
"\nπΉ Writing YAML to file...\n";
80 std::ofstream file(
"test_output.yaml");
82 nodeYaml->printYAML();
84 std::cout <<
"β
YAML File written successfully.\n";
86 std::cerr <<
"β Error writing YAML file.\n";
89 std::cout <<
"\nπΉ Checking written YAML content:\n";
90 std::ifstream fileCheck(
"test_output.yaml");
91 if (fileCheck.is_open()) {
92 std::ostringstream ossCheck;
93 ossCheck << fileCheck.rdbuf();
94 std::cout << ossCheck.str() <<
"\n";
97 std::cerr <<
"β Error checking YAML file content.\n";
100 std::cout <<
"\nπΉ Reading YAML from file...\n";
101 std::ifstream fileRead(
"test_output.yaml");
102 if (fileRead.is_open()) {
103 std::ostringstream oss;
104 oss << fileRead.rdbuf();
105 std::cout <<
"β
YAML File read before parsing:\n" << oss.str() <<
"\n";
106 auto parsedFileNode = yamlParser.
parse(oss.str());
107 std::cout <<
"β
YAML File read successfully:\n";
108 parsedFileNode->printJSON();
111 std::cerr <<
"β Error reading YAML file.\n";
121 std::string jsonInput = R
"({
126 "skills": ["C++", "Python", "YAML"]
129 std::cout << "πΉ Parsing JSON:\n";
130 auto nodeJson = jsonParser.
parse(jsonInput);
131 nodeJson->printJSON();
133 std::string yamlInput = R
"(
143 std::cout << "\nπΉ Parsing YAML:\n";
144 auto nodeYaml = yamlParser.
parse(yamlInput);
145 nodeYaml->printJSON();
147 std::cout <<
"\nπΉ Testing Node Manipulations:\n";
148 auto root = std::make_shared<DBNode>();
149 root->set(
"Alice",
"name");
150 root->set(1.5e-2,
"age");
151 root->set(
true,
"isAdmin");
152 root->set(
"Paris",
"address",
"city");
153 root->set(75001,
"address",
"zip");
154 auto listNode = std::make_shared<DBNode>();
155 listNode->set(
"C++",
"0");
156 listNode->set(
"Python",
"1");
157 listNode->set(
"YAML",
"2");
158 root->set(listNode,
"skills");
160 std::cout <<
" Generated JSON from DBNode:\n";
164 auto name = std::get<BlockName>(root->get(
"name"));
165 std::cout <<
"β
Name: " << name <<
"\n";
166 }
catch (
const std::exception& e) {
167 std::cerr <<
"β Error: " << e.what() <<
"\n";
171 auto city = std::get<BlockName>(root->get(
"address",
"city"));
172 std::cout <<
"β
Address city: " << city <<
"\n";
173 }
catch (
const std::exception& e) {
174 std::cerr <<
"β Error: " << e.what() <<
"\n";
178 auto skill1 = std::get<BlockName>(root->get(
"skills",
"0"));
179 auto skill2 = std::get<BlockName>(root->get(
"skills",
"1"));
180 auto skill3 = std::get<BlockName>(root->get(
"skills",
"2"));
181 std::cout <<
"β
Skills: " << skill1 <<
", " << skill2 <<
", " << skill3 <<
"\n";
182 }
catch (
const std::exception& e) {
183 std::cerr <<
"β Error: " << e.what() <<
"\n";
187 auto group = root->getGroup({
"address"});
188 std::cout <<
"β
Group 'address':\n";
189 for (
const auto& [key, val] : group) {
190 std::cout <<
"- " << key <<
" : ";
191 if (std::holds_alternative<BlockName>(val)) {
192 std::cout << std::get<BlockName>(val);
193 }
else if (std::holds_alternative<int>(val)) {
194 std::cout << std::get<int>(val);
198 }
catch (
const std::exception& e) {
199 std::cerr <<
"β Error: " << e.what() <<
"\n";
202 std::map<BlockName, DBNode::Value> newGroup = {
203 {
"newKey1",
"newValue1"},
207 root->setGroup({
"newSection"}, newGroup);
208 std::cout <<
"\nβ
Structure JSON after setGroup:\n";
211 std::cout <<
"\nπΉ Writing to file...\n";
212 std::ofstream file(
"test_output.json");
213 if (file.is_open()) {
214 root->printJSONToStream(file);
216 std::cout <<
"β
File written successfully.\n";
218 std::cerr <<
"β Error writing file.\n";