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

Fixed dumping of strings #24

Merged
merged 1 commit into from
Jan 11, 2015
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
45 changes: 44 additions & 1 deletion src/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
{
case (value_type::string):
{
return std::string("\"") + *value_.string + "\"";
return std::string("\"") + escapeString(*value_.string) + "\"";
}

case (value_type::boolean):
Expand Down Expand Up @@ -589,6 +589,49 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
}
}

/*!
Internal function to replace all occurrences of a character in a given string
with another string.

\param str the string that contains tokens to replace
\param c the character that needs to be replaced
\param replacement the string that is the replacement for the character
*/
void json::replaceChar(std::string& str, char c, const std::string& replacement)
const
{
size_t start_pos = 0;
while((start_pos = str.find(c, start_pos)) != std::string::npos) {
str.replace(start_pos, 1, replacement);
start_pos += replacement.length();
}
}

/*!
Escapes all special characters in the given string according to ECMA-404.
Necessary as some characters such as quotes, backslashes and so on
can't be used as is when dumping a string value.

\param str the string that should be escaped.

\return a copy of the given string with all special characters escaped.
*/
std::string json::escapeString(const std::string& str) const
{
std::string result(str);
// we first need to escape the backslashes as all other methods will insert
// legitimate backslashes into the result.
replaceChar(result, '\\', "\\\\");
// replace all other characters
replaceChar(result, '"', "\\\"");
replaceChar(result, '\n', "\\n");
replaceChar(result, '\r', "\\r");
replaceChar(result, '\f', "\\f");
replaceChar(result, '\b', "\\b");
replaceChar(result, '\t', "\\t");
return result;
}

/*!
Serialization function for JSON objects. The function tries to mimick Python's
\p json.dumps() function, and currently supports its \p indent parameter.
Expand Down
5 changes: 4 additions & 1 deletion src/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ class json

/// dump the object (with pretty printer)
std::string dump(const bool, const unsigned int, unsigned int = 0) const noexcept;

/// replaced a character in a string with another string
void replaceChar(std::string& str, char c, const std::string& replacement) const;
/// escapes special characters to safely dump the string
std::string escapeString(const std::string&) const;
public:
/// explicit value conversion
template<typename T>
Expand Down
11 changes: 11 additions & 0 deletions test/json_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,17 @@ TEST_CASE("string")
j1.clear();
CHECK(j1.get<std::string>() == "");
}

SECTION("Dumping")
{
CHECK(json("\"").dump(0) == "\"\\\"\"");
CHECK(json("\\").dump(0) == "\"\\\\\"");
CHECK(json("\n").dump(0) == "\"\\n\"");
CHECK(json("\t").dump(0) == "\"\\t\"");
CHECK(json("\b").dump(0) == "\"\\b\"");
CHECK(json("\f").dump(0) == "\"\\f\"");
CHECK(json("\r").dump(0) == "\"\\r\"");
}
}

TEST_CASE("boolean")
Expand Down