Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indentation to json dump method #748

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,14 @@ namespace crow
out.push_back('"');
}

inline void dump_internal(const wvalue& v, std::string& out) const
inline void dump_indentation_part(std::string& out, const int indent, const char separator, const int indent_level) const
{
out.push_back('\n');
out.append(indent_level * indent, separator);
}


inline void dump_internal(const wvalue& v, std::string& out, const int indent, const char separator, const int indent_level = 0) const
{
switch (v.t_)
{
Expand Down Expand Up @@ -1909,6 +1916,12 @@ namespace crow
case type::List:
{
out.push_back('[');

if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}

if (v.l)
{
bool first = true;
Expand All @@ -1917,17 +1930,34 @@ namespace crow
if (!first)
{
out.push_back(',');

if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}
}
first = false;
dump_internal(x, out);
dump_internal(x, out, indent, separator, indent_level + 1);
}
}

if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level);
}

out.push_back(']');
}
break;
case type::Object:
{
out.push_back('{');

if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}

if (v.o)
{
bool first = true;
Expand All @@ -1936,13 +1966,29 @@ namespace crow
if (!first)
{
out.push_back(',');
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}
}
first = false;
dump_string(kv.first, out);
out.push_back(':');
dump_internal(kv.second, out);

if (indent >= 0)
{
out.push_back(' ');
}

dump_internal(kv.second, out, indent, separator, indent_level + 1);
}
}

if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level);
}

out.push_back('}');
}
break;
Expand All @@ -1954,13 +2000,20 @@ namespace crow
}

public:
std::string dump() const
std::string dump(const int indent, const char separator = ' ') const
{
std::string ret;
ret.reserve(estimate_length());
dump_internal(*this, ret);
dump_internal(*this, ret, indent, separator);
return ret;
}

std::string dump() const
{
static constexpr int DontIndent = -1;

return dump(DontIndent);
}
};

// Used for accessing the internals of a wvalue
Expand Down
66 changes: 66 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,72 @@ TEST_CASE("json_write")
CHECK(R"({"scores":[1,2,3]})" == y.dump());
} // json_write

TEST_CASE("json_write_with_indent")
{
static constexpr int IndentationLevelOne = 1;
static constexpr int IndentationLevelTwo = 2;
static constexpr int IndentationLevelFour = 4;

json::wvalue y;

y["scores"][0] = 1;
y["scores"][1] = "king";
y["scores"][2][0] = "real";
y["scores"][2][1] = false;
y["scores"][2][2] = true;

CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelOne));

CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelTwo));

CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelFour));

static constexpr char TabSeparator = '\t';

CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelOne, TabSeparator));
} // json_write_with_indent


TEST_CASE("json_copy_r_to_w_to_w_to_r")
{
json::rvalue r = json::load(
Expand Down
Loading