tizen 2.4 release
[framework/context/context-common.git] / include / json.h
1 /*
2  * Copyright (c) 2015 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 __CONTEXT_JSON_H__
18 #define __CONTEXT_JSON_H__
19
20 #include <sys/types.h>
21 #include <glib.h>
22 #include <string>
23 #include <list>
24 #include <types_internal.h>
25
26 #define _J(cmt, jobj) \
27 do { \
28         _SD("%s: %s", (cmt), jobj.str().c_str()); \
29 } while(0)
30
31 #define EMPTY_JSON_OBJECT       "{}"
32 #define DEFAULT_PRECISION       3
33
34 namespace ctx {
35
36         class json {
37         private:
38                 typedef struct _JsonNode json_node_t;
39                 json_node_t *json_node;
40
41                 void parse(const char* s);
42                 void release();
43
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);
50
51         public:
52                 json();
53                 json(const char* s);
54                 json(const std::string& s);
55
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());
60                  */
61                 json(const json& j);
62
63                 ~json();
64
65                 json& operator=(const char* s);
66                 json& operator=(const std::string& s);
67
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();
72                  */
73                 json& operator=(const json& j);
74
75                 bool operator==(const json& rhs);
76                 bool operator!=(const json& rhs);
77
78                 char* dup_cstr();
79                 std::string str();
80
81                 bool get_keys(std::list<std::string>* list);
82
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);
89
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);
96
97                 int array_get_size(const char* path, const char* key);
98
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);
104
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);
110
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);
116         };
117
118 }       /* namespace ctx */
119
120 #endif // __CONTEXT_JSON_H__
121