[SF 2520679] JSON strings enclosed in single quotes

[SF 2520849] Dynamic/Pair support
This commit is contained in:
Aleksandar Fabijanic 2009-01-19 20:08:04 +00:00
Родитель 363711ed39
Коммит c92d329399
11 изменённых файлов: 523 добавлений и 34 удалений

Просмотреть файл

@ -3653,6 +3653,11 @@
RelativePath=".\include\Poco\DynamicStruct.h" RelativePath=".\include\Poco\DynamicStruct.h"
> >
</File> </File>
<File
RelativePath=".\include\Poco\Dynamic\Pair.h"
>
</File>
<File <File
RelativePath=".\include\Poco\Dynamic\Struct.h" RelativePath=".\include\Poco\Dynamic\Struct.h"
> >

Просмотреть файл

@ -4741,6 +4741,10 @@
RelativePath=".\include\Poco\DynamicStruct.h" RelativePath=".\include\Poco\DynamicStruct.h"
> >
</File> </File>
<File
RelativePath=".\include\Poco\Dynamic\Pair.h"
>
</File>
<File <File
RelativePath=".\include\Poco\Dynamic\Struct.h" RelativePath=".\include\Poco\Dynamic\Struct.h"
> >

Просмотреть файл

@ -4732,6 +4732,10 @@
RelativePath=".\include\Poco\DynamicStruct.h" RelativePath=".\include\Poco\DynamicStruct.h"
> >
</File> </File>
<File
RelativePath=".\include\Poco\Dynamic\Pair.h"
>
</File>
<File <File
RelativePath=".\include\Poco\Dynamic\Struct.h" RelativePath=".\include\Poco\Dynamic\Struct.h"
> >

Просмотреть файл

