From 18b92b5b36062dd00756008837fcfd3a3c73cf7f Mon Sep 17 00:00:00 2001 From: fbraem Date: Mon, 7 Dec 2015 17:33:10 +0100 Subject: [PATCH] Use escape to correctly escape unicode characters --- Foundation/include/Poco/JSONString.h | 7 +- Foundation/src/JSONString.cpp | 33 +-------- Foundation/src/VarHolder.cpp | 33 ++++----- Foundation/testsuite/src/StringTest.cpp | 86 +++++++++++----------- Foundation/testsuite/src/VarTest.cpp | 98 ++++++++++++------------- JSON/testsuite/src/JSONTest.cpp | 63 ++++++++++++---- JSON/testsuite/src/JSONTest.h | 5 +- 7 files changed, 163 insertions(+), 162 deletions(-) diff --git a/Foundation/include/Poco/JSONString.h b/Foundation/include/Poco/JSONString.h index 80cba92e7..941bf0aae 100644 --- a/Foundation/include/Poco/JSONString.h +++ b/Foundation/include/Poco/JSONString.h @@ -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 + /// 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 diff --git a/Foundation/src/JSONString.cpp b/Foundation/src/JSONString.cpp index eaa639ef4..4ec2a6b98 100644 --- a/Foundation/src/JSONString.cpp +++ b/Foundation/src/JSONString.cpp @@ -13,39 +13,16 @@ // SPDX-License-Identifier: BSL-1.0 // - #include "Poco/JSONString.h" +#include "Poco/UTF8String.h" #include - 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; } diff --git a/Foundation/src/VarHolder.cpp b/Foundation/src/VarHolder.cpp index e49511f4b..312f9e941 100644 --- a/Foundation/src/VarHolder.cpp +++ b/Foundation/src/VarHolder.cpp @@ -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,31 +38,28 @@ 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()); } bool isJSONString(const Var& any) { - return any.type() == typeid(std::string) || - any.type() == typeid(char) || - any.type() == typeid(char*) || - any.type() == typeid(Poco::DateTime) || - any.type() == typeid(Poco::LocalDateTime) || + return any.type() == typeid(std::string) || + any.type() == typeid(char) || + any.type() == typeid(char*) || + any.type() == typeid(Poco::DateTime) || + any.type() == typeid(Poco::LocalDateTime) || any.type() == typeid(Poco::Timestamp); } void appendJSONString(std::string& val, const Var& any) { - val += '"'; - escape(val, any.convert()); - val += '"'; + std::string json(val); + val.append(1, '"'); + escape(json, any.convert()); + val.append(json); + val.append(1, '"'); } @@ -74,14 +71,14 @@ void appendJSONKey(std::string& val, const Var& any) void appendJSONValue(std::string& val, const Var& any) { - if (any.isEmpty()) + if (any.isEmpty()) { val.append("null"); } - else + else { bool isStr = isJSONString(any); - if (isStr) + if (isStr) { appendJSONString(val, any.convert()); } diff --git a/Foundation/testsuite/src/StringTest.cpp b/Foundation/testsuite/src/StringTest.cpp index 7e7cc5932..d3519cf94 100644 --- a/Foundation/testsuite/src/StringTest.cpp +++ b/Foundation/testsuite/src/StringTest.cpp @@ -272,7 +272,7 @@ void StringTest::testIcompare() assert (icompare(ss1, 2, 2, ss2, 1, 3) < 0); assert (icompare(ss1, 2, 2, ss2, 1, 2) == 0); assert (icompare(ss3, 1, 3, ss1, 2, 3) > 0); - + assert (icompare(s1, s2.c_str()) == 0); assert (icompare(s1, s3.c_str()) < 0); assert (icompare(s1, s4.c_str()) < 0); @@ -281,12 +281,12 @@ void StringTest::testIcompare() assert (icompare(s2, s4.c_str()) < 0); assert (icompare(s1, s5.c_str()) > 0); assert (icompare(s5, s4.c_str()) < 0); - + assert (icompare(ss1, 2, 3, "aaa") == 0); assert (icompare(ss1, 2, 2, "aaa") < 0); assert (icompare(ss1, 2, 3, "AAA") == 0); assert (icompare(ss1, 2, 2, "bb") < 0); - + assert (icompare(ss1, 2, "aaa") > 0); } @@ -295,7 +295,7 @@ void StringTest::testCILessThan() { typedef std::map CIMapType; CIMapType ciMap; - + ciMap["z"] = 1; ciMap["b"] = 2; ciMap["A"] = 3; @@ -309,7 +309,7 @@ void StringTest::testCILessThan() assert (it->second == 4); typedef std::set CISetType; - + CISetType ciSet; ciSet.insert("z"); ciSet.insert("b"); @@ -346,7 +346,7 @@ void StringTest::testTranslateInPlace() void StringTest::testReplace() { std::string s("aabbccdd"); - + assert (replace(s, std::string("aa"), std::string("xx")) == "xxbbccdd"); assert (replace(s, std::string("bb"), std::string("xx")) == "aaxxccdd"); assert (replace(s, std::string("dd"), std::string("xx")) == "aabbccxx"); @@ -365,7 +365,7 @@ void StringTest::testReplace() assert (replace(s, "b", "") == "aaccdd"); assert (replace(s, "ee", "xx") == "aabbccdd"); assert (replace(s, "dd", "") == "aabbcc"); - + s = "aabbaabb"; assert (replace(s, std::string("aa"), std::string("")) == "bbbb"); assert (replace(s, std::string("a"), std::string("")) == "bbbb"); @@ -380,7 +380,7 @@ void StringTest::testReplace() assert (replace(s, "a", "x") == "xxbbxxbb"); assert (replace(s, "a", "xx") == "xxxxbbxxxxbb"); assert (replace(s, "aa", "xxx") == "xxxbbxxxbb"); - + assert (replace(s, "aa", "xx", 2) == "aabbxxbb"); assert (replace(s, 'a', 'x', 2) == "aabbxxbb"); assert (remove(s, 'a', 2) == "aabbbb"); @@ -422,7 +422,7 @@ void StringTest::testCat() assert (cat(s1, s2, s3, s4) == "onetwothreefour"); assert (cat(s1, s2, s3, s4, s5) == "onetwothreefourfive"); assert (cat(s1, s2, s3, s4, s5, s6) == "onetwothreefourfivesix"); - + std::vector vec; assert (cat(std::string(), vec.begin(), vec.end()) == ""); assert (cat(std::string(","), vec.begin(), vec.end()) == ""); @@ -520,7 +520,7 @@ void StringTest::testStringToFloat() float d = 12e34f; assert(strToFloat(format("12e34", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01e34); - + d = 1.234e30f; assert(strToFloat(format("1%c234e30", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); @@ -533,7 +533,7 @@ void StringTest::testStringToFloat() assertEqualDelta(d, result, 0.01); assert(strToFloat(format("-12%c34", ds), result, ds, ts)); assertEqualDelta(-12.34, result, 0.01); - + assert(strToFloat(format(" 12%c34", ds), result, ds, ts)); assertEqualDelta(12.34, result, 0.01); assert(strToFloat(format("12%c34 ", ds), result, ds, ts)); @@ -621,23 +621,23 @@ void StringTest::testStringToDouble() double d = 12e34; assert(strToDouble(format("12e34", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01e34); - + d = 1.234e100; assert(strToDouble(format("1%c234e100", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); assert(strToDouble(format("1%c234E+100", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); - + d = 1.234e-100; assert(strToDouble(format("1%c234E-100", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); - + d = -1.234e100; assert(strToDouble(format("-1%c234e+100", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); assert(strToDouble(format("-1%c234E100", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); - + d = 1.234e-100; assert(strToDouble(format(" 1%c234e-100 ", ds), result, ds, ts)); assertEqualDelta(d, result, 0.01); @@ -674,7 +674,7 @@ void StringTest::testStringToDouble() assertEqualDelta(d, result, 0.01); assert(strToDouble(format("-12%c34", ds), result, ds, ts)); assertEqualDelta(-12.34, result, 0.01); - + assert(strToDouble(format(" 12%c34", ds), result, ds, ts)); assertEqualDelta(12.34, result, 0.01); assert(strToDouble(format("12%c34 ", ds), result, ds, ts)); @@ -784,13 +784,13 @@ void StringTest::benchmarkStrToInt() sw.stop(); std::cout << "std::strtol Number: " << res << std::endl; double timeStrtol = sw.elapsed() / 1000.0; - + sw.restart(); for (int i = 0; i < 1000000; ++i) strToInt(num.c_str(), res, 10); sw.stop(); std::cout << "strToInt Number: " << res << std::endl; double timeStrToInt = sw.elapsed() / 1000.0; - + sw.restart(); for (int i = 0; i < 1000000; ++i) std::sscanf(num.c_str(), "%d", &res); sw.stop(); @@ -801,11 +801,11 @@ void StringTest::benchmarkStrToInt() std::cout << std::endl << "Timing and speedup relative to I/O stream:" << std::endl << std::endl; std::cout << std::setw(14) << "Stream:\t" << std::setw(10) << std::setfill(' ') << timeStream << "[ms]" << std::endl; - std::cout << std::setw(14) << "std::strtol:\t" << std::setw(10) << std::setfill(' ') << timeStrtol << "[ms]" << + std::cout << std::setw(14) << "std::strtol:\t" << std::setw(10) << std::setfill(' ') << timeStrtol << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeStrtol) << '\t' ; graph = (int) (timeStream / timeStrtol); for (int i = 0; i < graph; ++i) std::cout << '|'; - std::cout << std::endl << std::setw(14) << "strToInt:\t" << std::setw(10) << std::setfill(' ') << timeStrToInt << "[ms]" << + std::cout << std::endl << std::setw(14) << "strToInt:\t" << std::setw(10) << std::setfill(' ') << timeStrToInt << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeStrToInt) << '\t' ; graph = (int) (timeStream / timeStrToInt); for (int i = 0; i < graph; ++i) std::cout << '|'; @@ -843,7 +843,7 @@ void StringTest::benchmarkStrToFloat() sw.stop(); std::cout << "strToDouble Number: " << res << std::endl; double timeStrToDouble = sw.elapsed() / 1000.0; - + // standard sscanf sw.restart(); for (int i = 0; i < 1000000; ++i) std::sscanf(num.c_str(), "%lf", &res); @@ -862,11 +862,11 @@ void StringTest::benchmarkStrToFloat() std::cout << std::endl << "Timing and speedup relative to I/O stream:" << std::endl << std::endl; std::cout << std::setw(14) << "Stream:\t" << std::setw(10) << std::setfill(' ') << std::setprecision(4) << timeStream << "[ms]" << std::endl; - std::cout << std::setw(14) << "std::strtod:\t" << std::setw(10) << std::setfill(' ') << timeStdStrtod << "[ms]" << + std::cout << std::setw(14) << "std::strtod:\t" << std::setw(10) << std::setfill(' ') << timeStdStrtod << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeStdStrtod) << '\t' ; graph = (int) (timeStream / timeStdStrtod); for (int i = 0; i < graph; ++i) std::cout << '#'; - std::cout << std::endl << std::setw(14) << "strToDouble:\t" << std::setw(10) << std::setfill(' ') << timeStrToDouble << "[ms]" << + std::cout << std::endl << std::setw(14) << "strToDouble:\t" << std::setw(10) << std::setfill(' ') << timeStrToDouble << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeStrToDouble) << '\t' ; graph = (int) (timeStream / timeStrToDouble); for (int i = 0; i < graph; ++i) std::cout << '#'; @@ -986,13 +986,13 @@ void StringTest::testFloatToString() { double val = 1.03721575516329e-112; std::string str; - + assert (doubleToStr(str, val, 14, 21) == "1.03721575516329e-112"); assert (doubleToStr(str, val, 14, 22) == " 1.03721575516329e-112"); val = -val; assert (doubleToStr(str, val, 14, 22) == "-1.03721575516329e-112"); assert (doubleToStr(str, val, 14, 23) == " -1.03721575516329e-112"); - + val = -10372157551632.9; assert (doubleToStr(str, val, 1, 21, ',') == "-10,372,157,551,632.9"); assert (doubleToStr(str, val, 1, 22, ',') == " -10,372,157,551,632.9"); @@ -1046,7 +1046,7 @@ void StringTest::benchmarkFloatToStr() sw.stop(); std::cout << "std::sprintf Number: " << str << std::endl; double timeSprintf = sw.elapsed() / 1000.0; - + // POCO Way (via double-conversion) // no padding sw.restart(); @@ -1056,7 +1056,7 @@ void StringTest::benchmarkFloatToStr() std::cout << "doubleToStr(char) Number: " << buffer << std::endl; double timeDoubleToStrChar = sw.elapsed() / 1000.0; - // with padding + // with padding str = ""; sw.restart(); for (int i = 0; i < 1000000; ++i) doubleToStr(str, val); @@ -1067,16 +1067,16 @@ void StringTest::benchmarkFloatToStr() int graph; std::cout << std::endl << "Timing and speedup relative to I/O stream:" << std::endl << std::endl; std::cout << std::setw(14) << "Stream:\t" << std::setw(10) << std::setfill(' ') << std::setprecision(4) << timeStream << "[ms]" << std::endl; - - std::cout << std::setw(14) << "sprintf:\t" << std::setw(10) << std::setfill(' ') << timeSprintf << "[ms]" << + + std::cout << std::setw(14) << "sprintf:\t" << std::setw(10) << std::setfill(' ') << timeSprintf << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeSprintf) << '\t' ; graph = (int) (timeStream / timeSprintf); for (int i = 0; i < graph; ++i) std::cout << '#'; - - std::cout << std::endl << std::setw(14) << "doubleToChar:\t" << std::setw(10) << std::setfill(' ') << timeDoubleToStrChar << "[ms]" << + + std::cout << std::endl << std::setw(14) << "doubleToChar:\t" << std::setw(10) << std::setfill(' ') << timeDoubleToStrChar << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeDoubleToStrChar) << '\t' ; graph = (int) (timeStream / timeDoubleToStrChar); for (int i = 0; i < graph; ++i) std::cout << '#'; - - std::cout << std::endl << std::setw(14) << "doubleToString:\t" << std::setw(10) << std::setfill(' ') << timeDoubleToStrString << "[ms]" << + + std::cout << std::endl << std::setw(14) << "doubleToString:\t" << std::setw(10) << std::setfill(' ') << timeDoubleToStrString << "[ms]" << std::setw(10) << std::setfill(' ') << "Speedup: " << (timeStream / timeDoubleToStrString) << '\t' ; graph = (int) (timeStream / timeDoubleToStrString); for (int i = 0; i < graph; ++i) std::cout << '#'; @@ -1086,17 +1086,17 @@ 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 + // ??? on MSVC, the assert macro expansion // fails to compile when this string is inline ??? std::string str = "\"foo\\\\\""; assert (toJSON("foo\\") == str); diff --git a/Foundation/testsuite/src/VarTest.cpp b/Foundation/testsuite/src/VarTest.cpp index c2b01dd34..10d7d6271 100644 --- a/Foundation/testsuite/src/VarTest.cpp +++ b/Foundation/testsuite/src/VarTest.cpp @@ -72,7 +72,7 @@ void VarTest::testInt8() { Poco::Int8 src = 32; Var a1 = src; - + assert (a1.type() == typeid(Poco::Int8)); std::string s1; @@ -124,10 +124,10 @@ void VarTest::testInt8() std::string t2; a2.convert(t2); assert (s1 == t2); - + Int8 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -215,7 +215,7 @@ void VarTest::testInt16() Int16 value = a1.extract(); assert (value == 32); - + try { Int32 value2; value2 = a1.extract(); @@ -248,7 +248,7 @@ void VarTest::testInt32() { Poco::Int32 src = 32; Var a1 = src; - + assert (a1.type() == typeid(Poco::Int32)); std::string s1; @@ -300,10 +300,10 @@ void VarTest::testInt32() std::string t2; a2.convert(t2); assert (s1 == t2); - + Int32 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -336,7 +336,7 @@ void VarTest::testInt64() { Poco::Int64 src = 32; Var a1 = src; - + assert (a1.type() == typeid(Poco::Int64)); std::string s1; @@ -391,7 +391,7 @@ void VarTest::testInt64() Int64 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -479,7 +479,7 @@ void VarTest::testUInt8() UInt8 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -567,7 +567,7 @@ void VarTest::testUInt16() UInt16 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -655,7 +655,7 @@ void VarTest::testUInt32() UInt32 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -743,7 +743,7 @@ void VarTest::testUInt64() UInt64 value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -776,7 +776,7 @@ void VarTest::testBool() { bool src = true; Var a1 = src; - + assert (a1.type() == typeid(bool)); std::string s1; @@ -831,7 +831,7 @@ void VarTest::testBool() bool value = a1.extract(); assert (value); - + try { Int16 value2; value2 = a1.extract(); @@ -847,7 +847,7 @@ void VarTest::testChar() { char src = ' '; Var a1 = src; - + assert (a1.type() == typeid(char)); std::string s1; @@ -902,7 +902,7 @@ void VarTest::testChar() char value = a1.extract(); assert (value == ' '); - + try { Int16 value2; value2 = a1.extract(); @@ -921,7 +921,7 @@ void VarTest::testFloat() float src = 32.0f; Var a1 = src; - + assert (a1.type() == typeid(float)); std::string s1; @@ -976,7 +976,7 @@ void VarTest::testFloat() float value = a1.extract(); assert (value == 32.0f); - + try { Int16 value2; value2 = a1.extract(); @@ -1013,7 +1013,7 @@ void VarTest::testDouble() double src = 32.0; Var a1 = src; - + assert (a1.type() == typeid(double)); std::string s1; @@ -1068,7 +1068,7 @@ void VarTest::testDouble() double value = a1.extract(); assert (value == 32.0); - + try { Int16 value2; value2 = a1.extract(); @@ -1078,7 +1078,7 @@ void VarTest::testDouble() { } - + Var a3 = a1 + 1.0; assert (a3 == 33.0); a3 = a1 - 1.0; @@ -1101,7 +1101,7 @@ void VarTest::testDouble() void VarTest::testString() { Var a1("32"); - + assert (a1.type() == typeid(std::string)); std::string s1; @@ -1152,7 +1152,7 @@ void VarTest::testString() const std::string& value = a1.extract(); assert (value == "32"); - + try { Int16 value2; value2 = a1.extract(); @@ -1186,7 +1186,7 @@ void VarTest::testLong() { long src = 32; Var a1 = src; - + assert (a1.type() == typeid(long)); std::string s1; @@ -1238,10 +1238,10 @@ void VarTest::testLong() std::string t2; a2.convert(t2); assert (s1 == t2); - + long value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -1274,7 +1274,7 @@ void VarTest::testULong() { unsigned long src = 32; Var a1 = src; - + assert (a1.type() == typeid(unsigned long)); std::string s1; @@ -1329,7 +1329,7 @@ void VarTest::testULong() unsigned long value = a1.extract(); assert (value == 32); - + try { Int16 value2; value2 = a1.extract(); @@ -1607,26 +1607,26 @@ void VarTest::testArithmeticOperators() assert (any3 == "123456789"); assert (("xyz" + any3) == "xyz123456789"); - try { any3 = any1 - any2; fail ("must fail"); } + try { any3 = any1 - any2; fail ("must fail"); } catch (InvalidArgumentException&){} - try { any3 -= any2; fail ("must fail"); } + try { any3 -= any2; fail ("must fail"); } catch (InvalidArgumentException&){} - try { any3 = any1 * any2; fail ("must fail"); } + try { any3 = any1 * any2; fail ("must fail"); } catch (InvalidArgumentException&){} - try { any3 *= any2; fail ("must fail"); } + try { any3 *= any2; fail ("must fail"); } catch (InvalidArgumentException&){} - try { any3 = any1 / any2; fail ("must fail"); } + try { any3 = any1 / any2; fail ("must fail"); } catch (InvalidArgumentException&){} - try { any3 /= any2; fail ("must fail"); } + try { any3 /= any2; fail ("must fail"); } catch (InvalidArgumentException&){} any1 = 10; - + assert (any1++ == 10); assert (any1 == 11); assert (++any1 == 12); @@ -1637,16 +1637,16 @@ void VarTest::testArithmeticOperators() any1 = 1.23; - try { ++any1; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any1++; fail ("must fail"); } + try { ++any1; fail ("must fail"); } catch (InvalidArgumentException&){} - try { --any1; fail ("must fail"); } + try { any1++; fail ("must fail"); } catch (InvalidArgumentException&){} - - try { any1--; fail ("must fail"); } + + try { --any1; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any1--; fail ("must fail"); } catch (InvalidArgumentException&){} } @@ -1733,9 +1733,9 @@ void VarTest::testCtor() Var a1(42); Var a2(a1); Var a3; - + a3 = a1; - + assert (a2 == 42); assert (a3 == 42); } @@ -2157,7 +2157,7 @@ void VarTest::testArrayOfStructsToString() "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" } " "] ] ]"; - + assert (res == expected); assert (a1.toString() == res); } @@ -2388,7 +2388,7 @@ void VarTest::testJSONDeserializeStruct() void VarTest::testDate() { Poco::DateTime dtNow(2007, 3, 13, 8, 12, 15); - + Poco::Timestamp tsNow = dtNow.timestamp(); Poco::LocalDateTime ldtNow(dtNow.timestamp()); Var dt(dtNow); @@ -2425,7 +2425,7 @@ void VarTest::testGetIdxMustThrow(Var& a1, std::vector::size_type n) { try { - Var val1 = a1[n]; + Var val1 = a1[n]; fail("bad cast - must throw"); val1 = 0; // silence the compiler } @@ -2439,7 +2439,7 @@ void VarTest::testGetIdxMustThrow(Var& a1, std::vector::size_type n) try { const Var& c1 = a1; - const Var& cval1 = c1[n]; + const Var& cval1 = c1[n]; fail("bad const cast - must throw"); assert (cval1 == c1); // silence the compiler } diff --git a/JSON/testsuite/src/JSONTest.cpp b/JSON/testsuite/src/JSONTest.cpp index 316241b6c..f7be93ff3 100644 --- a/JSON/testsuite/src/JSONTest.cpp +++ b/JSON/testsuite/src/JSONTest.cpp @@ -376,7 +376,7 @@ void JSONTest::testEmptyObject() void JSONTest::testComplexObject() { - std::string json = + std::string json = "{" "\"id\": 1," "\"jsonrpc\": \"2.0\"," @@ -430,7 +430,7 @@ void JSONTest::testComplexObject() "}" "]" "}"; - + Parser parser; Var result; @@ -599,7 +599,7 @@ void JSONTest::testObjectProperty() } assert(result.type() == typeid(Object::Ptr)); - + Object::Ptr object = result.extract(); assert (object->isObject("test")); assert (!object->isArray("test")); @@ -1104,7 +1104,7 @@ void JSONTest::testComment() catch(Poco::SyntaxException&) { } - + parser.reset(); parser.setAllowComments(true); try @@ -1187,7 +1187,7 @@ void JSONTest::testPrintHandler() "}" ); - json = + json = "{" "\"array\":" "[" @@ -1453,7 +1453,7 @@ void JSONTest::testStringifyPreserveOrder() assert (ostr.str() == "{\"Simpsons\":{\"husband\":{\"name\":\"Homer\",\"age\":38},\"wife\":{\"name\":\"Marge\",\"age\":36}," "\"children\":[\"Bart\",\"Lisa\",\"Maggie\"]," "\"address\":{\"number\":742,\"street\":\"Evergreen Terrace\",\"town\":\"Springfield\"}}}"); - + ostr.str(""); Stringifier::stringify(result, ostr, 1); assert (ostr.str() == "{\n" @@ -1473,7 +1473,7 @@ void JSONTest::testStringifyPreserveOrder() " ],\n" " \"address\" : {\n" " \"number\" : 742,\n" - " \"street\" : \"Evergreen Terrace\",\n" + " \"street\" : \"Evergreen Terrace\",\n" " \"town\" : \"Springfield\"\n" " }\n" " }\n" @@ -1498,7 +1498,7 @@ void JSONTest::testStringifyPreserveOrder() " ],\n" " \"address\" : {\n" " \"number\" : 742,\n" - " \"street\" : \"Evergreen Terrace\",\n" + " \"street\" : \"Evergreen Terrace\",\n" " \"town\" : \"Springfield\"\n" " }\n" " }\n" @@ -1523,7 +1523,7 @@ void JSONTest::testStringifyPreserveOrder() " ],\n" " \"address\" : {\n" " \"number\" : 742,\n" - " \"street\" : \"Evergreen Terrace\",\n" + " \"street\" : \"Evergreen Terrace\",\n" " \"town\" : \"Springfield\"\n" " }\n" " }\n" @@ -1534,16 +1534,16 @@ void JSONTest::testStringifyPreserveOrder() assert (ds["Simpsons"]["husband"].isStruct()); assert (ds["Simpsons"]["husband"]["name"] == "Homer"); assert (ds["Simpsons"]["husband"]["age"] == 38); - + assert (ds["Simpsons"]["wife"].isStruct()); assert (ds["Simpsons"]["wife"]["name"] == "Marge"); assert (ds["Simpsons"]["wife"]["age"] == 36); - + assert (ds["Simpsons"]["children"].isArray()); assert (ds["Simpsons"]["children"][0] == "Bart"); assert (ds["Simpsons"]["children"][1] == "Lisa"); assert (ds["Simpsons"]["children"][2] == "Maggie"); - + assert (ds["Simpsons"]["address"].isStruct()); assert (ds["Simpsons"]["address"]["number"] == 742); assert (ds["Simpsons"]["address"]["street"] == "Evergreen Terrace"); @@ -1625,7 +1625,7 @@ void JSONTest::testInvalidJanssonFiles() parser.parse(fis); result = parser.asVar(); // We shouldn't get here. - std::cout << "We didn't get an exception. This is the result: " << result.convert() << std::endl; + std::cout << "We didn't get an exception. This is the result: " << result.convert() << std::endl; fail(result.convert()); } catch(JSONException&) @@ -1668,7 +1668,7 @@ void JSONTest::testInvalidUnicodeJanssonFiles() parser.parse(fis); result = parser.asVar(); // We shouldn't get here. - std::cout << "We didn't get an exception. This is the result: " << result.convert() << std::endl; + std::cout << "We didn't get an exception. This is the result: " << result.convert() << std::endl; fail(result.convert()); } catch(JSONException&) @@ -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(); + + 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; } diff --git a/JSON/testsuite/src/JSONTest.h b/JSON/testsuite/src/JSONTest.h index e705bf4bb..1d5fe832f 100644 --- a/JSON/testsuite/src/JSONTest.h +++ b/JSON/testsuite/src/JSONTest.h @@ -74,10 +74,11 @@ public: void testInvalidJanssonFiles(); void testTemplate(); void testItunes(); - void testUnicode(); + void testUnicode(); void testInvalidUnicodeJanssonFiles(); void testSmallBuffer(); - + void testEscape0(); + void testEscapeUnicode(); void setUp(); void tearDown();