2 * Copyright (c) 2016 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 __RRS_JSON_H__
18 #define __RRS_JSON_H__
20 #include <sys/types.h>
25 #define _J(cmt, jobj) \
27 _SD("%s: %s", (cmt), jobj.str().c_str()); \
30 #define EMPTY_JSON_OBJECT "{}"
31 #define DEFAULT_PRECISION 3
38 Json(const std::string& s);
40 /* This Json(const Json& j) only copies the reference to the underlying Json node.
41 * Therefore, changes applied to a Json object affect the other.
42 * If you need to create a 'real' copy of a Json, which can be manipulated separately,
43 * utilize the str() function, e.g., ctx::Json copy(original.str());
49 Json& operator=(const char* s);
50 Json& operator=(const std::string& s);
52 /* This operator=(const Json& j) only copies the reference to the underlying Json node.
53 * Therefore, changes applied to a Json object affect the other.
54 * If you need to create a 'real' copy of a Json, which can be manipulated separately,
55 * utilize the str() function, e.g., ctx::Json copy = original.str();
57 Json& operator=(const Json& j);
59 bool operator==(const Json& rhs);
60 bool operator!=(const Json& rhs);
62 void load(const char *file);
67 bool getKeys(std::list<std::string>* list);
69 bool set(const char* path, const char* key, Json& val);
70 bool set(const char* path, const char* key, int val);
71 bool set(const char* path, const char* key, int64_t val);
72 bool set(const char* path, const char* key, double val, int prec = DEFAULT_PRECISION);
73 bool set(const char* path, const char* key, std::string val);
74 bool set(const char* path, const char* key, GVariant *val);
76 bool get(const char* path, const char* key, Json* val);
77 bool get(const char* path, const char* key, int* val);
78 bool get(const char* path, const char* key, int64_t* val);
79 bool get(const char* path, const char* key, double* val);
80 bool get(const char* path, const char* key, std::string* val);
81 bool get(const char* path, const char* key, GVariant **val);
83 int arrayGetSize(const char* path, const char* key);
85 bool arrayAppend(const char* path, const char* key, Json& val);
86 bool arrayAppend(const char* path, const char* key, int val);
87 bool arrayAppend(const char* path, const char* key, int64_t val);
88 bool arrayAppend(const char* path, const char* key, double val, int prec = DEFAULT_PRECISION);
89 bool arrayAppend(const char* path, const char* key, std::string val);
91 bool arraySetAt(const char* path, const char* key, int index, Json& val);
92 bool arraySetAt(const char* path, const char* key, int index, int val);
93 bool arraySetAt(const char* path, const char* key, int index, int64_t val);
94 bool arraySetAt(const char* path, const char* key, int index, double val, int prec = DEFAULT_PRECISION);
95 bool arraySetAt(const char* path, const char* key, int index, std::string val);
97 bool getArrayElem(const char* path, const char* key, int index, Json* val);
98 bool getArrayElem(const char* path, const char* key, int index, int* val);
99 bool getArrayElem(const char* path, const char* key, int index, int64_t* val);
100 bool getArrayElem(const char* path, const char* key, int index, double* val);
101 bool getArrayElem(const char* path, const char* key, int index, std::string* val);
103 std::string getStr(const char* key);
104 Json *getJson(const char* key);
106 typedef struct _JsonNode json_node_t;
107 json_node_t *__jsonNode;
109 void parse(const char* s);
112 /* For Json vs Json comparison */
113 bool getMemberList(json_node_t* node, std::list<std::string>& list);
114 bool nodeEquals(json_node_t* lhs, json_node_t* rhs);
115 bool valueEquals(json_node_t* lhs, json_node_t* rhs);
116 bool objectEquals(json_node_t* lhs, json_node_t* rhs);
117 bool arrayEquals(json_node_t* lhs, json_node_t* rhs);
121 #endif // __RRS_JSON_H__