2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __CONTEXT_JSON_H__
18 #define __CONTEXT_JSON_H__
20 #include <sys/types.h>
24 #include <types_internal.h>
26 #define _J(cmt, jobj) \
28 _SD("%s: %s", (cmt), jobj.str().c_str()); \
31 #define EMPTY_JSON_OBJECT "{}"
32 #define DEFAULT_PRECISION 3
38 typedef struct _JsonNode json_node_t;
39 json_node_t *json_node;
41 void parse(const char* s);
44 /* For json vs json comparison */
45 bool get_member_list(json_node_t* node, std::list<std::string>& list);
46 bool node_equals(json_node_t* lhs, json_node_t* rhs);
47 bool value_equals(json_node_t* lhs, json_node_t* rhs);
48 bool object_equals(json_node_t* lhs, json_node_t* rhs);
49 bool array_equals(json_node_t* lhs, json_node_t* rhs);
54 json(const std::string& s);
56 /* This json(const json& j) only copies the reference to the underlying json node.
57 * Therefore, changes applied to a json object affect the other.
58 * If you need to create a 'real' copy of a json, which can be manipulated separately,
59 * utilize the str() function, e.g., ctx::json copy(original.str());
65 json& operator=(const char* s);
66 json& operator=(const std::string& s);
68 /* This operator=(const json& j) only copies the reference to the underlying json node.
69 * Therefore, changes applied to a json object affect the other.
70 * If you need to create a 'real' copy of a json, which can be manipulated separately,
71 * utilize the str() function, e.g., ctx::json copy = original.str();
73 json& operator=(const json& j);
75 bool operator==(const json& rhs);
76 bool operator!=(const json& rhs);
81 bool get_keys(std::list<std::string>* list);
83 bool set(const char* path, const char* key, json& val);
84 bool set(const char* path, const char* key, int val);
85 bool set(const char* path, const char* key, int64_t val);
86 bool set(const char* path, const char* key, double val, int prec = DEFAULT_PRECISION);
87 bool set(const char* path, const char* key, std::string val);
88 bool set(const char* path, const char* key, GVariant *val);
90 bool get(const char* path, const char* key, json* val);
91 bool get(const char* path, const char* key, int* val);
92 bool get(const char* path, const char* key, int64_t* val);
93 bool get(const char* path, const char* key, double* val);
94 bool get(const char* path, const char* key, std::string* val);
95 bool get(const char* path, const char* key, GVariant **val);
97 int array_get_size(const char* path, const char* key);
99 bool array_append(const char* path, const char* key, json& val);
100 bool array_append(const char* path, const char* key, int val);
101 bool array_append(const char* path, const char* key, int64_t val);
102 bool array_append(const char* path, const char* key, double val, int prec = DEFAULT_PRECISION);
103 bool array_append(const char* path, const char* key, std::string val);
105 bool array_set_at(const char* path, const char* key, int index, json& val);
106 bool array_set_at(const char* path, const char* key, int index, int val);
107 bool array_set_at(const char* path, const char* key, int index, int64_t val);
108 bool array_set_at(const char* path, const char* key, int index, double val, int prec = DEFAULT_PRECISION);
109 bool array_set_at(const char* path, const char* key, int index, std::string val);
111 bool get_array_elem(const char* path, const char* key, int index, json* val);
112 bool get_array_elem(const char* path, const char* key, int index, int* val);
113 bool get_array_elem(const char* path, const char* key, int index, int64_t* val);
114 bool get_array_elem(const char* path, const char* key, int index, double* val);
115 bool get_array_elem(const char* path, const char* key, int index, std::string* val);
118 } /* namespace ctx */
120 #endif // __CONTEXT_JSON_H__