Merge remote-tracking branch 'pocoproject@github/develop' into develop

This commit is contained in:
FrancisANDRE 2015-12-08 10:28:03 +01:00
commit 92713554a0
9 changed files with 383 additions and 169 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
/// 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

@ -54,10 +54,24 @@ struct Foundation_API UTF8
static std::string& toUpperInPlace(std::string& str);
static std::string toLower(const std::string& str);
static std::string& toLowerInPlace(std::string& str);
static void removeBOM(std::string& str);
/// Remove the UTF-8 Byte Order Mark sequence (0xEF, 0xBB, 0xBF)
/// from the beginning of the string, if it's there.
static std::string escape(const std::string& s);
/// Escapes a string. Special characters like tab, backslash, ... are
/// escaped. Unicode characters are escaped to \uxxxx.
static std::string escape(const std::string::const_iterator& begin, const std::string::const_iterator& end);
/// Escapes a string. Special characters like tab, backslash, ... are
/// escaped. Unicode characters are escaped to \uxxxx.
static std::string unescape(const std::string& s);
/// Creates an UTF8 string from a string that contains escaped characters.
static std::string unescape(const std::string::const_iterator& begin, const std::string::const_iterator& end);
/// Creates an UTF8 string from a string that contains escaped characters.
};

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

@ -19,6 +19,8 @@
#include "Poco/TextIterator.h"
#include "Poco/TextConverter.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Ascii.h"
#include <algorithm>
@ -32,11 +34,11 @@ namespace
int UTF8::icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, std::string::const_iterator it2, std::string::const_iterator end2)
{
{
std::string::size_type sz = str.size();
if (pos > sz) pos = sz;
if (pos + n > sz) n = sz - pos;
TextIterator uit1(str.begin() + pos, str.begin() + pos + n, utf8);
TextIterator uit1(str.begin() + pos, str.begin() + pos + n, utf8);
TextIterator uend1(str.begin() + pos + n);
TextIterator uit2(it2, end2, utf8);
TextIterator uend2(end2);
@ -50,7 +52,7 @@ int UTF8::icompare(const std::string& str, std::string::size_type pos, std::stri
return 1;
++uit1; ++uit2;
}
if (uit1 == uend1)
return uit2 == uend2 ? 0 : -1;
else
@ -162,14 +164,211 @@ std::string& UTF8::toLowerInPlace(std::string& str)
void UTF8::removeBOM(std::string& str)
{
if (str.size() >= 3
&& static_cast<unsigned char>(str[0]) == 0xEF
&& static_cast<unsigned char>(str[1]) == 0xBB
if (str.size() >= 3
&& static_cast<unsigned char>(str[0]) == 0xEF
&& static_cast<unsigned char>(str[1]) == 0xBB
&& static_cast<unsigned char>(str[2]) == 0xBF)
{
str.erase(0, 3);
}
}
std::string UTF8::escape(const std::string &s)
{
return escape(s.begin(), s.end());
}
std::string UTF8::escape(const std::string::const_iterator& begin, const std::string::const_iterator& end)
{
static Poco::UInt32 offsetsFromUTF8[6] = {
0x00000000UL, 0x00003080UL, 0x000E2080UL,
0x03C82080UL, 0xFA082080UL, 0x82082080UL
};
std::string result;
std::string::const_iterator it = begin;
while(it != end)
{
Poco::UInt32 ch = 0;
unsigned int sz = 0;
do
{
ch <<= 6;
ch += (unsigned char)*it++;
sz++;
}
while (it != end && (*it & 0xC0) == 0x80 && sz < 6);
ch -= offsetsFromUTF8[sz-1];
if (ch == '\n') result += "\\n";
else if (ch == '\t') result += "\\t";
else if (ch == '\r') result += "\\r";
else if (ch == '\b') result += "\\b";
else if (ch == '\f') result += "\\f";
else if (ch == '\v') result += "\\v";
else if (ch == '\a') result += "\\a";
else if (ch == '\\') result += "\\\\";
else if (ch == '\"') result += "\\\"";
else if (ch == '/') result += "\\/";
else if (ch == '\0') result += "\\u0000";
else if (ch < 32 || ch == 0x7f)
{
result += "\\u";
NumberFormatter::appendHex(result, (unsigned short) ch, 4);
}
else if (ch > 0xFFFF)
{
ch -= 0x10000;
result += "\\u";
NumberFormatter::appendHex(result, (unsigned short) (( ch >> 10 ) & 0x03ff ) + 0xd800, 4);
result += "\\u";
NumberFormatter::appendHex(result, (unsigned short) (ch & 0x03ff ) + 0xdc00, 4);
}
else if (ch >= 0x80 && ch <= 0xFFFF)
{
result += "\\u";
NumberFormatter::appendHex(result, (unsigned short) ch, 4);
}
else
{
result += (char) ch;
}
}
return result;
}
std::string UTF8::unescape(const std::string &s)
{
return unescape(s.begin(), s.end());
}
std::string UTF8::unescape(const std::string::const_iterator& begin, const std::string::const_iterator& end)
{
std::string result;
std::string::const_iterator it = begin;
while (it != end)
{
Poco::UInt32 ch = (Poco::UInt32) *it++;
if (ch == '\\')
{
if ( it == end )
{
//Invalid sequence!
}
if (*it == 'n')
{
ch = '\n';
it++;
}
else if (*it == 't')
{
ch = '\t';
it++;
}
else if (*it == 'r')
{
ch = '\r';
it++;
}
else if (*it == 'b')
{
ch = '\b';
it++;
}
else if (*it == 'f')
{
ch = '\f';
it++;
}
else if (*it == 'v')
{
ch = '\v';
it++;
}
else if (*it == 'a')
{
ch = '\a';
it++;
}
else if (*it == 'u')
{
char digs[5];
memset(digs, 0, 5);
unsigned int dno = 0;
it++;
while (it != end && Ascii::isHexDigit(*it) && dno < 4) digs[dno++] = *it++;
if (dno > 0)
{
ch = strtol(digs, NULL, 16);
}
if( ch >= 0xD800 && ch <= 0xDBFF )
{
if ( it == end || *it != '\\' )
{
//Invalid sequence!
}
else
{
it++;
if ( it == end || *it != 'u' )
{
//Invalid sequence!
}
else
{
it++;
}
}
// UTF-16 surrogate pair. Go fetch other half
memset(digs, 0, 5);
dno = 0;
while (it != end && Ascii::isHexDigit(*it) && dno < 4) digs[dno++] = *it++;
if (dno > 0)
{
Poco::UInt32 temp = strtol(digs, NULL, 16);
if( temp >= 0xDC00 && temp <= 0xDFFF )
{
ch = ( ( ( ch - 0xD800 ) << 10 ) | ( temp - 0xDC00 ) ) + 0x10000;
}
}
}
}
else if (*it == 'U')
{
char digs[9];
memset(digs, 0, 9);
unsigned int dno = 0;
it++;
while (it != end && Ascii::isHexDigit(*it) && dno < 8)
{
digs[dno++] = *it++;
}
if (dno > 0)
{
ch = strtol(digs, NULL, 16);
}
}
}
unsigned char utf8[4];
UTF8Encoding encoding;
int sz = encoding.convert(ch, utf8, 4);
result.append((char*) utf8, sz);
}
return result;
}
} // namespace Poco

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,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<std::string>());
val += '"';
std::string json(val);
val.append(1, '"');
escape(json, any.convert<std::string>());
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<std::string>());
}

