Skip to content

Commit

Permalink
to_string: added an option to print pretty or not
Browse files Browse the repository at this point in the history
  • Loading branch information
eranif committed Sep 16, 2023
1 parent e0d04da commit 674f2ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int main(int argc, char** argv)
size_t index = 0;
for(const auto& lexer : lexers_array) {
std::stringstream s;
tinyjson::to_string(lexer, s);
tinyjson::to_string(lexer, s, false);
std::cout << "printing lexer " << index << ":" << std::endl;
std::cout << s.str() << std::endl;
++index;
Expand Down
46 changes: 27 additions & 19 deletions tinyjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ struct element {
return m_children.back();
}

FLATTEN_INLINE const char* suffix(bool is_last) const
FLATTEN_INLINE const char* suffix(bool is_last, bool pretty) const
{
if(is_last) {
return "\n";
return pretty ? "\n" : "";
} else {
return ",\n";
return pretty ? ",\n" : ",";
}
}

Expand Down Expand Up @@ -281,64 +281,72 @@ struct element {
return m_property_name.c_str();
}

FLATTEN_INLINE void to_string(std::stringstream& ss, int depth, bool last_child) const
FLATTEN_INLINE void to_string(std::stringstream& ss, int depth, bool last_child, bool pretty) const
{
std::string indent(depth, ' ');
if(!pretty) {
indent.clear();
}

const std::string NEW_LINE = pretty ? "\n" : "";
ss << indent;
if(property_name()) {
ss << "\"" << property_name() << "\": ";
ss << "\"" << property_name() << "\":" << (pretty ? " " : "");
}

switch(m_kind) {
case element_kind::T_STRING: {
std::string_view sv;
as_str(&sv);
std::string escaped_str;
ss << escape_string(sv, &escaped_str) << suffix(last_child);
ss << escape_string(sv, &escaped_str) << suffix(last_child, pretty);
} break;
case element_kind::T_NUMBER: {
double d;
as_number(&d);
ss << d << suffix(last_child);
ss << d << suffix(last_child, pretty);
} break;
case element_kind::T_TRUE: {
ss << "true" << suffix(last_child);
ss << "true" << suffix(last_child, pretty);
} break;
case element_kind::T_FALSE: {
ss << "false" << suffix(last_child);
ss << "false" << suffix(last_child, pretty);
} break;
case element_kind::T_NULL: {
ss << "null" << suffix(last_child);
ss << "null" << suffix(last_child, pretty);
} break;
case element_kind::T_OBJECT: {
if(m_children.empty()) {
ss << "{}" << suffix(last_child);
ss << "{}" << suffix(last_child, pretty);
}
ss << "{\n";
ss << "{" << NEW_LINE;
for(size_t i = 0; i < m_children.size(); ++i) {
bool is_last = i == m_children.size() - 1;
m_children[i].to_string(ss, depth + 1, is_last);
m_children[i].to_string(ss, depth + 1, is_last, pretty);
}
ss << indent << "}" << suffix(last_child);
ss << indent << "}" << suffix(last_child, pretty);
} break;
case element_kind::T_ARRAY: {
if(m_children.empty()) {
ss << "[]" << suffix(last_child);
ss << "[]" << suffix(last_child, pretty);
}
ss << "[\n";
ss << "[" << NEW_LINE;
for(size_t i = 0; i < m_children.size(); ++i) {
bool is_last = i == m_children.size() - 1;
m_children[i].to_string(ss, depth + 1, is_last);
m_children[i].to_string(ss, depth + 1, is_last, pretty);
}
ss << indent << "]" << suffix(last_child);
ss << indent << "]" << suffix(last_child, pretty);
} break;
case element_kind::T_INVALID:
break;
}
}
};

FLATTEN_INLINE void to_string(const element& root, std::stringstream& ss) { root.to_string(ss, 0, true); }
FLATTEN_INLINE void to_string(const element& root, std::stringstream& ss, bool pretty = true)
{
root.to_string(ss, 0, true, pretty);
}

/// For convenience. Same as calling `tinyjson::element::parse`
FLATTEN_INLINE bool parse(const std::string& content, element* root) { return element::parse(content, root); }
Expand Down

0 comments on commit 674f2ed

Please sign in to comment.