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 json field " + key(mKeyPrefix, name));
156 value.resize(static_cast<size_t>(length));
157 FromKVJsonVisitor visitor(*this, name, false);
158 for (int i = 0; i < length; ++i) {
159 visitor.getValue(i, value[i]);
163 template<typename T, typename std::enable_if<!isVisitable<T>::value, int>::type = 0>
164 void getValue(int i, T& t)
166 std::string k = key(mKeyPrefix, std::to_string(i));
167 if (mStorePtr->exists(k)) {
168 t = mStorePtr->get<T>(k);
171 json_object* object = mObject ? json_object_array_get_idx(mObject, i) : nullptr;
173 throw ConfigException("Missing json array elem " + k);
175 fromJsonObject(object, t);
179 template<typename T, typename std::enable_if<isUnion<T>::value, int>::type = 0>
180 void getValue(int i, T& t)
182 FromKVJsonVisitor visitor(*this, i, true);
186 template<typename T, typename std::enable_if<isVisitable<T>::value && !isUnion<T>::value, int>::type = 0>
187 void getValue(int i, T& t)
189 FromKVJsonVisitor visitor(*this, i, false);
194 void getValue(int i, std::vector<T>& value)
196 std::string k = key(mKeyPrefix, std::to_string(i));
197 int length = getArraySize(k, mObject);
198 value.resize(static_cast<size_t>(length));
199 FromKVJsonVisitor visitor(*this, i, false);
200 for (int i = 0; i < length; ++i) {
201 visitor.getValue(i, value[i]);
205 static void checkType(json_object* object, json_type type)
207 if (type != json_object_get_type(object)) {
208 throw ConfigException("Invalid field type " + std::to_string(type));
212 static void fromJsonObject(json_object* object, int& value)
214 checkType(object, json_type_int);
215 std::int64_t value64 = json_object_get_int64(object);
216 if (value64 > INT32_MAX || value64 < INT32_MIN) {
217 throw ConfigException("Value out of range");
219 value = static_cast<int>(value64);
222 static void fromJsonObject(json_object* object, std::int64_t& value)
224 checkType(object, json_type_int);
225 value = json_object_get_int64(object);
228 static void fromJsonObject(json_object* object, bool& value)
230 checkType(object, json_type_boolean);
231 value = json_object_get_boolean(object);
234 static void fromJsonObject(json_object* object, double& value)
236 checkType(object, json_type_double);
237 value = json_object_get_double(object);
240 static void fromJsonObject(json_object* object, std::string& value)
242 checkType(object, json_type_string);
243 value = json_object_get_string(object);
247 } // namespace config
249 #endif // CONFIG_FROM_KVJSON_VISITOR_HPP