1 /*
2   Copyright (c) 2009 Dave Gamble
3 
4   Permission is hereby granted, free of charge, to any person obtaining a copy
5   of this software and associated documentation files (the "Software"), to deal
6   in the Software without restriction, including without limitation the rights
7   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8   copies of the Software, and to permit persons to whom the Software is
9   furnished to do so, subject to the following conditions:
10 
11   The above copyright notice and this permission notice shall be included in
12   all copies or substantial portions of the Software.
13 
14   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   THE SOFTWARE.
21 */
22 #include "switch.h"
23 #ifndef SWITCH_JSON__h
24 #define SWITCH_JSON__h
25 
26 #ifdef FREESWITCHCORE_EXPORTS
27 #ifndef CJSON_EXPORT_SYMBOLS
28 #define CJSON_EXPORT_SYMBOLS 1
29 #endif
30 #endif
31 
32 #ifdef SWITCH_API_VISIBILITY
33 #ifndef CJSON_API_VISIBILITY
34 #define CJSON_API_VISIBILITY 1
35 #endif
36 #endif
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
43 #include "switch_cJSON.h"
44 #include "switch_cJSON_Utils.h"
45 
46 SWITCH_DECLARE(cJSON *) cJSON_CreateStringPrintf(const char *fmt, ...);
47 SWITCH_DECLARE(const char *)cJSON_GetObjectCstr(const cJSON *object, const char *string);
48 
json_add_child_obj(cJSON * json,const char * name,cJSON * obj)49 static inline cJSON *json_add_child_obj(cJSON *json, const char *name, cJSON *obj)
50 {
51 	cJSON *new_json = NULL;
52 
53 	switch_assert(json);
54 
55 	if (obj) {
56 		new_json = obj;
57 	} else {
58 		new_json = cJSON_CreateObject();
59 	}
60 
61 	switch_assert(new_json);
62 
63 	cJSON_AddItemToObject(json, name, new_json);
64 
65 	return new_json;
66 }
67 
json_add_child_array(cJSON * json,const char * name)68 static inline cJSON *json_add_child_array(cJSON *json, const char *name)
69 {
70 	cJSON *new_json = NULL;
71 
72 	switch_assert(json);
73 
74 	new_json = cJSON_CreateArray();
75 	switch_assert(new_json);
76 
77 	cJSON_AddItemToObject(json, name, new_json);
78 
79 	return new_json;
80 }
81 
json_add_child_string(cJSON * json,const char * name,const char * val)82 static inline cJSON *json_add_child_string(cJSON *json, const char *name, const char *val)
83 {
84 	cJSON *new_json = NULL;
85 
86 	switch_assert(json);
87 
88 	new_json = cJSON_CreateString(val);
89 	switch_assert(new_json);
90 
91 	cJSON_AddItemToObject(json, name, new_json);
92 
93 	return new_json;
94 }
95 
cJSON_isTrue(cJSON * json)96 static inline int cJSON_isTrue(cJSON *json)
97 {
98 	if (!json) return 0;
99 
100 	if (json->type == cJSON_True) return 1;
101 
102 	if (json->type == cJSON_String && (
103 		!strcasecmp(json->valuestring, "yes") ||
104 		!strcasecmp(json->valuestring, "on") ||
105 		!strcasecmp(json->valuestring, "true") ||
106 		!strcasecmp(json->valuestring, "t") ||
107 		!strcasecmp(json->valuestring, "enabled") ||
108 		!strcasecmp(json->valuestring, "active") ||
109 		!strcasecmp(json->valuestring, "allow") ||
110 		atoi(json->valuestring))) {
111 		return 1;
112 	}
113 
114 	if (json->type == cJSON_Number && (json->valueint || json->valuedouble)) {
115 		return 1;
116 	}
117 
118 	return 0;
119 }
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 #endif
126