View File

@ -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<std::string, int, CILess> 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<std::string, CILess> 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<std::string> 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);

View File

@ -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<Int8>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -215,7 +215,7 @@ void VarTest::testInt16()
Int16 value = a1.extract<Int16>();
assert (value == 32);
try
{
Int32 value2; value2 = a1.extract<Int32>();
@ -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<Int32>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<Int64>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -479,7 +479,7 @@ void VarTest::testUInt8()
UInt8 value = a1.extract<UInt8>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -567,7 +567,7 @@ void VarTest::testUInt16()
UInt16 value = a1.extract<UInt16>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -655,7 +655,7 @@ void VarTest::testUInt32()
UInt32 value = a1.extract<UInt32>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -743,7 +743,7 @@ void VarTest::testUInt64()
UInt64 value = a1.extract<UInt64>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<bool>();
assert (value);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<char>();
assert (value == ' ');
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<float>();
assert (value == 32.0f);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<double>();
assert (value == 32.0);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<std::string>();
assert (value == "32");
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<long>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<unsigned long>();
assert (value == 32);
try
{
Int16 value2; value2 = a1.extract<Int16>();
@ -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<Var>::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<Var>::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
}

View File

@ -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<Object::Ptr>();
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::string>() << std::endl;
std::cout << "We didn't get an exception. This is the result: " << result.convert<std::string>() << std::endl;
fail(result.convert<std::string>());
}
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::string>() << std::endl;
std::cout << "We didn't get an exception. This is the result: " << result.convert<std::string>() << std::endl;
fail(result.convert<std::string>());
}
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<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

@ -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();