提交 fc58a735 编写于 作者: N Niels

fix #60 (double escaping)

上级 28f21c43
json_unit json_unit
html html
benchmark
...@@ -2251,7 +2251,7 @@ class basic_json ...@@ -2251,7 +2251,7 @@ class basic_json
{ {
// control characters (everything between 0x00 and 0x1f) // control characters (everything between 0x00 and 0x1f)
// -> create four-digit hex representation // -> 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); ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c);
result += ss.str(); result += ss.str();
} }
...@@ -2391,7 +2391,7 @@ class basic_json ...@@ -2391,7 +2391,7 @@ class basic_json
// 15 digits of precision allows round-trip IEEE 754 // 15 digits of precision allows round-trip IEEE 754
// string->double->string // string->double->string
const auto sz = static_cast<unsigned int>(std::snprintf(nullptr, 0, "%.15g", m_value.number_float)); 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); std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
return string_t(buf.data()); return string_t(buf.data());
} }
...@@ -4577,21 +4577,19 @@ basic_json_parser_59: ...@@ -4577,21 +4577,19 @@ basic_json_parser_59:
result += "\r"; result += "\r";
break; break;
} }
// characters that are not "un"escsaped
case '\\': case '\\':
{ {
result += "\\\\"; result += "\\";
break; break;
} }
case '/': case '/':
{ {
result += "\\/"; result += "/";
break; break;
} }
case '"': case '"':
{ {
result += "\\\""; result += "\"";
break; break;
} }
......
...@@ -2251,7 +2251,7 @@ class basic_json ...@@ -2251,7 +2251,7 @@ class basic_json
{ {
// control characters (everything between 0x00 and 0x1f) // control characters (everything between 0x00 and 0x1f)
// -> create four-digit hex representation // -> 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); ss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c);
result += ss.str(); result += ss.str();
} }
...@@ -2391,7 +2391,7 @@ class basic_json ...@@ -2391,7 +2391,7 @@ class basic_json
// 15 digits of precision allows round-trip IEEE 754 // 15 digits of precision allows round-trip IEEE 754
// string->double->string // string->double->string
const auto sz = static_cast<unsigned int>(std::snprintf(nullptr, 0, "%.15g", m_value.number_float)); 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); std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
return string_t(buf.data()); return string_t(buf.data());
} }
...@@ -3883,21 +3883,19 @@ class basic_json ...@@ -3883,21 +3883,19 @@ class basic_json
result += "\r"; result += "\r";
break; break;
} }
// characters that are not "un"escsaped
case '\\': case '\\':
{ {
result += "\\\\"; result += "\\";
break; break;
} }
case '/': case '/':
{ {
result += "\\/"; result += "/";
break; break;
} }
case '"': case '"':
{ {
result += "\\\""; result += "\"";
break; break;
} }
......
...@@ -7328,12 +7328,12 @@ TEST_CASE("parser class") ...@@ -7328,12 +7328,12 @@ TEST_CASE("parser class")
SECTION("escaped") SECTION("escaped")
{ {
// quotation mark // quotation mark "\""
CHECK(json::parser("\"\\\"\"").parse() == json("\\\"")); CHECK(json::parser("\"\\\"\"").parse() == R"("\"")"_json);
// reverse solidus // reverse solidus "\\"
CHECK(json::parser("\"\\\\\"").parse() == json("\\\\")); CHECK(json::parser("\"\\\\\"").parse() == R"("\\")"_json);
// solidus // solidus
CHECK(json::parser("\"\\/\"").parse() == json("\\/")); CHECK(json::parser("\"\\/\"").parse() == R"("/")"_json);
// backspace // backspace
CHECK(json::parser("\"\\b\"").parse() == json("\b")); CHECK(json::parser("\"\\b\"").parse() == json("\b"));
// formfeed // formfeed
...@@ -8267,3 +8267,16 @@ TEST_CASE("concepts") ...@@ -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);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册