Formatted includes/json_handling

This commit is contained in:
Aaron Po
2026-07-19 04:10:32 -04:00
parent ffabd85b6d
commit 969125e3a5

View File

@@ -27,83 +27,84 @@
inline void PrettyPrint(std::ostream& outstream, inline void PrettyPrint(std::ostream& outstream,
boost::json::value const& json_val, boost::json::value const& json_val,
std::string* indent = nullptr) { std::string* indent = nullptr) {
std::string str; std::string str;
if (indent == nullptr) { if (indent == nullptr) {
indent = &str; indent = &str;
} }
switch (json_val.kind()) { switch (json_val.kind()) {
case boost::json::kind::object: { case boost::json::kind::object: {
outstream << "{\n"; outstream << "{\n";
indent->append(4, ' '); indent->append(4, ' ');
auto const& obj = json_val.get_object(); auto const& obj = json_val.get_object();
if (!obj.empty()) { if (!obj.empty()) {
const auto* iter = obj.begin(); const auto* iter = obj.begin();
for (;;) { for (;;) {
outstream << *indent << boost::json::serialize(iter->key()) << " : "; outstream << *indent << boost::json::serialize(iter->key())
PrettyPrint(outstream, iter->value(), indent); << " : ";
iter = std::next(iter); PrettyPrint(outstream, iter->value(), indent);
if (iter == obj.end()) { iter = std::next(iter);
break; if (iter == obj.end()) {
} break;
}
outstream << ",\n"; outstream << ",\n";
} }
}
outstream << "\n";
indent->resize(indent->size() - 4);
outstream << *indent << "}";
break;
} }
case boost::json::kind::array: {
outstream << "[\n";
indent->append(4, ' ');
auto const& arr = json_val.get_array();
if (!arr.empty()) {
const auto* iter = arr.begin();
for (;;) {
outstream << *indent;
PrettyPrint(outstream, *iter, indent);
iter = std::next(iter);
if (iter == arr.end()) {
break;
}
outstream << ",\n";
}
}
outstream << "\n";
indent->resize(indent->size() - 4);
outstream << *indent << "]";
break;
}
case boost::json::kind::string: {
outstream << serialize(json_val.get_string());
break;
}
case boost::json::kind::uint64:
case boost::json::kind::int64:
case boost::json::kind::double_:
outstream << json_val;
break;
case boost::json::kind::bool_:
if (json_val.get_bool()) {
outstream << "true";
} else {
outstream << "false";
}
break;
case boost::json::kind::null:
outstream << "null";
break;
}
if (indent->empty()) {
outstream << "\n"; outstream << "\n";
indent->resize(indent->size() - 4); }
outstream << *indent << "}";
break;
}
case boost::json::kind::array: {
outstream << "[\n";
indent->append(4, ' ');
auto const& arr = json_val.get_array();
if (!arr.empty()) {
const auto* iter = arr.begin();
for (;;) {
outstream << *indent;
PrettyPrint(outstream, *iter, indent);
iter = std::next(iter);
if (iter == arr.end()) {
break;
}
outstream << ",\n";
}
}
outstream << "\n";
indent->resize(indent->size() - 4);
outstream << *indent << "]";
break;
}
case boost::json::kind::string: {
outstream << serialize(json_val.get_string());
break;
}
case boost::json::kind::uint64:
case boost::json::kind::int64:
case boost::json::kind::double_:
outstream << json_val;
break;
case boost::json::kind::bool_:
if (json_val.get_bool()) {
outstream << "true";
} else {
outstream << "false";
}
break;
case boost::json::kind::null:
outstream << "null";
break;
}
if (indent->empty()) {
outstream << "\n";
}
} }
#endif // BIERGARTEN_PIPELINE_INCLUDES_JSON_HANDLING_PRETTY_PRINT_H_ #endif // BIERGARTEN_PIPELINE_INCLUDES_JSON_HANDLING_PRETTY_PRINT_H_