5b70eb95ad49ac74055ab64e0cc8406a053ae8ac
[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
30 #include <json/json.h>
31 #include <string>
32 #include <vector>
33
34 namespace config {
35
36 class ToJsonVisitor {
37
38 public:
39     ToJsonVisitor()
40         : mObject(json_object_new_object())
41     {
42     }
43
44     ToJsonVisitor(const ToJsonVisitor& visitor)
45         : mObject(json_object_get(visitor.mObject))
46     {
47     }
48
49     ~ToJsonVisitor()
50     {
51         json_object_put(mObject);
52     }
53
54     std::string toString() const
55     {
56         return json_object_to_json_string(mObject);
57     }
58
59     template<class T>
60     void visit(const std::string& name, const T& value)
61     {
62         json_object_object_add(mObject, name.c_str(), toJsonObject(value));
63     }
64 private:
65     json_object* mObject;
66
67     ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
68
69     json_object* detach()
70     {
71         json_object* ret = mObject;
72         mObject = nullptr;
73         return ret;
74     }
75
76     static json_object* toJsonObject(int value)
77     {
78         return json_object_new_int(value);
79     }
80
81     static json_object* toJsonObject(std::int64_t value)
82     {
83         return json_object_new_int64(value);
84     }
85
86     static json_object* toJsonObject(bool value)
87     {
88         return json_object_new_boolean(value);
89     }
90
91     static json_object* toJsonObject(double value)
92     {
93         return json_object_new_double(value);
94     }
95
96     static json_object* toJsonObject(const std::string& value)
97     {
98         return json_object_new_string(value.c_str());
99     }
100
101     template<class T>
102     static json_object* toJsonObject(const std::vector<T>& value)
103     {
104         json_object* array = json_object_new_array();
105         for (const T& v : value) {
106             json_object_array_add(array, toJsonObject(v));
107         }
108         return array;
109     }
110
111     template<class T, class = typename std::enable_if<isVisitable<T>::value>::type>
112     static json_object* toJsonObject(const T& value)
113     {
114         ToJsonVisitor visitor;
115         value.accept(visitor);
116         return visitor.detach();
117     }
118 };
119
120 } // namespace config
121
122 #endif // CONFIG_TO_JSON_VISITOR_HPP