add new json::stringify overloads
This commit is contained in:
parent
390a16ace1
commit
5241c91cc8
@ -44,6 +44,14 @@ void stringifyObj(
|
|||||||
bool nice
|
bool nice
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void stringifyArr(
|
||||||
|
const List* list,
|
||||||
|
std::stringstream& ss,
|
||||||
|
int indent,
|
||||||
|
const std::string& indentstr,
|
||||||
|
bool nice
|
||||||
|
);
|
||||||
|
|
||||||
void stringifyValue(
|
void stringifyValue(
|
||||||
const Value& value,
|
const Value& value,
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
@ -54,26 +62,7 @@ void stringifyValue(
|
|||||||
if (auto map = std::get_if<Map_sptr>(&value)) {
|
if (auto map = std::get_if<Map_sptr>(&value)) {
|
||||||
stringifyObj(map->get(), ss, indent, indentstr, nice);
|
stringifyObj(map->get(), ss, indent, indentstr, nice);
|
||||||
} else if (auto listptr = std::get_if<List_sptr>(&value)) {
|
} else if (auto listptr = std::get_if<List_sptr>(&value)) {
|
||||||
auto list = *listptr;
|
stringifyArr(listptr->get(), ss, indent, indentstr, nice);
|
||||||
if (list->size() == 0) {
|
|
||||||
ss << "[]";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ss << '[';
|
|
||||||
for (uint i = 0; i < list->size(); i++) {
|
|
||||||
Value& value = list->get(i);
|
|
||||||
if (i > 0 || nice) {
|
|
||||||
newline(ss, nice, indent, indentstr);
|
|
||||||
}
|
|
||||||
stringifyValue(value, ss, indent + 1, indentstr, nice);
|
|
||||||
if (i + 1 < list->size()) {
|
|
||||||
ss << ',';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nice) {
|
|
||||||
newline(ss, true, indent - 1, indentstr);
|
|
||||||
}
|
|
||||||
ss << ']';
|
|
||||||
} else if (auto flag = std::get_if<bool>(&value)) {
|
} else if (auto flag = std::get_if<bool>(&value)) {
|
||||||
ss << (*flag ? "true" : "false");
|
ss << (*flag ? "true" : "false");
|
||||||
} else if (auto num = std::get_if<number_t>(&value)) {
|
} else if (auto num = std::get_if<number_t>(&value)) {
|
||||||
@ -87,6 +76,38 @@ void stringifyValue(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void stringifyArr(
|
||||||
|
const List* list,
|
||||||
|
std::stringstream& ss,
|
||||||
|
int indent,
|
||||||
|
const std::string& indentstr,
|
||||||
|
bool nice
|
||||||
|
) {
|
||||||
|
if (list == nullptr) {
|
||||||
|
ss << "nullptr";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (list->values.empty()) {
|
||||||
|
ss << "[]";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ss << "[";
|
||||||
|
for (size_t i = 0; i < list->size(); i++) {
|
||||||
|
if (i > 0 || nice) {
|
||||||
|
newline(ss, nice, indent, indentstr);
|
||||||
|
}
|
||||||
|
const Value& value = list->values[i];
|
||||||
|
stringifyValue(value, ss, indent + 1, indentstr, nice);
|
||||||
|
if (i + 1 < list->size()) {
|
||||||
|
ss << ',';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nice) {
|
||||||
|
newline(ss, true, indent - 1, indentstr);
|
||||||
|
}
|
||||||
|
ss << ']';
|
||||||
|
}
|
||||||
|
|
||||||
void stringifyObj(
|
void stringifyObj(
|
||||||
const Map* obj,
|
const Map* obj,
|
||||||
std::stringstream& ss,
|
std::stringstream& ss,
|
||||||
@ -103,7 +124,7 @@ void stringifyObj(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ss << "{";
|
ss << "{";
|
||||||
uint index = 0;
|
size_t index = 0;
|
||||||
for (auto& entry : obj->values) {
|
for (auto& entry : obj->values) {
|
||||||
const std::string& key = entry.first;
|
const std::string& key = entry.first;
|
||||||
if (index > 0 || nice) {
|
if (index > 0 || nice) {
|
||||||
@ -131,6 +152,14 @@ std::string json::stringify(
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string json::stringify(
|
||||||
|
const dynamic::List* arr, bool nice, const std::string& indent
|
||||||
|
) {
|
||||||
|
std::stringstream ss;
|
||||||
|
stringifyArr(arr, ss, 1, indent, nice);
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
std::string json::stringify(
|
std::string json::stringify(
|
||||||
const dynamic::Value& value, bool nice, const std::string& indent
|
const dynamic::Value& value, bool nice, const std::string& indent
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -14,6 +14,10 @@ namespace json {
|
|||||||
const dynamic::Map* obj, bool nice, const std::string& indent
|
const dynamic::Map* obj, bool nice, const std::string& indent
|
||||||
);
|
);
|
||||||
|
|
||||||
|
std::string stringify(
|
||||||
|
const dynamic::List* arr, bool nice, const std::string& indent
|
||||||
|
);
|
||||||
|
|
||||||
std::string stringify(
|
std::string stringify(
|
||||||
const dynamic::Value& value, bool nice, const std::string& indent
|
const dynamic::Value& value, bool nice, const std::string& indent
|
||||||
);
|
);
|
||||||
|
|||||||
@ -9,11 +9,22 @@ std::ostream& operator<<(std::ostream& stream, const dynamic::Value& value) {
|
|||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const dynamic::Map& value) {
|
||||||
|
stream << json::stringify(&value, false, " ");
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value) {
|
std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value) {
|
||||||
stream << json::stringify(value, false, " ");
|
stream << json::stringify(value, false, " ");
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const dynamic::List& value) {
|
||||||
|
stream << json::stringify(&value, false, " ");
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
std::ostream& operator<<(
|
std::ostream& operator<<(
|
||||||
std::ostream& stream, const dynamic::List_sptr& value
|
std::ostream& stream, const dynamic::List_sptr& value
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -160,5 +160,7 @@ namespace dynamic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const dynamic::Value& value);
|
std::ostream& operator<<(std::ostream& stream, const dynamic::Value& value);
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const dynamic::Map& value);
|
||||||
std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value);
|
std::ostream& operator<<(std::ostream& stream, const dynamic::Map_sptr& value);
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const dynamic::List& value);
|
||||||
std::ostream& operator<<(std::ostream& stream, const dynamic::List_sptr& value);
|
std::ostream& operator<<(std::ostream& stream, const dynamic::List_sptr& value);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user