Use escape to correctly escape unicode characters

This commit is contained in:
fbraem 2015-12-07 17:33:10 +01:00
parent 7086d93324
commit 18b92b5b36
7 changed files with 163 additions and 162 deletions

View File

@ -26,16 +26,11 @@
namespace Poco {
std::string Foundation_API toJSON(char c);
/// Utility function for escaping JSON characters.
void Foundation_API toJSON(const std::string& value, std::ostream& out, bool wrap = true);
/// Formats string value into the supplied output stream by
/// escaping control characters.
/// If wrap is true, the resulting string is enclosed in double quotes
std::string Foundation_API toJSON(const std::string& value, bool wrap = true);
/// Formats string value by escaping control characters.
/// If wrap is true, the resulting string is enclosed in double quotes

View File

@ -13,39 +13,16 @@
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/JSONString.h"
#include "Poco/UTF8String.h"
#include <ostream>
namespace Poco {
std::string toJSON(char c)
{
switch (c)
{
case '\\': return "\\\\";
case '"': return "\\\"";
case '/': return "\\/";
case '\b': return "\\b";
case '\f': return "\\f";
case '\n': return "\\n";
case '\r': return "\\r";
case '\t': return "\\t";
default: return std::string(1, c);
}
}
void toJSON(const std::string& value, std::ostream& out, bool wrap)
{
if (wrap) out << '"';
for (std::string::const_iterator it = value.begin(),
end = value.end(); it != end; ++it)
{
out << toJSON(*it);
}
out << UTF8::escape(value.begin(), value.end());
if (wrap) out << '"';
}
@ -54,11 +31,7 @@ std::string toJSON(const std::string& value, bool wrap)
{
std::string ret;
if (wrap) ret.append(1, '"');
for (std::string::const_iterator it = value.begin(),
end = value.end(); it != end; ++it)
{
ret.append(toJSON(*it));
}
ret.append(UTF8::escape(value.begin(), value.end()));
if (wrap) ret.append(1, '"');
return ret;
}

View File

@ -16,7 +16,7 @@
#include "Poco/Dynamic/VarHolder.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSONString.h"
#include "Poco/UTF8String.h"
namespace Poco {
@ -38,12 +38,7 @@ namespace Impl {
void escape(std::string& target, const std::string& source)
{
std::string::const_iterator it(source.begin());
std::string::const_iterator end(source.end());
for (; it != end; ++it)
{
target.append(Poco::toJSON(*it));
}
target = UTF8::escape(source.begin(), source.end());
}
@ -60,9 +55,11 @@ bool isJSONString(const Var& any)
void appendJSONString(std::string& val, const Var& any)
{
val += '"';
escape(val, any.convert<std::string>());
val += '"';
std::string json(val);
val.append(1, '"');
escape(json, any.convert<std::string>());
val.append(json);
val.append(1, '"');
}

View File

@ -1086,15 +1086,15 @@ void StringTest::benchmarkFloatToStr()
void StringTest::testJSONString()
{
assert (toJSON('\\') == "\\\\");
assert (toJSON('"') == "\\\"");
assert (toJSON('/') == "\\/");
assert (toJSON('\b') == "\\b");
assert (toJSON('\f') == "\\f");
assert (toJSON('\n') == "\\n");
assert (toJSON('\r') == "\\r");
assert (toJSON('\t') == "\\t");
assert (toJSON('a') == "a");
assert (toJSON("\\", false) == "\\\\");
assert (toJSON("\"", false) == "\\\"");
assert (toJSON("/", false) == "\\/");
assert (toJSON("\b", false) == "\\b");
assert (toJSON("\f", false) == "\\f");
assert (toJSON("\n", false) == "\\n");
assert (toJSON("\r", false) == "\\r");
assert (toJSON("\t", false) == "\\t");
assert (toJSON("a", false) == "a");
// ??? on MSVC, the assert macro expansion
// fails to compile when this string is inline ???

View File

@ -1782,6 +1782,38 @@ void JSONTest::testSmallBuffer()
parser.parse(jsonStr);
}
void JSONTest::testEscape0()
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
std::string nullString("B\0b", 3);
json->set("name", nullString);
std::stringstream ss;
json->stringify(ss);
assert(ss.str().compare("{\"name\":\"B\\u0000b\"}") == 0);
}
void JSONTest::testEscapeUnicode()
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
std::string chinese("{ \"name\" : \"\\u4e2d\" }");
Poco::JSON::Parser parser(new Poco::JSON::ParseHandler());
Var result = parser.parse(chinese);
assert(result.type() == typeid(Object::Ptr));
Object::Ptr object = result.extract<Object::Ptr>();
std::stringstream ss;
object->stringify(ss);
//assert(ss.str().compare("{\"name\":\"B\\u0000b\"}") == 0);
std::cout << ss.str() << std::endl;
}
std::string JSONTest::getTestFilesPath(const std::string& type)
{
@ -1798,6 +1830,7 @@ std::string JSONTest::getTestFilesPath(const std::string& type)
ostr.str("");
ostr << "/JSON/testsuite/data/" << type << '/';
validDir = Poco::Environment::get("POCO_BASE") + ostr.str();
std::cout << validDir << std::endl;
pathPattern = validDir;
if (Poco::File(pathPattern).exists())
@ -1855,6 +1888,8 @@ CppUnit::Test* JSONTest::suite()
CppUnit_addTest(pSuite, JSONTest, testTemplate);
CppUnit_addTest(pSuite, JSONTest, testUnicode);
CppUnit_addTest(pSuite, JSONTest, testSmallBuffer);
CppUnit_addTest(pSuite, JSONTest, testEscape0);
CppUnit_addTest(pSuite, JSONTest, testEscapeUnicode);
return pSuite;
}

View File

@ -77,7 +77,8 @@ public:
void testUnicode();
void testInvalidUnicodeJanssonFiles();
void testSmallBuffer();
void testEscape0();
void testEscapeUnicode();
void setUp();
void tearDown();