Publishing project from SPIN to public
[platform/core/convergence/remote-rsc-svc.git] / src / common / Json.h
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __RRS_JSON_H__
18 #define __RRS_JSON_H__
19
20 #include <sys/types.h>
21 #include <glib.h>
22 #include <string>
23 #include <list>
24
25 #define _J(cmt, jobj) \
26 do { \
27         _SD("%s: %s", (cmt), jobj.str().c_str()); \
28 } while (0)
29
30 #define EMPTY_JSON_OBJECT       "{}"
31 #define DEFAULT_PRECISION       3
32
33
34 class Json {
35 public:
36         Json();
37         Json(const char* s);
38         Json(const std::string& s);
39
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());
44          */
45         Json(const Json& j);
46
47         ~Json();
48
49         Json& operator=(const char* s);
50         Json& operator=(const std::string& s);
51
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();
56          */
57         Json& operator=(const Json& j);
58
59         bool operator==(const Json& rhs);
60         bool operator!=(const Json& rhs);
61
62         void load(const char *file);
63
64         char* dupCStr();
65         std::string str();
66
67         bool getKeys(std::list<std::string>* list);
68
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);
75
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);
82
83         int arrayGetSize(const char* path, const char* key);
84
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);
90
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);
96
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);
102
103         std::string getStr(const char* key);
104         Json *getJson(const char* key);
105 private:
106         typedef struct _JsonNode json_node_t;
107         json_node_t *__jsonNode;
108
109         void parse(const char* s);
110         void release();
111
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);
118 };
119
120
121 #endif // __RRS_JSON_H__
122