Skip to content

Commit

Permalink
fix #60 (double escaping)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Apr 24, 2015
1 parent 28f21c4 commit fc58a73
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
json_unit
html

benchmark
12 changes: 5 additions & 7 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,7 @@ class basic_json
{
// control characters (everything between 0x00 and 0x1f)
// -> create four-digit hex representation
std::stringstream ss;
std::basic_stringstream<typename string_t::value_type> ss;
ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c);
result += ss.str();
}
Expand Down Expand Up @@ -2391,7 +2391,7 @@ class basic_json
// 15 digits of precision allows round-trip IEEE 754
// string->double->string
const auto sz = static_cast<unsigned int>(std::snprintf(nullptr, 0, "%.15g", m_value.number_float));
std::vector<char> buf(sz + 1);
std::vector<typename string_t::value_type> buf(sz + 1);
std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
return string_t(buf.data());
}
Expand Down Expand Up @@ -4577,21 +4577,19 @@ class basic_json
result += "\r";
break;
}

// characters that are not "un"escsaped
case '\\':
{
result += "\\\\";
result += "\\";
break;
}
case '/':
{
result += "\\/";
result += "/";
break;
}
case '"':
{
result += "\\\"";
result += "\"";
break;
}

Expand Down
12 changes: 5 additions & 7 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,7 @@ class basic_json
{
// control characters (everything between 0x00 and 0x1f)
// -> create four-digit hex representation
std::stringstream ss;
std::basic_stringstream<typename string_t::value_type> ss;
ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c);
result += ss.str();
}
Expand Down Expand Up @@ -2391,7 +2391,7 @@ class basic_json
// 15 digits of precision allows round-trip IEEE 754
// string->double->string
const auto sz = static_cast<unsigned int>(std::snprintf(nullptr, 0, "%.15g", m_value.number_float));
std::vector<char> buf(sz + 1);
std::vector<typename string_t::value_type> buf(sz + 1);
std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
return string_t(buf.data());
}
Expand Down Expand Up @@ -3883,21 +3883,19 @@ class basic_json
result += "\r";
break;
}

// characters that are not "un"escsaped
case '\\':
{
result += "\\\\";
result += "\\";
break;
}
case '/':
{
result += "\\/";
result += "/";
break;
}
case '"':
{
result += "\\\"";
result += "\"";
break;
}

Expand Down
23 changes: 18 additions & 5 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7328,12 +7328,12 @@ TEST_CASE("parser class")

SECTION("escaped")
{
// quotation mark
CHECK(json::parser("\"\\\"\"").parse() == json("\\\""));
// reverse solidus
CHECK(json::parser("\"\\\\\"").parse() == json("\\\\"));
// quotation mark "\""
CHECK(json::parser("\"\\\"\"").parse() == R"("\"")"_json);
// reverse solidus "\\"
CHECK(json::parser("\"\\\\\"").parse() == R"("\\")"_json);
// solidus
CHECK(json::parser("\"\\/\"").parse() == json("\\/"));
CHECK(json::parser("\"\\/\"").parse() == R"("/")"_json);
// backspace
CHECK(json::parser("\"\\b\"").parse() == json("\b"));
// formfeed
Expand Down Expand Up @@ -8267,3 +8267,16 @@ TEST_CASE("concepts")
}
}
}

TEST_CASE("regression tests")
{
SECTION("issue #60 - Double quotation mark is not parsed correctly")
{
SECTION("escape_dobulequote")
{
auto s = "[\"\\\"foo\\\"\"]";
json j = json::parse(s);
CHECK(j == R"(["\"foo\""])"_json);
}
}
}

0 comments on commit fc58a73

Please sign in to comment.