GH #144: Poco::Dynamic emits invalid JSON

GH #144: Poco::Dynamic emits invalid JSON
This commit is contained in:
Aleksandar Fabijanic
2013-04-06 11:04:33 -05:00
parent d0ff7ccf7c
commit f9aea9acdf
7 changed files with 74 additions and 22 deletions

View File

@@ -120,6 +120,13 @@ public:
return _data.second;
}
std::string toString()
{
std::string str;
Var(*this).convert<std::string>(str);
return str;
}
private:
Data _data;
};
@@ -208,9 +215,9 @@ public:
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ ");
Var key(_val.first());
appendJSONString(val, key);
appendJSONKey(val, key);
val.append(" : ");
appendJSONString(val, _val.second());
appendJSONValue(val, _val.second());
val.append(" }");
}
@@ -357,9 +364,9 @@ public:
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ ");
Var key(_val.first());
appendJSONString(val, key);
appendJSONKey(val, key);
val.append(" : ");
appendJSONString(val, _val.second());
appendJSONValue(val, _val.second());
val.append(" }");
}

View File

@@ -204,6 +204,13 @@ public:
return keys;
}
std::string toString()
{
std::string str;
Var(*this).convert<std::string>(str);
return str;
}
private:
Data _data;
};
@@ -294,20 +301,20 @@ public:
if (!_val.empty())
{
Var key(it->first);
appendJSONString(val, key);
appendJSONKey(val, key);
val.append(" : ");
appendJSONString(val, it->second);
appendJSONValue(val, it->second);
++it;
}
for (; it != itEnd; ++it)
{
val.append(", ");
Var key(it->first);
appendJSONString(val, key);
appendJSONKey(val, key);
val.append(" : ");
appendJSONString(val, it->second);
appendJSONValue(val, it->second);
}
val.append(" }");
val.append(" }");
}
void convert(Poco::DateTime&) const
@@ -465,18 +472,18 @@ public:
if (!_val.empty())
{
Var key(it->first);
appendJSONString(val, key);
appendJSONKey(val, key);
val.append(" : ");
appendJSONString(val, it->second);
appendJSONValue(val, it->second);
++it;
}
for (; it != itEnd; ++it)
{
val.append(", ");
Var key(it->first);
appendJSONString(val, key);
appendJSONKey(val, key);
val.append(" : ");
appendJSONString(val, it->second);
appendJSONValue(val, it->second);
}
val.append(" }");
}

View File

@@ -70,8 +70,20 @@ bool Foundation_API isJSONString(const Var& any);
/// Returns true for values that should be JSON-formatted as string.
void Foundation_API appendJSONKey(std::string& val, const Var& any);
/// Converts the any to a JSON key (i.e. wraps it into double quotes
/// regardless of the underlying type) and appends it to val.
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 string (i.e. wraps it into double quotes)
/// regardless of the underlying type) and appends it to val.
void Foundation_API appendJSONValue(std::string& val, const Var& any);
/// Converts the any to a JSON value (if underlying type qualifies
/// as string - see isJSONString() - , it is wrapped into double quotes)
/// and appends it to val
class Foundation_API VarHolder
@@ -2694,14 +2706,14 @@ public:
typename std::vector<T>::const_iterator itEnd = _val.end();
if (!_val.empty())
{
appendJSONString(val, *it);
appendJSONValue(val, *it);
++it;
}
for (; it != itEnd; ++it)
{
val.append(", ");
appendJSONString(val, *it);
appendJSONValue(val, *it);
}
val.append(" ]");
}