strings are escaped

This commit is contained in:
Niels 2015-02-04 22:49:46 +01:00
parent 5ec433604a
commit a5188b08df
2 changed files with 80 additions and 1 deletions

View file

@ -1321,6 +1321,72 @@ class basic_json
} }
} }
/*!
Escape a string by replacing special characters by a sequence of an
escape character (backslash) and another character.
*/
static string_t escape_string(const string_t& s)
{
// create a result string of at least the size than s
string_t result;
result.reserve(s.size());
for (auto c : s)
{
switch (c)
{
// quotation mark
case '"':
{
result.append("\\\"", 2);
break;
}
// reverse solidus
case '\\':
{
result.append("\\\\", 2);
break;
}
// backspace
case '\b':
{
result.append("\\b", 2);
break;
}
// formfeed
case '\f':
{
result.append("\\f", 2);
break;
}
// newline
case '\n':
{
result.append("\\n", 2);
break;
}
// carriage return
case '\r':
{
result.append("\\r", 2);
break;
}
// horizontal tab
case '\t':
{
result.append("\\t", 2);
break;
}
default:
{
result.append(1, c);
}
}
}
return result;
}
/*! /*!
Internal implementation of the serialization function. Internal implementation of the serialization function.
@ -1417,7 +1483,7 @@ class basic_json
case (value_t::string): case (value_t::string):
{ {
return string_t("\"") + *m_value.string + "\""; return string_t("\"") + escape_string(*m_value.string) + "\"";
} }
case (value_t::boolean): case (value_t::boolean):

View file

@ -11,6 +11,19 @@ using nlohmann::json;
TEST_CASE() TEST_CASE()
{ {
CHECK(json::escape_string("\\") == "\\\\");
CHECK(json::escape_string("\"") == "\\\"");
CHECK(json::escape_string("\n") == "\\n");
CHECK(json::escape_string("\r") == "\\r");
CHECK(json::escape_string("\f") == "\\f");
CHECK(json::escape_string("\b") == "\\b");
CHECK(json::escape_string("\t") == "\\t");
{
json j = "AC/DC";
CHECK(j.dump() == "\"AC/DC\"");
}
{ {
json j = {1, 2, 3, 4}; json j = {1, 2, 3, 4};
std::cerr << j << std::endl; std::cerr << j << std::endl;