#define CONFIG_TO_JSON_VISITOR_HPP
#include "config/is-visitable.hpp"
+#include "config/exception.hpp"
#include <json/json.h>
#include <string>
json_object_put(mObject);
}
+ ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
+
std::string toString() const
{
return json_object_to_json_string(mObject);
}
- template<class T>
+ template<typename T>
void visit(const std::string& name, const T& value)
{
json_object_object_add(mObject, name.c_str(), toJsonObject(value));
private:
json_object* mObject;
- ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
json_object* detach()
{
return ret;
}
- static json_object* toJsonObject(int value)
+ static json_object* toJsonObject(std::int32_t value)
{
return json_object_new_int(value);
}
return json_object_new_int64(value);
}
+ static json_object* toJsonObject(std::uint32_t value)
+ {
+ if (value > INT32_MAX) {
+ throw ConfigException("Value out of range");
+ }
+ return json_object_new_int(value);
+ }
+
+ static json_object* toJsonObject(std::uint64_t value)
+ {
+ if (value > INT64_MAX) {
+ throw ConfigException("Value out of range");
+ }
+ return json_object_new_int64(value);
+ }
+
static json_object* toJsonObject(bool value)
{
return json_object_new_boolean(value);
return json_object_new_string(value.c_str());
}
- template<class T>
+ template<typename T>
static json_object* toJsonObject(const std::vector<T>& value)
{
json_object* array = json_object_new_array();
return array;
}
- template<class T, class = typename std::enable_if<isVisitable<T>::value>::type>
+ template<typename T, class = typename std::enable_if<isVisitable<T>::value>::type>
static json_object* toJsonObject(const T& value)
{
ToJsonVisitor visitor;