@ -0,0 +1,427 @@
//
// Pair.h
//
// $Id: //poco/Main/Foundation/include/Poco/Dynamic/Pair.h#9 $
//
// Library: Foundation
// Package: Dynamic
// Module: Pair
//
// Definition of the Pair class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_Pair_INCLUDED
#define Foundation_Pair_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/VarHolder.h"
#include <utility>
namespace Poco {
namespace Dynamic {
template <typename K>
class Pair
/// Pair allows to define a pair of values.
{
public:
typedef typename std::pair<K, Var> Data;
Pair(): _data()
/// Creates an empty Pair
{
}
Pair(const Pair& other): _data(other._data)
/// Creates the Pair from another pair.
{
}
Pair(const Data& val): _data(val)
/// Creates the Pair from the given value.
{
}
template <typename T>
Pair(const std::pair<K, T>& val): _data(std::make_pair(val.first, Var(val.second)))
/// Creates Pair form standard pair.
{
}
template <typename T>
Pair(const K& first, const T& second): _data(std::make_pair(first, Var(second)))
/// Creates pair from two values.
{
}
virtual ~Pair()
/// Destroys the Pair.
{
}
Pair& swap(Pair& other)
/// Swaps the content of the two Pairs.
{
std::swap(_data, other._data);
return *this;
}
Pair& operator = (const Pair& other)
/// Copy constructs Pair from another pair.
{
Pair(other).swap(*this);
return *this;
}
inline const K& first() const
/// Returns the first member of the pair.
{
return _data.first;
}
inline const Var& second() const
/// Returns the second member of the pair.
{
return _data.second;
}
private:
Data _data;
};
template <>
class VarHolderImpl<Pair<std::string> >: public VarHolder
{
public:
VarHolderImpl(const Pair<std::string>& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(Pair<std::string>);
}
void convert(Int8& val) const
{
throw BadCastException("Cannot cast Pair type to Int8");
}
void convert(Int16& val) const
{
throw BadCastException("Cannot cast Pair type to Int16");
}
void convert(Int32& val) const
{
throw BadCastException("Cannot cast Pair type to Int32");
}
void convert(Int64& val) const
{
throw BadCastException("Cannot cast Pair type to Int64");
}
void convert(UInt8& val) const
{
throw BadCastException("Cannot cast Pair type to UInt8");
}
void convert(UInt16& val) const
{
throw BadCastException("Cannot cast Pair type to UInt16");
}
void convert(UInt32& val) const
{
throw BadCastException("Cannot cast Pair type to UInt32");
}
void convert(UInt64& val) const
{
throw BadCastException("Cannot cast Pair type to UInt64");
}
void convert(bool& val) const
{
throw BadCastException("Cannot cast Pair type to bool");
}
void convert(float& val) const
{
throw BadCastException("Cannot cast Pair type to float");
}
void convert(double& val) const
{
throw BadCastException("Cannot cast Pair type to double");
}
void convert(char& val) const
{
throw BadCastException("Cannot cast Pair type to char");
}
void convert(std::string& val) const
{
// Serialize in JSON format: equals an object
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ ");
Var key(_val.first());
appendJSONString(val, key);
val.append(" : ");
appendJSONString(val, _val.second());
val.append(" }");
}
void convert(Poco::DateTime&) const
{
throw BadCastException("Pair -> Poco::DateTime");
}
void convert(Poco::LocalDateTime&) const
{
throw BadCastException("Pair -> Poco::LocalDateTime");
}
void convert(Poco::Timestamp&) const
{
throw BadCastException("Pair -> Poco::Timestamp");
}
VarHolder* clone() const
{
return new VarHolderImpl(_val);
}
const Pair<std::string>& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isStruct() const
{
return false;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
Pair<std::string> _val;
};
template <>
class VarHolderImpl<Pair<int> >: public VarHolder
{
public:
VarHolderImpl(const Pair<int>& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(Pair<int>);
}
void convert(Int8& val) const
{
throw BadCastException("Cannot cast Pair type to Int8");
}
void convert(Int16& val) const
{
throw BadCastException("Cannot cast Pair type to Int16");
}
void convert(Int32& val) const
{
throw BadCastException("Cannot cast Pair type to Int32");
}
void convert(Int64& val) const
{
throw BadCastException("Cannot cast Pair type to Int64");
}
void convert(UInt8& val) const
{
throw BadCastException("Cannot cast Pair type to UInt8");
}
void convert(UInt16& val) const
{
throw BadCastException("Cannot cast Pair type to UInt16");
}
void convert(UInt32& val) const
{
throw BadCastException("Cannot cast Pair type to UInt32");
}
void convert(UInt64& val) const
{
throw BadCastException("Cannot cast Pair type to UInt64");
}
void convert(bool& val) const
{
throw BadCastException("Cannot cast Pair type to bool");
}
void convert(float& val) const
{
throw BadCastException("Cannot cast Pair type to float");
}
void convert(double& val) const
{
throw BadCastException("Cannot cast Pair type to double");
}
void convert(char& val) const
{
throw BadCastException("Cannot cast Pair type to char");
}
void convert(std::string& val) const
{
// Serialize in JSON format: equals an object
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ ");
Var key(_val.first());
appendJSONString(val, key);
val.append(" : ");
appendJSONString(val, _val.second());
val.append(" }");
}
void convert(Poco::DateTime&) const
{
throw BadCastException("Pair -> Poco::DateTime");
}
void convert(Poco::LocalDateTime&) const
{
throw BadCastException("Pair -> Poco::LocalDateTime");
}
void convert(Poco::Timestamp&) const
{
throw BadCastException("Pair -> Poco::Timestamp");
}
VarHolder* clone() const
{
return new VarHolderImpl(_val);
}
const Pair<int>& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isStruct() const
{
return false;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
Pair<int> _val;
};
} // namespace Dynamic
} // namespace Poco
#endif // Foundation_Pair_INCLUDED

Просмотреть файл

@ -1,7 +1,7 @@
// //
// Struct.h // Struct.h
// //
// $Id: //poco/Main/Foundation/include/Poco/Struct.h#9 $ // $Id: //poco/Main/Foundation/include/Poco/Dynamic/Struct.h#9 $
// //
// Library: Foundation // Library: Foundation
// Package: Dynamic // Package: Dynamic
@ -69,7 +69,7 @@ public:
{ {
} }
Struct(const Data &val): _data(val) Struct(const Data& val): _data(val)
/// Creates the Struct from the given value. /// Creates the Struct from the given value.
{ {
} }
@ -286,9 +286,6 @@ public:
void convert(std::string& val) const void convert(std::string& val) const
{ {
// Serialize in JSON format: equals an object
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ "); val.append("{ ");
Struct<std::string>::ConstIterator it = _val.begin(); Struct<std::string>::ConstIterator it = _val.begin();
Struct<std::string>::ConstIterator itEnd = _val.end(); Struct<std::string>::ConstIterator itEnd = _val.end();
@ -460,9 +457,6 @@ public:
void convert(std::string& val) const void convert(std::string& val) const
{ {
// Serialize in JSON format: equals an object
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ "); val.append("{ ");
Struct<int>::ConstIterator it = _val.begin(); Struct<int>::ConstIterator it = _val.begin();
Struct<int>::ConstIterator itEnd = _val.end(); Struct<int>::ConstIterator itEnd = _val.end();

Просмотреть файл

@ -53,7 +53,6 @@ namespace Dynamic {
template <typename T> template <typename T>
class Struct; class Struct;
class Foundation_API Var class Foundation_API Var
/// Var allows to store data of different types and to convert between these types transparently. /// Var allows to store data of different types and to convert between these types transparently.
/// Var puts forth the best effort to provide intuitive and reasonable conversion semantics and prevent /// Var puts forth the best effort to provide intuitive and reasonable conversion semantics and prevent

Просмотреть файл

@ -65,6 +65,9 @@ namespace Dynamic {
class Var; class Var;
bool Foundation_API isJSONString(const Var& any);
/// Returns true for values that should be JSON-formatted as string.
void Foundation_API appendJSONString(std::string& val, const Var& any); void Foundation_API appendJSONString(std::string& val, const Var& any);
/// Converts the any to a JSON value and adds it to val /// Converts the any to a JSON value and adds it to val

Просмотреть файл

@ -423,12 +423,12 @@ Var Var::parseArray(const std::string& val, std::string::size_type& pos)
std::string Var::parseString(const std::string& val, std::string::size_type& pos) std::string Var::parseString(const std::string& val, std::string::size_type& pos)
{ {
static const std::string STR_STOP("'\""); static const std::string STR_STOP("\"");
static const std::string OTHER_STOP(" ,]}"); // we stop at space, ',', ']' or '}' static const std::string OTHER_STOP(" ,]}"); // we stop at space, ',', ']' or '}'
bool inString = false; bool inString = false;
//skip optional ' " //skip optional ' "
if (val[pos] == '\'' || val[pos] == '"') if (val[pos] == '"')
{ {
inString = true; inString = true;
++pos; ++pos;

Просмотреть файл

@ -52,17 +52,24 @@ VarHolder::~VarHolder()
} }
bool isJSONString(const Var& any)
{
return any.type() == typeid(std::string) ||
any.type() == typeid(char) ||
any.type() == typeid(Poco::DateTime) ||
any.type() == typeid(Poco::LocalDateTime);
}
void appendJSONString(std::string& val, const Var& any) void appendJSONString(std::string& val, const Var& any)
{ {
bool isJsonString = (any.type() == typeid(std::string) || any.type() == typeid(char) || any.type() == typeid(Poco::DateTime) || any.type() == typeid(Poco::LocalDateTime)); if (any.isEmpty()) val.append("null");
if (isJsonString) else
{ {
val.append(1, '\''); bool isStr = isJSONString(any);
} if (isStr) val.append(1, '"');
val.append(any.convert<std::string>()); val.append(any.convert<std::string>());
if (isJsonString) if (isStr) val.append(1, '"');
{
val.append(1, '\'');
} }
} }

Просмотреть файл

@ -36,8 +36,10 @@
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/Dynamic/Var.h" #include "Poco/Dynamic/Var.h"
#include "Poco/Bugcheck.h" #include "Poco/Bugcheck.h"
#include "Poco/DynamicStruct.h" #include "Poco/Dynamic/Struct.h"
#include "Poco/Dynamic/Pair.h"
#include <map>
#include <utility>
#if defined(_MSC_VER) && _MSC_VER < 1400 #if defined(_MSC_VER) && _MSC_VER < 1400
#pragma warning(disable:4800)//forcing value to bool 'true' or 'false' #pragma warning(disable:4800)//forcing value to bool 'true' or 'false'
@ -2010,6 +2012,49 @@ void VarTest::testDynamicStructInt()
} }
void VarTest::testDynamicPair()
{
Pair<int> aPair;
assert (0 == aPair.first());
try
{
std::string s = aPair.second().convert<std::string>();
fail ("must fail");
}
catch (InvalidAccessException&) { }
Var va(aPair);
assert ("{ 0 : null }" == va.convert<std::string>());
aPair = Pair<int>(4, "123");
assert ("123" == aPair.second());
va = aPair;
assert ("{ 4 : \"123\" }" == va.convert<std::string>());
int i = 1;
std::string s = "2";
Pair<int> iPair(i, s);
assert (1 == iPair.first());
assert ("2" == iPair.second());
Pair<std::string> sPair(s, i);
assert ("2" == sPair.first());
assert (1 == sPair.second());
std::pair<int, std::string> p = std::make_pair(i, s);
Pair<int> pPair(p);
assert (1 == pPair.first());
assert ("2" == pPair.second());
Var vp(pPair);
assert ("{ 1 : \"2\" }" == vp.convert<std::string>());
Var vs(sPair);
assert ("{ \"2\" : 1 }" == vs.convert<std::string>());
}
void VarTest::testArrayToString() void VarTest::testArrayToString()
{ {
std::string s1("string"); std::string s1("string");
@ -2019,7 +2064,7 @@ void VarTest::testArrayToString()
s16.push_back(s2); s16.push_back(s2);
Var a1(s16); Var a1(s16);
std::string res = a1.convert<std::string>(); std::string res = a1.convert<std::string>();
std::string expected("[ 'string', 23 ]"); std::string expected("[ \"string\", 23 ]");
assert (res == expected); assert (res == expected);
} }
@ -2032,8 +2077,7 @@ void VarTest::testStructToString()
aStruct["Age"] = 1; aStruct["Age"] = 1;
Var a1(aStruct); Var a1(aStruct);
std::string res = a1.convert<std::string>(); std::string res = a1.convert<std::string>();
std::string expected = "{ 'Age' : 1, 'First Name' : 'Junior', 'Last Name' : 'POCO' }"; std::string expected = "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }";
;
assert (res == expected); assert (res == expected);
} }
@ -2057,14 +2101,14 @@ void VarTest::testArrayOfStructsToString()
Var a1(s16); Var a1(s16);
std::string res = a1.convert<std::string>(); std::string res = a1.convert<std::string>();
std::string expected = "[ " std::string expected = "[ "
"{ 'Age' : 1, 'First Name' : 'Junior', 'Last Name' : 'POCO' }, " "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, "
"{ 'Age' : 100, 'First Name' : 'Senior', 'Last Name' : 'POCO' }, " "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, "
"[ " "[ "
"{ 'Age' : 1, 'First Name' : 'Junior', 'Last Name' : 'POCO' }, " "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, "
"{ 'Age' : 100, 'First Name' : 'Senior', 'Last Name' : 'POCO' }, " "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, "
"[ " "[ "
"{ 'Age' : 1, 'First Name' : 'Junior', 'Last Name' : 'POCO' }, " "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, "
"{ 'Age' : 100, 'First Name' : 'Senior', 'Last Name' : 'POCO' } " "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" } "
"] ] ]"; "] ] ]";
assert (res == expected); assert (res == expected);
@ -2090,8 +2134,8 @@ void VarTest::testStructWithArraysToString()
aStruct["Address"] = addr; aStruct["Address"] = addr;
Var a2(aStruct); Var a2(aStruct);
std::string res = a2.convert<std::string>(); std::string res = a2.convert<std::string>();
std::string expected = "{ 'Address' : { 'Country' : 'Carinthia', 'Number' : 4, 'Street' : 'Unknown' }, " std::string expected = "{ \"Address\" : { \"Country\" : \"Carinthia\", \"Number\" : 4, \"Street\" : \"Unknown\" }, "
"'Age' : 1, 'First Name' : 'Junior', 'Last Name' : [ 'string', 23 ] }"; "\"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : [ \"string\", 23 ] }";
assert (res == expected); assert (res == expected);
} }
@ -2435,6 +2479,7 @@ CppUnit::Test* VarTest::suite()
CppUnit_addTest(pSuite, VarTest, testIsStruct); CppUnit_addTest(pSuite, VarTest, testIsStruct);
CppUnit_addTest(pSuite, VarTest, testIsArray); CppUnit_addTest(pSuite, VarTest, testIsArray);
CppUnit_addTest(pSuite, VarTest, testArrayIdxOperator); CppUnit_addTest(pSuite, VarTest, testArrayIdxOperator);
CppUnit_addTest(pSuite, VarTest, testDynamicPair);
CppUnit_addTest(pSuite, VarTest, testDynamicStructBasics); CppUnit_addTest(pSuite, VarTest, testDynamicStructBasics);
CppUnit_addTest(pSuite, VarTest, testDynamicStructString); CppUnit_addTest(pSuite, VarTest, testDynamicStructString);
CppUnit_addTest(pSuite, VarTest, testDynamicStructInt); CppUnit_addTest(pSuite, VarTest, testDynamicStructInt);

Просмотреть файл

@ -72,6 +72,7 @@ public:
void testIsStruct(); void testIsStruct();
void testIsArray(); void testIsArray();
void testArrayIdxOperator(); void testArrayIdxOperator();
void testDynamicPair();
void testDynamicStructBasics(); void testDynamicStructBasics();
void testDynamicStructString(); void testDynamicStructString();
void testDynamicStructInt(); void testDynamicStructInt();