Util: Optimize away returning strings
This commit is contained in:
@@ -30,110 +30,101 @@ Serializer::~Serializer()
|
|||||||
|
|
||||||
std::string Serializer::dump(const Value& value)
|
std::string Serializer::dump(const Value& value)
|
||||||
{
|
{
|
||||||
return dumpHelper(value);
|
dumpHelper(value);
|
||||||
|
return m_output;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------
|
// ------------------------------------------
|
||||||
|
|
||||||
std::string Serializer::dumpHelper(const Value& value, const uint32_t indentLevel)
|
void Serializer::dumpHelper(const Value& value, const uint32_t indentLevel)
|
||||||
{
|
{
|
||||||
switch (value.m_type) {
|
switch (value.m_type) {
|
||||||
case Value::Type::Null:
|
case Value::Type::Null:
|
||||||
return "null";
|
m_output += "null";
|
||||||
break;
|
break;
|
||||||
case Value::Type::Bool:
|
case Value::Type::Bool:
|
||||||
return value.m_value.boolean ? "true" : "false";
|
m_output += value.m_value.boolean ? "true" : "false";
|
||||||
break;
|
break;
|
||||||
case Value::Type::Number: {
|
case Value::Type::Number: {
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << value.m_value.number;
|
os << value.m_value.number;
|
||||||
return os.str();
|
m_output += os.str();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Value::Type::String:
|
case Value::Type::String:
|
||||||
return "\"" + *value.m_value.string + "\"";
|
m_output += "\"" + *value.m_value.string + "\"";
|
||||||
break;
|
break;
|
||||||
case Value::Type::Array:
|
case Value::Type::Array:
|
||||||
return dumpArray(value, indentLevel);
|
dumpArray(value, indentLevel);
|
||||||
break;
|
break;
|
||||||
case Value::Type::Object:
|
case Value::Type::Object:
|
||||||
return dumpObject(value, indentLevel);
|
dumpObject(value, indentLevel);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Serializer::dumpArray(const Value& value, const uint32_t indentLevel)
|
void Serializer::dumpArray(const Value& value, const uint32_t indentLevel)
|
||||||
{
|
{
|
||||||
std::string result;
|
|
||||||
|
|
||||||
// Append [
|
// Append [
|
||||||
result += "[";
|
m_output += "[";
|
||||||
if (m_indent > 0) {
|
if (m_indent > 0) {
|
||||||
result += '\n';
|
m_output += '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
auto values = value.m_value.array->elements();
|
auto values = value.m_value.array->elements();
|
||||||
for (auto it = values.begin(); it != values.end(); ++it) {
|
for (auto it = values.begin(); it != values.end(); ++it) {
|
||||||
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
|
m_output += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
|
||||||
result += dumpHelper(*it, indentLevel + 1);
|
dumpHelper(*it, indentLevel + 1);
|
||||||
|
|
||||||
// Add comma, except after the last element
|
// Add comma, except after the last element
|
||||||
if (it != std::prev(values.end(), 1)) {
|
if (it != std::prev(values.end(), 1)) {
|
||||||
result += ",";
|
m_output += ",";
|
||||||
}
|
}
|
||||||
if (m_indent > 0) {
|
if (m_indent > 0) {
|
||||||
result += '\n';
|
m_output += '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append indentation
|
// Append indentation
|
||||||
result += std::string(m_indent * indentLevel, m_indentCharacter);
|
m_output += std::string(m_indent * indentLevel, m_indentCharacter);
|
||||||
|
|
||||||
// Append ]
|
// Append ]
|
||||||
result += "]";
|
m_output += "]";
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Serializer::dumpObject(const Value& value, const uint32_t indentLevel)
|
void Serializer::dumpObject(const Value& value, const uint32_t indentLevel)
|
||||||
{
|
{
|
||||||
std::string result;
|
|
||||||
|
|
||||||
// Append {
|
// Append {
|
||||||
result += "{";
|
m_output += "{";
|
||||||
if (m_indent > 0) {
|
if (m_indent > 0) {
|
||||||
result += '\n';
|
m_output += '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
auto members = value.m_value.object->members();
|
auto members = value.m_value.object->members();
|
||||||
for (auto it = members.begin(); it != members.end(); ++it) {
|
for (auto it = members.begin(); it != members.end(); ++it) {
|
||||||
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
|
m_output += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
|
||||||
result += "\"" + it->first + "\":";
|
m_output += "\"" + it->first + "\":";
|
||||||
if (m_indent > 0) {
|
if (m_indent > 0) {
|
||||||
result += ' ';
|
m_output += ' ';
|
||||||
}
|
}
|
||||||
result += dumpHelper(it->second, indentLevel + 1);
|
dumpHelper(it->second, indentLevel + 1);
|
||||||
|
|
||||||
// Add comma, except after the last element
|
// Add comma, except after the last element
|
||||||
if (it != std::prev(members.end(), 1)) {
|
if (it != std::prev(members.end(), 1)) {
|
||||||
result += ",";
|
m_output += ",";
|
||||||
}
|
}
|
||||||
if (m_indent > 0) {
|
if (m_indent > 0) {
|
||||||
result += '\n';
|
m_output += '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append indentation
|
// Append indentation
|
||||||
result += std::string(m_indent * indentLevel, m_indentCharacter);
|
m_output += std::string(m_indent * indentLevel, m_indentCharacter);
|
||||||
|
|
||||||
// Append }
|
// Append }
|
||||||
result += "}";
|
m_output += "}";
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Json
|
} // namespace Json
|
||||||
|
|||||||
@@ -22,9 +22,11 @@ public:
|
|||||||
std::string dump(const Value& value);
|
std::string dump(const Value& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string dumpHelper(const Value& value, const uint32_t indentLevel = 0);
|
void dumpHelper(const Value& value, const uint32_t indentLevel = 0);
|
||||||
std::string dumpArray(const Value& value, const uint32_t indentLevel = 0);
|
void dumpArray(const Value& value, const uint32_t indentLevel = 0);
|
||||||
std::string dumpObject(const Value& value, const uint32_t indentLevel = 0);
|
void dumpObject(const Value& value, const uint32_t indentLevel = 0);
|
||||||
|
|
||||||
|
std::string m_output;
|
||||||
|
|
||||||
uint32_t m_indent { 0 };
|
uint32_t m_indent { 0 };
|
||||||
char m_indentCharacter { ' ' };
|
char m_indentCharacter { ' ' };
|
||||||
|
|||||||
Reference in New Issue
Block a user