Only escape string values in Poco::JSON::Stringifier::stringify() #1027

This commit is contained in:
Alex Fabijanic 2016-04-23 14:52:03 -06:00
parent b14b6f3d7f
commit 0286d245ab
3 changed files with 56 additions and 22 deletions

View File

@ -208,51 +208,51 @@ public:
/// Returns the date and time expressed in UTC-based
/// time. UTC base time is midnight, October 15, 1582.
/// Resolution is 100 nanoseconds.
bool operator == (const DateTime& dateTime) const;
bool operator != (const DateTime& dateTime) const;
bool operator < (const DateTime& dateTime) const;
bool operator <= (const DateTime& dateTime) const;
bool operator > (const DateTime& dateTime) const;
bool operator >= (const DateTime& dateTime) const;
bool operator == (const DateTime& dateTime) const;
bool operator != (const DateTime& dateTime) const;
bool operator < (const DateTime& dateTime) const;
bool operator <= (const DateTime& dateTime) const;
bool operator > (const DateTime& dateTime) const;
bool operator >= (const DateTime& dateTime) const;
DateTime operator + (const Timespan& span) const;
DateTime operator - (const Timespan& span) const;
Timespan operator - (const DateTime& dateTime) const;
DateTime& operator += (const Timespan& span);
DateTime& operator -= (const Timespan& span);
void makeUTC(int tzd);
/// Converts a local time into UTC, by applying the given time zone differential.
void makeLocal(int tzd);
/// Converts a UTC time into a local time, by applying the given time zone differential.
static bool isLeapYear(int year);
/// Returns true if the given year is a leap year;
/// false otherwise.
static int daysOfMonth(int year, int month);
/// Returns the number of days in the given month
/// and year. Month is from 1 to 12.
static bool isValid(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
/// Checks if the given date and time is valid
/// (all arguments are within a proper range).
///
/// Returns true if all arguments are valid, false otherwise.
protected:
protected:
static double toJulianDay(Timestamp::UtcTimeVal utcTime);
/// Computes the Julian day for an UTC time.
static double toJulianDay(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
/// Computes the Julian day for a Gregorian calendar date and time.
/// See <http://vsg.cape.com/~pbaum/date/jdimp.htm>, section 2.3.1 for the algorithm.
static Timestamp::UtcTimeVal toUtcTime(double julianDay);
/// Computes the UTC time for a Julian day.
void computeGregorian(double julianDay);
/// Computes the Gregorian date for the given Julian day.
/// See <http://vsg.cape.com/~pbaum/date/injdimp.htm>, section 3.3.1 for the algorithm.
@ -369,13 +369,13 @@ inline bool DateTime::operator == (const DateTime& dateTime) const
}
inline bool DateTime::operator != (const DateTime& dateTime) const
inline bool DateTime::operator != (const DateTime& dateTime) const
{
return _utcTime != dateTime._utcTime;
}
inline bool DateTime::operator < (const DateTime& dateTime) const
inline bool DateTime::operator < (const DateTime& dateTime) const
{
return _utcTime < dateTime._utcTime;
}
@ -387,13 +387,13 @@ inline bool DateTime::operator <= (const DateTime& dateTime) const
}
inline bool DateTime::operator > (const DateTime& dateTime) const
inline bool DateTime::operator > (const DateTime& dateTime) const
{
return _utcTime > dateTime._utcTime;
}
inline bool DateTime::operator >= (const DateTime& dateTime) const
inline bool DateTime::operator >= (const DateTime& dateTime) const
{
return _utcTime >= dateTime._utcTime;
}

View File

@ -62,11 +62,15 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
if (any.type() == typeid(char)) formatString(value, out);
else out << value;
}
else
else if (any.isString() || any.isDateTime() || any.isDate() || any.isTime())
{
std::string value = any.convert<std::string>();
formatString(value, out);
}
else
{
out << any.convert<std::string>();
}
}

View File

@ -23,6 +23,8 @@
#include "Poco/TextConverter.h"
#include "Poco/Nullable.h"
#include "Poco/Dynamic/Struct.h"
#include "Poco/DateTime.h"
#include "Poco/DateTimeFormatter.h"
#include <set>
#include <iostream>
@ -30,7 +32,8 @@
using namespace Poco::JSON;
using namespace Poco::Dynamic;
using Poco::DynamicStruct;
using Poco::DateTime;
using Poco::DateTimeFormatter;
JSONTest::JSONTest(const std::string& name): CppUnit::TestCase("JSON")
{
@ -897,6 +900,12 @@ void JSONTest::testStringElement()
Poco::Dynamic::Array da = *array;
assert (da.size() == 1);
assert (da[0] == "value");
std::stringstream s;
json = "[ \"\\u0017\" ]";
Var v = Parser().parse(json);
Stringifier::condense(v, s);
assert(s.str() == "[\"\\u0017\"]");
}
@ -1245,6 +1254,27 @@ void JSONTest::testPrintHandler()
void JSONTest::testStringify()
{
std::ostringstream os;
Var i = 123;
Stringifier::stringify(i, os);
assert(os.str() == "123");
os.str("");
Var f = 123.456;
Stringifier::stringify(f, os);
assert(os.str() == "123.456");
os.str("");
Var s = "abcdef";
Stringifier::stringify(s, os);
assert(os.str() == "\"abcdef\"");
os.str("");
DateTime dt;
Var d = dt;
Stringifier::stringify(d, os);
assert(os.str() == std::string("\"" + DateTimeFormatter::format(dt, Poco::DateTimeFormat::ISO8601_FORMAT) + "\""));
std::string str1 = "\r";
std::string str2 = "\n";
Poco::JSON::Object obj1, obj2;