-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToPlain.cpp
107 lines (93 loc) · 4.64 KB
/
ToPlain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* \file ToPlain.cpp
* \brief nlohmann::json
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#include <nlohmann/json.hpp>
//-------------------------------------------------------------------------------------------------
// Recursive function to convert JSON to plain text without removing HTML tags
std::string
jsonToPlainText(
const nlohmann::json &a_json,
const std::string &a_indent = ""
)
{
std::string result;
const std::string nl = "\n";
// If the current element is an object, iterate over its key-value pairs
if ( a_json.is_object() ) {
for (auto it_node = a_json.begin(); it_node != a_json.end(); ++ it_node) {
result += a_indent + it_node.key() + ":" + nl;
result += jsonToPlainText(it_node.value(), a_indent + " ");
}
}
// If the current element is an array, iterate over its elements
else if ( a_json.is_array() ) {
for (const auto &it_node : a_json) {
if (it_node.is_object() || it_node.is_array()) {
result += jsonToPlainText(it_node, a_indent + " ");
}
else if ( it_node.is_string() ) {
result += a_indent + " - " + it_node.get<std::string>() + nl;
}
else {
result += a_indent + " - " + it_node.dump() + nl;
}
}
}
// If the current element is a string, number, or boolean, print it directly
else if (a_json.is_string()) {
result += a_indent + a_json.get<std::string>() + nl;
}
else {
result += a_indent + a_json.dump() + nl; // For numbers and booleans
}
return result;
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
// JSON data as a string
const std::string jsonString = R"(
{
"paymentPolicy": {
"localCurrency": "EUR",
"propertyFees": [
"<p>You'll be asked to pay the following charges at the property:<\/p>",
"<ul><li>A tax is imposed by the city and collected at the property. Exemptions or reductions might apply. For more details, please contact the property using the information on the reservation confirmation received after booking. <\/li><li>An effective city/local tax rate of 5.00 percent will be charged<\/li><li>Deposit is payable by bank transfer and is due within 72 hours of booking the reservation.<\/li><\/ul>",
"<p>We have included all charges provided to us by the property. <\/p>"
],
"optionalExtras": [
"<ul> <li>Fee for buffet breakfast: approximately EUR 23 for adults and EUR 11.5 for children<\/li><li>Self parking fee: EUR 22 per day<\/li><li>Nearby parking fee: EUR 13 per day (328 ft away)<\/li><li>Pet fee: EUR 20 per pet, per stay<\/li><li>Service animals are exempt from fees<\/li><\/ul>",
"<p>The above list may not be comprehensive. Fees and deposits may not include tax and are subject to change. <\/p>"
]
},
"formsOfPayment": [
"American Express",
"Diners Club",
"JCB International",
"Mastercard",
"Visa"
]
})";
std::cout << jsonString << "\n----------" << std::endl;
nlohmann::json jsonData = nlohmann::json::parse(jsonString);
std::string plainText = jsonToPlainText(jsonData);
std::cout << plainText << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
paymentPolicy:
localCurrency:
EUR
optionalExtras:
- <ul> <li>Fee for buffet breakfast: approximately EUR 23 for adults and EUR 11.5 for children</li><li>Self parking fee: EUR 22 per day</li><li>Nearby parking fee: EUR 13 per day (328 ft away)</li><li>Pet fee: EUR 20 per pet, per stay</li><li>Service animals are exempt from fees</li></ul>
- <p>The above list may not be comprehensive. Fees and deposits may not include tax and are subject to change. </p>
propertyFees:
- <p>Youll be asked to pay the following charges at the property:</p>
- <ul><li>A tax is imposed by the city and collected at the property. Exemptions or reductions might apply. For more details, please contact the property using the information on the reservation confirmation received after booking. </li><li>An effective city/local tax rate of 5.00 percent will be charged</li><li>Deposit is payable by bank transfer and is due within 72 hours of booking the reservation.</li></ul>
- <p>We have included all charges provided to us by the property. </p>
#endif