2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
22 * @author Krzysztof Dynowski (k.dynowski@samsumg.com)
23 * @brief KVStore visitor with defaults values from json
26 #ifndef CONFIG_FROM_KVJSON_VISITOR_HPP
27 #define CONFIG_FROM_KVJSON_VISITOR_HPP
29 #include "config/from-kvstore-visitor.hpp"
30 #include "config/is-union.hpp"
31 #include <json/json.h>
35 class FromKVJsonVisitor {
37 FromKVJsonVisitor(const std::string& db, const std::string& json, const std::string& prefix)
38 : mStorePtr(new KVStore(db))
42 mObject = json_tokener_parse(json.c_str());
43 if (mObject == nullptr) {
44 throw ConfigException("Json parsing error");
49 ~FromKVJsonVisitor() {
50 json_object_put(mObject);
53 FromKVJsonVisitor(const FromKVJsonVisitor& v) :
54 mObject(v.mObject ? json_object_get(v.mObject) : nullptr),
55 mStorePtr(v.mStorePtr),
56 mKeyPrefix(v.mKeyPrefix),
60 FromKVJsonVisitor& operator=(const FromKVJsonVisitor&) = delete;
63 void visit(const std::string& name, T& value) {
64 getValue(name, value);
68 std::shared_ptr<KVStore> mStorePtr;
69 std::string mKeyPrefix;
73 // create visitor for field object (visitable object)
74 FromKVJsonVisitor(const FromKVJsonVisitor& v, const std::string& name, const bool isUnion) :
75 mStorePtr(v.mStorePtr), mIsUnion(isUnion || v.mIsUnion)
77 mKeyPrefix = key(v.mKeyPrefix, name);
78 json_object* object = nullptr;
79 if (v.mObject && !json_object_object_get_ex(v.mObject, name.c_str(), &object)) {
81 throw ConfigException("Missing json field " + mKeyPrefix);
83 mObject = object ? json_object_get(object) : nullptr;
86 // create visitor for vector i-th element (visitable object)
87 FromKVJsonVisitor(const FromKVJsonVisitor& v, int i, const bool isUnion) :
88 mStorePtr(v.mStorePtr), mIsUnion(isUnion || v.mIsUnion)
90 mKeyPrefix = key(v.mKeyPrefix, std::to_string(i));
91 json_object* object = nullptr;
93 object = json_object_array_get_idx(v.mObject, i);
94 checkType(object, json_type_object);
96 mObject = object ? json_object_get(object) : nullptr;
99 template<typename T, typename std::enable_if<!isVisitable<T>::value, int>::type = 0>
100 void getValue(const std::string& name, T& t)
102 std::string k = key(mKeyPrefix, name);
103 if (mStorePtr->exists(k)) {
104 t = mStorePtr->get<T>(k);
107 json_object* object = nullptr;
109 json_object_object_get_ex(mObject, name.c_str(), &object);
112 throw ConfigException("Missing json field " + key(mKeyPrefix, name));
114 fromJsonObject(object, t);
118 template<typename T, typename std::enable_if<isUnion<T>::value, int>::type = 0>
119 void getValue(const std::string& name, T& t)
121 FromKVJsonVisitor visitor(*this, name, true);
125 template<typename T, typename std::enable_if<isVisitable<T>::value && !isUnion<T>::value, int>::type = 0>
126 void getValue(const std::string& name, T& t)
128 FromKVJsonVisitor visitor(*this, name, false);
132 int getArraySize(std::string& name, json_object* object)
134 if (mStorePtr->exists(name)) {
135 return mStorePtr->get<int>(name);
138 return json_object_array_length(object);
144 void getValue(const std::string& name, std::vector<T>& value)
146 json_object* object = nullptr;
147 if (mObject && json_object_object_get_ex(mObject, name.c_str(), &object)) {
148 checkType(object, json_type_array);
151 std::string k = key(mKeyPrefix, name);
152 int length = getArraySize(k, object);
154 throw ConfigException("Missing array length " + k);
156 value.resize(static_cast<size_t>(length));
157 FromKVJsonVisitor visitor(*this, name, false);
158 if (mStorePtr->exists(k)) {
159 visitor.mObject = nullptr;
161 for (int i = 0; i < length; ++i) {
162 visitor.getValue(i, value[i]);
166 template<typename T, typename std::enable_if<!isVisitable<T>::value, int>::type = 0>
167 void getValue(int i, T& t)
169 std::string k = key(mKeyPrefix, std::to_string(i));
170 if (mStorePtr->exists(k)) {
171 t = mStorePtr->get<T>(k);
174 json_object* object = mObject ? json_object_array_get_idx(mObject, i) : nullptr;
176 throw ConfigException("Missing json array elem " + k);
178 fromJsonObject(object, t);
182 template<typename T, typename std::enable_if<isUnion<T>::value, int>::type = 0>
183 void getValue(int i, T& t)
185 FromKVJsonVisitor visitor(*this, i, true);
189 template<typename T, typename std::enable_if<isVisitable<T>::value && !isUnion<T>::value, int>::type = 0>
190 void getValue(int i, T& t)
192 FromKVJsonVisitor visitor(*this, i, false);
197 void getValue(int i, std::vector<T>& value)
199 std::string k = key(mKeyPrefix, std::to_string(i));
200 int length = getArraySize(k, mObject);
201 value.resize(static_cast<size_t>(length));
202 FromKVJsonVisitor visitor(*this, i, false);
203 if (mStorePtr->exists(k)) {
204 visitor.mObject = nullptr;
206 for (int i = 0; i < length; ++i) {
207 visitor.getValue(i, value[i]);
211 static void checkType(json_object* object, json_type type)
213 if (type != json_object_get_type(object)) {
214 throw ConfigException("Invalid field type " + std::to_string(type));
218 static void fromJsonObject(json_object* object, int& value)
220 checkType(object, json_type_int);
221 std::int64_t value64 = json_object_get_int64(object);
222 if (value64 > INT32_MAX || value64 < INT32_MIN) {
223 throw ConfigException("Value out of range");
225 value = static_cast<int>(value64);
228 static void fromJsonObject(json_object* object, std::int64_t& value)
230 checkType(object, json_type_int);
231 value = json_object_get_int64(object);
234 static void fromJsonObject(json_object* object, bool& value)
236 checkType(object, json_type_boolean);
237 value = json_object_get_boolean(object);
240 static void fromJsonObject(json_object* object, double& value)
242 checkType(object, json_type_double);
243 value = json_object_get_double(object);
246 static void fromJsonObject(json_object* object, std::string& value)
248 checkType(object, json_type_string);
249 value = json_object_get_string(object);
253 } // namespace config
255 #endif // CONFIG_FROM_KVJSON_VISITOR_HPP