Support for uints
[archive/platform/core/system/libConfig.git] / src / config / to-json-visitor.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22  * @brief   JSON visitor
23  */
24
25 #ifndef CONFIG_TO_JSON_VISITOR_HPP
26 #define CONFIG_TO_JSON_VISITOR_HPP
27
28 #include "config/is-visitable.hpp"
29 #include "config/exception.hpp"
30
31 #include <json/json.h>
32 #include <string>
33 #include <vector>
34
35 namespace config {
36
37 class ToJsonVisitor {
38
39 public:
40     ToJsonVisitor()
41         : mObject(json_object_new_object())
42     {
43     }
44
45     ToJsonVisitor(const ToJsonVisitor& visitor)
46         : mObject(json_object_get(visitor.mObject))
47     {
48     }
49
50     ~ToJsonVisitor()
51     {
52         json_object_put(mObject);
53     }
54
55     ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
56
57     std::string toString() const
58     {
59         return json_object_to_json_string(mObject);
60     }
61
62     template<typename T>
63     void visit(const std::string& name, const T& value)
64     {
65         json_object_object_add(mObject, name.c_str(), toJsonObject(value));
66     }
67 private:
68     json_object* mObject;
69
70
71     json_object* detach()
72     {
73         json_object* ret = mObject;
74         mObject = nullptr;
75         return ret;
76     }
77
78     static json_object* toJsonObject(std::int32_t value)
79     {
80         return json_object_new_int(value);
81     }
82
83     static json_object* toJsonObject(std::int64_t value)
84     {
85         return json_object_new_int64(value);
86     }
87
88     static json_object* toJsonObject(std::uint32_t value)
89     {
90         if (value > INT32_MAX) {
91             throw ConfigException("Value out of range");
92         }
93         return json_object_new_int(value);
94     }
95
96     static json_object* toJsonObject(std::uint64_t value)
97     {
98         if (value > INT64_MAX) {
99             throw ConfigException("Value out of range");
100         }
101         return json_object_new_int64(value);
102     }
103
104     static json_object* toJsonObject(bool value)
105     {
106         return json_object_new_boolean(value);
107     }
108
109     static json_object* toJsonObject(double value)
110     {
111         return json_object_new_double(value);
112     }
113
114     static json_object* toJsonObject(const std::string& value)
115     {
116         return json_object_new_string(value.c_str());
117     }
118
119     template<typename T>
120     static json_object* toJsonObject(const std::vector<T>& value)
121     {
122         json_object* array = json_object_new_array();
123         for (const T& v : value) {
124             json_object_array_add(array, toJsonObject(v));
125         }
126         return array;
127     }
128
129     template<typename T, class = typename std::enable_if<isVisitable<T>::value>::type>
130     static json_object* toJsonObject(const T& value)
131     {
132         ToJsonVisitor visitor;
133         value.accept(visitor);
134         return visitor.detach();
135     }
136 };
137
138 } // namespace config
139
140 #endif // CONFIG_TO_JSON_VISITOR_HPP