Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
main_parser.cpp
Go to the documentation of this file.
1#include "JsonParser.h"
2#include "YamlParser.h"
3#include "DBNode.h"
4#include <memory>
5#include <string>
6#include <iostream>
7#include <fstream>
8
9int main_YAML() {
10 YAMLParser yamlParser;
11
12 std::cout << "\nπŸ”Ή Parsing YAML from string:\n";
13 std::string yamlInput = R"(
14name: Bob
15age: 25
1615:
17 truc: 5
18 machin: 1.5
19 bidule: 1.8e-3
20location:
21 city: London
22 country: UK
23skills:
24 - JavaScript
25 - Rust
26 - Go
27)";
28 auto nodeYaml = yamlParser.parse(yamlInput);
29 nodeYaml->printJSON();
30
31 try {
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";
36 }
37
38 try {
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";
43 }
44
45 try {
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";
52 }
53
54 try {
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);
63 }
64 std::cout << "\n";
65 }
66 } catch (const std::exception& e) {
67 std::cerr << "❌ Error: " << e.what() << "\n";
68 }
69
70 std::map<BlockName, DBNode::Value> newGroup = {
71 {{"hobby","hobbies"}, "cycling"},
72 {"job", "engineer"}
73 };
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");
81 if (file.is_open()) {
82 nodeYaml->printYAML();
83 file.close();
84 std::cout << "βœ… YAML File written successfully.\n";
85 } else {
86 std::cerr << "❌ Error writing YAML file.\n";
87 }
88
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";
95 fileCheck.close();
96 } else {
97 std::cerr << "❌ Error checking YAML file content.\n";
98 }
99
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();
109 fileRead.close();
110 } else {
111 std::cerr << "❌ Error reading YAML file.\n";
112 }
113
114 return 0;
115}
116
117int main() {
118 JSONParser jsonParser;
119 YAMLParser yamlParser;
120
121 std::string jsonInput = R"({
122 "name": "Alice",
123 "age": 1.2e-1,
124 "details": {
125 "city": "Paris",
126 "skills": ["C++", "Python", "YAML"]
127 }
128 })";
129 std::cout << "πŸ”Ή Parsing JSON:\n";
130 auto nodeJson = jsonParser.parse(jsonInput);
131 nodeJson->printJSON();
132
133 std::string yamlInput = R"(
134name: Alice
135age: 30
136details:
137 city: Paris
138 skills:
139 - C++
140 - Python
141 - YAML
142)";
143 std::cout << "\nπŸ”Ή Parsing YAML:\n";
144 auto nodeYaml = yamlParser.parse(yamlInput);
145 nodeYaml->printJSON();
146
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");
159
160 std::cout << " Generated JSON from DBNode:\n";
161 root->printJSON();
162
163 try {
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";
168 }
169
170 try {
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";
175 }
176
177 try {
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";
184 }
185
186 try {
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);
195 }
196 std::cout << "\n";
197 }
198 } catch (const std::exception& e) {
199 std::cerr << "❌ Error: " << e.what() << "\n";
200 }
201
202 std::map<BlockName, DBNode::Value> newGroup = {
203 {"newKey1", "newValue1"},
204 {"newKey2", 12345},
205 {"newKey3", false},
206 };
207 root->setGroup({"newSection"}, newGroup);
208 std::cout << "\nβœ… Structure JSON after setGroup:\n";
209 root->printJSON();
210
211 std::cout << "\nπŸ”Ή Writing to file...\n";
212 std::ofstream file("test_output.json");
213 if (file.is_open()) {
214 root->printJSONToStream(file);
215 file.close();
216 std::cout << "βœ… File written successfully.\n";
217 } else {
218 std::cerr << "❌ Error writing file.\n";
219 }
220
221 // std::cout << "\nπŸ”Ή Reading from file...\n";
222 // std::ifstream fileRead("test_output.json");
223 // if (fileRead.is_open()) {
224 // std::ostringstream oss;
225 // oss << fileRead.rdbuf();
226 // auto parsedFileNode = jsonParser.parse(oss.str());
227 // std::cout << "βœ… File read successfully:\n";
228 // parsedFileNode->printJSON();
229 // fileRead.close();
230 // } else {
231 // std::cerr << "❌ Error reading file.\n";
232 // }
233
234
235
236
237 main_YAML();
238 return 0;
239}
Hierarchical key–value tree with JSON/YAML serialization.
Lightweight JSON parser/serializer for DBNode trees.
Lightweight YAML parser/serializer for DBNode trees.
JSON implementation of the IParser interface.
Definition JsonParser.h:42
std::shared_ptr< DBNode > parse(const std::string &input) const override
Parses a JSON document from an in-memory string.
Definition JsonParser.cpp:3
YAML implementation of the IParser interface.
Definition YamlParser.h:39
std::shared_ptr< DBNode > parse(const std::string &input) const override
Parses a YAML string into a DBNode hierarchy.
int main_YAML()
int main()