SnapmaticJson: fix indent of empty array and objects

This commit is contained in:
Syping 2023-03-31 01:33:54 +02:00
parent 7e13f01d6d
commit a10d259db1

View file

@ -27,14 +27,14 @@ void serializer(std::ostream &os, const boost::json::value &jv, std::string *ind
indent = &indent_; indent = &indent_;
switch (jv.kind()) { switch (jv.kind()) {
case boost::json::kind::object: { case boost::json::kind::object: {
auto const &obj = jv.get_object();
if (!obj.empty()) {
if (do_indent) { if (do_indent) {
os << "{\n"; os << "{\n";
indent->append(4, ' '); indent->append(4, ' ');
} }
else else
os << "{"; os << "{";
auto const &obj = jv.get_object();
if (!obj.empty()) {
auto it = obj.begin(); auto it = obj.begin();
for (;;) { for (;;) {
if (do_indent) if (do_indent)
@ -49,7 +49,6 @@ void serializer(std::ostream &os, const boost::json::value &jv, std::string *ind
else else
os << ","; os << ",";
} }
}
if (do_indent) { if (do_indent) {
os << "\n"; os << "\n";
indent->resize(indent->size() - 4); indent->resize(indent->size() - 4);
@ -57,17 +56,20 @@ void serializer(std::ostream &os, const boost::json::value &jv, std::string *ind
} }
else else
os << "}"; os << "}";
}
else
os << "{}";
break; break;
} }
case boost::json::kind::array: { case boost::json::kind::array: {
auto const &arr = jv.get_array();
if (!arr.empty()) {
if (do_indent) { if (do_indent) {
os << "[\n"; os << "[\n";
indent->append(4, ' '); indent->append(4, ' ');
} }
else else
os << "["; os << "[";
auto const &arr = jv.get_array();
if (!arr.empty()) {
auto it = arr.begin(); auto it = arr.begin();
for (;;) { for (;;) {
if (do_indent) if (do_indent)
@ -80,7 +82,6 @@ void serializer(std::ostream &os, const boost::json::value &jv, std::string *ind
else else
os << ","; os << ",";
} }
}
if (do_indent) { if (do_indent) {
os << "\n"; os << "\n";
indent->resize(indent->size() - 4); indent->resize(indent->size() - 4);
@ -88,6 +89,9 @@ void serializer(std::ostream &os, const boost::json::value &jv, std::string *ind
} }
else else
os << "]"; os << "]";
}
else
os << "[]";
break; break;
} }
case boost::json::kind::string: { case boost::json::kind::string: {