Usage modern C++ features on JSON modules (enhanced) (#4613)

This commit is contained in:
Matej Kenda
2024-07-25 18:18:37 +02:00
committed by GitHub
parent 891c1e03bf
commit 5117e27515
19 changed files with 286 additions and 368 deletions

View File

@@ -217,7 +217,7 @@ public:
private:
void resetDynArray() const;
typedef SharedPtr<Poco::Dynamic::Array> ArrayPtr;
using ArrayPtr = SharedPtr<Poco::Dynamic::Array>;
ValueVec _values;
mutable ArrayPtr _pArray;
@@ -340,98 +340,96 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Array::Ptr);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val->stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@@ -441,22 +439,22 @@ public:
return _val;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}
@@ -474,98 +472,96 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Array);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = _val.size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val.stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@@ -575,22 +571,22 @@ public:
return _val;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}

View File

@@ -188,7 +188,7 @@ public:
bool isArray(const std::string& key) const;
/// Returns true when the given property contains an array.
bool isArray(ConstIterator& it) const;
bool isArray(const ConstIterator& it) const;
/// Returns true when the given property contains an array.
bool isNull(const std::string& key) const;
@@ -197,7 +197,7 @@ public:
bool isObject(const std::string& key) const;
/// Returns true when the given property contains an object.
bool isObject(ConstIterator& it) const;
bool isObject(const ConstIterator& it) const;
/// Returns true when the given property contains an object.
template<typename T>
@@ -207,7 +207,7 @@ public:
/// def will be returned.
{
T value = def;
ValueMap::const_iterator it = _values.find(key);
auto it = _values.find(key);
if (it != _values.end() && ! it->second.isEmpty())
{
try
@@ -255,9 +255,9 @@ public:
/// Insertion order preservation property is left intact.
private:
typedef std::deque<ValueMap::const_iterator> KeyList;
typedef Poco::DynamicStruct::Ptr StructPtr;
typedef Poco::OrderedDynamicStruct::Ptr OrdStructPtr;
using KeyList = std::deque<ValueMap::const_iterator>;
using StructPtr = Poco::DynamicStruct::Ptr;
using OrdStructPtr = Poco::OrderedDynamicStruct::Ptr;
void syncKeys(const KeyList& keys);
@@ -311,30 +311,28 @@ private:
if (obj->_preserveInsOrder)
{
KeyList::const_iterator it = obj->_keys.begin();
KeyList::const_iterator end = obj->_keys.end();
for (; it != end; ++it)
for (const auto& it: obj->_keys)
{
if (obj->isObject((*it)->first))
if (obj->isObject(it->first))
{
Object::Ptr pObj = obj->getObject((*it)->first);
Object::Ptr pObj = obj->getObject(it->first);
S str = makeStructImpl<S>(pObj);
ds.insert((*it)->first, str);
ds.insert(it->first, str);
}
else if (obj->isArray((*it)->first))
else if (obj->isArray(it->first))
{
Array::Ptr pArr = obj->getArray((*it)->first);
Array::Ptr pArr = obj->getArray(it->first);
std::vector<Poco::Dynamic::Var> v = Poco::JSON::Array::makeArray(pArr);
ds.insert((*it)->first, v);
ds.insert(it->first, v);
}
else
ds.insert((*it)->first, (*it)->second);
ds.insert(it->first, it->second);
}
}
else
{
ConstIterator it = obj->begin();
ConstIterator end = obj->end();
auto it = obj->begin();
const auto end = obj->end();
for (; it != end; ++it)
{
if (obj->isObject(it))
@@ -430,19 +428,19 @@ inline Object::ConstIterator Object::end() const
inline bool Object::has(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return it != _values.end();
}
inline bool Object::isArray(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return isArray(it);
}
inline bool Object::isArray(ConstIterator& it) const
inline bool Object::isArray(const ConstIterator& it) const
{
return it != _values.end() && (it->second.type() == typeid(Array::Ptr) || it->second.type() == typeid(Array));
}
@@ -450,19 +448,19 @@ inline bool Object::isArray(ConstIterator& it) const
inline bool Object::isNull(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return it == _values.end() || it->second.isEmpty();
}
inline bool Object::isObject(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
const auto it = _values.find(key);
return isObject(it);
}
inline bool Object::isObject(ConstIterator& it) const
inline bool Object::isObject(const ConstIterator& it) const
{
return it != _values.end() && (it->second.type() == typeid(Object::Ptr) || it->second.type() == typeid(Object));
}
@@ -478,8 +476,8 @@ inline void Object::remove(const std::string& key)
{
if (_preserveInsOrder)
{
KeyList::iterator it = _keys.begin();
KeyList::iterator end = _keys.end();
auto it = _keys.begin();
const auto end = _keys.end();
for (; it != end; ++it)
{
if (key == (*it)->first)
@@ -508,7 +506,7 @@ inline const Dynamic::Var& Object::getValue(ValueMap::const_iterator& it) const
inline const Dynamic::Var& Object::getValue(KeyList::const_iterator& it) const
{
ValueMap::const_iterator itv = _values.find((*it)->first);
const auto itv = _values.find((*it)->first);
if (itv != _values.end())
return itv->second;
else
@@ -531,101 +529,99 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Object::Ptr);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val->stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
//TODO: val = _val;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@@ -635,27 +631,27 @@ public:
return _val;
}
bool isArray() const
bool isArray() const override
{
return false;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}
@@ -673,101 +669,99 @@ public:
{
}
~VarHolderImpl()
{
}
~VarHolderImpl() override = default;
const std::type_info& type() const
const std::type_info& type() const override
{
return typeid(JSON::Object);
}
void convert(Int8&) const
void convert(Int8&) const override
{
throw BadCastException();
}
void convert(Int16&) const
void convert(Int16&) const override
{
throw BadCastException();
}
void convert(Int32&) const
void convert(Int32&) const override
{
throw BadCastException();
}
void convert(Int64&) const
void convert(Int64&) const override
{
throw BadCastException();
}
void convert(UInt8&) const
void convert(UInt8&) const override
{
throw BadCastException();
}
void convert(UInt16&) const
void convert(UInt16&) const override
{
throw BadCastException();
}
void convert(UInt32&) const
void convert(UInt32&) const override
{
throw BadCastException();
}
void convert(UInt64&) const
void convert(UInt64&) const override
{
throw BadCastException();
}
void convert(bool& value) const
void convert(bool& value) const override
{
value = _val.size() > 0;
}
void convert(float&) const
void convert(float&) const override
{
throw BadCastException();
}
void convert(double&) const
void convert(double&) const override
{
throw BadCastException();
}
void convert(char&) const
void convert(char&) const override
{
throw BadCastException();
}
void convert(std::string& s) const
void convert(std::string& s) const override
{
std::ostringstream oss;
_val.stringify(oss);
s = oss.str();
}
void convert(DateTime& /*val*/) const
void convert(DateTime& /*val*/) const override
{
//TODO: val = _val;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
void convert(LocalDateTime& /*ldt*/) const override
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
void convert(Timestamp& /*ts*/) const override
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = nullptr) const override
{
return cloneHolder(pVarHolder, _val);
}
@@ -777,27 +771,27 @@ public:
return _val;
}
bool isArray() const
bool isArray() const override
{
return false;
}
bool isInteger() const
bool isInteger() const override
{
return false;
}
bool isSigned() const
bool isSigned() const override
{
return false;
}
bool isNumeric() const
bool isNumeric() const override
{
return false;
}
bool isString() const
bool isString() const override
{
return false;
}

View File

@@ -40,61 +40,61 @@ public:
/// inside objects is preserved. Otherwise, items
/// will be sorted by keys.
virtual ~ParseHandler();
~ParseHandler() override;
/// Destroys the ParseHandler.
virtual void reset();
void reset() override;
/// Resets the handler state.
void startObject();
void startObject() override;
/// Handles a '{'; a new object is started.
void endObject();
void endObject() override;
/// Handles a '}'; the object is closed.
void startArray();
void startArray() override;
/// Handles a '['; a new array is started.
void endArray();
void endArray() override;
/// Handles a ']'; the array is closed.
void key(const std::string& k);
void key(const std::string& k) override;
/// A key is read
Dynamic::Var asVar() const;
Dynamic::Var asVar() const override;
/// Returns the result of the parser (an object or an array).
virtual void value(int v);
void value(int v) override;
/// An integer value is read
virtual void value(unsigned v);
void value(unsigned v) override;
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
virtual void value(Int64 v);
void value(Int64 v) override;
/// A 64-bit integer value is read
virtual void value(UInt64 v);
void value(UInt64 v) override;
/// An unsigned 64-bit integer value is read. This will only be
/// triggered if the value cannot fit into a signed 64-bit integer.
#endif
virtual void value(const std::string& s);
void value(const std::string& s) override;
/// A string value is read.
virtual void value(double d);
void value(double d) override;
/// A double value is read.
virtual void value(bool b);
void value(bool b) override;
/// A boolean value is read.
virtual void null();
void null() override;
/// A null value is read.
private:
void setValue(const Poco::Dynamic::Var& value);
typedef std::stack<Dynamic::Var> Stack;
using Stack = std::stack<Dynamic::Var>;
Stack _stack;
std::string _key;

View File

@@ -20,11 +20,7 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/ParserImpl.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Dynamic/Var.h"
#include <string>
@@ -68,7 +64,7 @@ public:
Parser(const Handler::Ptr& pHandler = new ParseHandler);
/// Creates JSON Parser, using the given Handler and buffer size.
virtual ~Parser();
~Parser() override;
/// Destroys JSON Parser.
void reset();

View File

@@ -19,11 +19,7 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Dynamic/Var.h"
#include <string>
@@ -183,7 +179,7 @@ inline Dynamic::Var ParserImpl::asVarImpl() const
{
if (_pHandler) return _pHandler->asVar();
return Dynamic::Var();
return {};
}

View File

@@ -44,56 +44,56 @@ public:
PrintHandler(std::ostream& out, unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
/// Creates the PrintHandler.
~PrintHandler();
~PrintHandler() override;
/// Destroys the PrintHandler.
void reset();
void reset() override;
/// Resets the handler state.
void startObject();
void startObject() override;
/// The parser has read a '{'; a new object is started.
/// If indent is greater than zero, a newline will be appended.
void endObject();
void endObject() override;
/// The parser has read a '}'; the object is closed.
void startArray();
void startArray() override;
/// The parser has read a [; a new array will be started.
/// If indent is greater than zero, a newline will be appended.
void endArray();
void endArray() override;
/// The parser has read a ]; the array is closed.
void key(const std::string& k);
void key(const std::string& k) override;
/// A key of an object is read; it will be written to the output,
/// followed by a ':'. If indent is greater than zero, the colon
/// is padded by a space before and after.
void null();
void null() override;
/// A null value is read; "null" will be written to the output.
void value(int v);
void value(int v) override;
/// An integer value is read.
void value(unsigned v);
void value(unsigned v) override;
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
void value(Int64 v);
void value(Int64 v) override;
/// A 64-bit integer value is read; it will be written to the output.
void value(UInt64 v);
void value(UInt64 v) override;
/// An unsigned 64-bit integer value is read; it will be written to the output.
#endif
void value(const std::string& value);
void value(const std::string& value) override;
/// A string value is read; it will be formatted and written to the output.
void value(double d);
void value(double d) override;
/// A double value is read; it will be written to the output.
void value(bool b);
void value(bool b) override;
/// A boolean value is read; it will be written to the output.
void comma();

View File

@@ -21,7 +21,6 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Template.h"
#include "Poco/Path.h"
#include "Poco/SharedPtr.h"
#include "Poco/Logger.h"
#include <vector>
#include <map>