Support for uints
[archive/platform/core/system/libConfig.git] / src / config / to-json-visitor.hpp
index 5b70eb9..70f619b 100644 (file)
@@ -26,6 +26,7 @@
 #define CONFIG_TO_JSON_VISITOR_HPP
 
 #include "config/is-visitable.hpp"
+#include "config/exception.hpp"
 
 #include <json/json.h>
 #include <string>
@@ -51,12 +52,14 @@ public:
         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));
@@ -64,7 +67,6 @@ public:
 private:
     json_object* mObject;
 
-    ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
 
     json_object* detach()
     {
@@ -73,7 +75,7 @@ private:
         return ret;
     }
 
-    static json_object* toJsonObject(int value)
+    static json_object* toJsonObject(std::int32_t value)
     {
         return json_object_new_int(value);
     }
@@ -83,6 +85,22 @@ private:
         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);
@@ -98,7 +116,7 @@ private:
         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();
@@ -108,7 +126,7 @@ private:
         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;