Jamoma API  0.6.0.a19
libjson.h
1 #ifndef LIBJSON_H
2 #define LIBJSON_H
3 
4 #include "_internal/Source/JSONDefs.h" //for typedefs of functions, strings, and nodes
5 
6 /*
7  This is the C interface to libjson.
8 
9  This file also declares various things that are needed for
10  C++ programming
11 */
12 
13 #ifdef JSON_LIBRARY //compiling the library, hide the interface
14  #ifdef __cplusplus
15  extern "C" {
16  #endif
17 
18  #ifdef JSON_NO_C_CONSTS
19  /* The interface has no consts in it, but ther must be const_cast internally */
20  #define json_const
21  #define TOCONST_CSTR(x) const_cast<const json_char *>(x)
22  #else
23  #define json_const const
24  #define TOCONST_CSTR(x) x
25  #endif
26 
27  /*
28  stuff that's in namespace libjson
29  */
30  void json_free(void * str);
31  void json_delete(JSONNODE * node);
32  #ifdef JSON_MEMORY_MANAGE
33  void json_free_all(void);
34  void json_delete_all(void);
35  #endif
36  #ifdef JSON_READ_PRIORITY
37  JSONNODE * json_parse(json_const json_char * json);
38  JSONNODE * json_parse_unformatted(json_const json_char * json);
39  #endif
40  json_char * json_strip_white_space(json_const json_char * json);
41  #ifdef JSON_VALIDATE
42  #ifdef JSON_DEPRECATED_FUNCTIONS
43  JSONNODE * json_deprecated(json_validate(json_const json_char * json), "json_validate is deprecated, use json_is_valid and json_parse instead");
44  #endif
45  json_bool_t json_is_valid(json_const json_char * json);
46  json_bool_t json_is_valid_unformatted(json_const json_char * json);
47  #endif
48  #if defined JSON_DEBUG && !defined JSON_STDERROR
49  /* When libjson errors, a callback allows the user to know what went wrong */
50  void json_register_debug_callback(json_error_callback_t callback);
51  #endif
52  #ifdef JSON_MUTEX_CALLBACKS
53  #ifdef JSON_MUTEX_MANAGE
54  void json_register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, json_mutex_callback_t destroy, void * manager_lock);
55  #else
56  void json_register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, void * manager_lock);
57  #endif
58  void json_set_global_mutex(void * mutex);
59  void json_set_mutex(JSONNODE * node, void * mutex);
60  void json_lock(JSONNODE * node, int threadid);
61  void json_unlock(JSONNODE * node, int threadid);
62  #endif
63  #ifdef JSON_MEMORY_CALLBACKS
64  void json_register_memory_callbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre);
65  #endif
66 
67  #ifdef JSON_STREAM
68  JSONSTREAM * json_new_stream(json_stream_callback_t callback, json_stream_e_callback_t e_callback, void * identifier);
69  void json_stream_push(JSONSTREAM * stream, json_const json_char * addendum);
70  void json_delete_stream(JSONSTREAM * stream);
71  void json_stream_reset(JSONSTREAM * stream);
72  #endif
73 
74 
75  /*
76  stuff that's in class JSONNode
77  */
78  /* ctors */
79  JSONNODE * json_new_a(json_const json_char * name, json_const json_char * value);
80  JSONNODE * json_new_i(json_const json_char * name, json_int_t value);
81  JSONNODE * json_new_f(json_const json_char * name, json_number value);
82  JSONNODE * json_new_b(json_const json_char * name, json_bool_t value);
83  JSONNODE * json_new(char type);
84  JSONNODE * json_copy(json_const JSONNODE * orig);
85  JSONNODE * json_duplicate(json_const JSONNODE * orig);
86 
87  /* assignment */
88  void json_set_a(JSONNODE * node, json_const json_char * value);
89  void json_set_i(JSONNODE * node, json_int_t value);
90  void json_set_f(JSONNODE * node, json_number value);
91  void json_set_b(JSONNODE * node, json_bool_t value);
92  void json_set_n(JSONNODE * node, json_const JSONNODE * orig);
93 
94  /* inspectors */
95  char json_type(json_const JSONNODE * node);
96  json_index_t json_size(json_const JSONNODE * node);
97  json_bool_t json_empty(json_const JSONNODE * node);
98  json_char * json_name(json_const JSONNODE * node);
99  #ifdef JSON_COMMENTS
100  json_char * json_get_comment(json_const JSONNODE * node);
101  #endif
102  json_char * json_as_string(json_const JSONNODE * node);
103  json_int_t json_as_int(json_const JSONNODE * node);
104  json_number json_as_float(json_const JSONNODE * node);
105  json_bool_t json_as_bool(json_const JSONNODE * node);
106  #ifdef JSON_CASTABLE
107  JSONNODE * json_as_node(json_const JSONNODE * node);
108  JSONNODE * json_as_array(json_const JSONNODE * node);
109  #endif
110  #ifdef JSON_BINARY
111  void * json_as_binary(json_const JSONNODE * node, unsigned long * size);
112  #endif
113  #ifdef JSON_WRITE_PRIORITY
114  json_char * json_write(json_const JSONNODE * node);
115  json_char * json_write_formatted(json_const JSONNODE * node);
116  #endif
117 
118  /* modifiers */
119  void json_set_name(JSONNODE * node, json_const json_char * name);
120  #ifdef JSON_COMMENTS
121  void json_set_comment(JSONNODE * node, json_const json_char * comment);
122  #endif
123  void json_clear(JSONNODE * node);
124  void json_nullify(JSONNODE * node);
125  void json_swap(JSONNODE * node, JSONNODE * node2);
126  void json_merge(JSONNODE * node, JSONNODE * node2);
127  #if !defined (JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
128  void json_preparse(JSONNODE * node);
129  #endif
130  #ifdef JSON_BINARY
131  void json_set_binary(JSONNODE * node, json_const void * data, unsigned long length);
132  #endif
133  #ifdef JSON_EXPOSE_BASE64
134  json_char * json_encode64(json_const void * binary, json_index_t bytes);
135  void * json_decode64(json_const json_char * text, unsigned long * size);
136  #endif
137  #ifdef JSON_CASTABLE
138  void json_cast(JSONNODE * node, char type);
139  #endif
140 
141  /* children access */
142  void json_reserve(JSONNODE * node, json_index_t siz);
143  JSONNODE * json_at(JSONNODE * node, json_index_t pos);
144  JSONNODE * json_get(JSONNODE * node, json_const json_char * name);
145  #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
146  JSONNODE * json_get_nocase(JSONNODE * node, json_const json_char * name);
147  JSONNODE * json_pop_back_nocase(JSONNODE * node, json_const json_char * name);
148  #endif
149  void json_push_back(JSONNODE * node, JSONNODE * node2);
150  JSONNODE * json_pop_back_at(JSONNODE * node, json_index_t pos);
151  JSONNODE * json_pop_back(JSONNODE * node, json_const json_char * name);
152  #ifdef JSON_ITERATORS
153  JSONNODE_ITERATOR json_find(JSONNODE * node, json_const json_char * name);
154  #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
155  JSONNODE_ITERATOR json_find_nocase(JSONNODE * node, json_const json_char * name);
156  #endif
157  JSONNODE_ITERATOR json_erase(JSONNODE * node, JSONNODE_ITERATOR it);
158  JSONNODE_ITERATOR json_erase_multi(JSONNODE * node, JSONNODE_ITERATOR start, JSONNODE_ITERATOR end);
159  JSONNODE_ITERATOR json_insert(JSONNODE * node, JSONNODE_ITERATOR it, JSONNODE * node2);
160  JSONNODE_ITERATOR json_insert_multi(JSONNODE * node, JSONNODE_ITERATOR it, JSONNODE_ITERATOR start, JSONNODE_ITERATOR end);
161 
162  /* iterator functions */
163  JSONNODE_ITERATOR json_begin(JSONNODE * node);
164  JSONNODE_ITERATOR json_end(JSONNODE * node);
165  #endif
166 
167  /* comparison */
168  json_bool_t json_equal(JSONNODE * node, JSONNODE * node2);
169 
170  #ifdef __cplusplus
171  }
172  #endif
173 #else
174  #ifndef __cplusplus
175  #error Turning off JSON_LIBRARY requires C++
176  #endif
177  #include "_internal/Source/JSONNode.h" //not used in this file, but libjson.h should be the only file required to use it embedded
178  #include "_internal/Source/JSONWorker.h"
179  #include "_internal/Source/JSONValidator.h"
180  #include "_internal/Source/JSONStream.h"
181  #include "_internal/Source/JSONPreparse.h"
182  #ifdef JSON_EXPOSE_BASE64
183  #include "_internal/Source/JSON_Base64.h"
184  #endif
185  #ifndef JSON_NO_EXCEPTIONS
186  #include <stdexcept> //some methods throw exceptions
187  #endif
188 
189  #include <cwchar> /* need wide characters */
190  #include <string>
191 
192  namespace libjson {
193  #ifdef JSON_EXPOSE_BASE64
194  inline static json_string encode64(const unsigned char * binary, size_t bytes) json_nothrow {
195  return JSONBase64::json_encode64(binary, bytes);
196  }
197 
198  inline static std::string decode64(const json_string & encoded) json_nothrow {
199  return JSONBase64::json_decode64(encoded);
200  }
201  #endif
202 
203  //useful if you have json that you don't want to parse, just want to strip to cut down on space
204  inline static json_string strip_white_space(const json_string & json) json_nothrow {
205  return JSONWorker::RemoveWhiteSpaceAndComments(json, false);
206  }
207 
208  #ifndef JSON_STRING_HEADER
209  inline static std::string to_std_string(const json_string & str){
210  #if defined(JSON_UNICODE) ||defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
211  return std::string(str.begin(), str.end());
212  #else
213  return str;
214  #endif
215  }
216  inline static std::wstring to_std_wstring(const json_string & str){
217  #if (!defined(JSON_UNICODE)) || defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
218  return std::wstring(str.begin(), str.end());
219  #else
220  return str;
221  #endif
222  }
223 
224  inline static json_string to_json_string(const std::string & str){
225  #if defined(JSON_UNICODE) ||defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
226  return json_string(str.begin(), str.end());
227  #else
228  return str;
229  #endif
230  }
231  inline static json_string to_json_string(const std::wstring & str){
232  #if (!defined(JSON_UNICODE)) || defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
233  return json_string(str.begin(), str.end());
234  #else
235  return str;
236  #endif
237  }
238  #endif
239 
240  #ifdef JSON_READ_PRIORITY
241  //if json is invalid, it throws a std::invalid_argument exception
242  inline static JSONNode parse(const json_string & json) json_throws(std::invalid_argument) {
243  #ifdef JSON_PREPARSE
244  size_t len;
245  json_auto<json_char> buffer(JSONWorker::RemoveWhiteSpace(json, len, false));
246  return JSONPreparse::isValidRoot(buffer.ptr);
247  #else
248  return JSONWorker::parse(json);
249  #endif
250  }
251 
252  inline static JSONNode parse_unformatted(const json_string & json) json_throws(std::invalid_argument) {
253  #ifdef JSON_PREPARSE
254  return JSONPreparse::isValidRoot(json);
255  #else
256  return JSONWorker::parse_unformatted(json);
257  #endif
258  }
259 
260  #ifdef JSON_VALIDATE
261  inline static bool is_valid(const json_string & json) json_nothrow {
262  #ifdef JSON_SECURITY_MAX_STRING_LENGTH
263  if (json_unlikely(json.length() > JSON_SECURITY_MAX_STRING_LENGTH)){
264  JSON_FAIL(JSON_TEXT("Exceeding JSON_SECURITY_MAX_STRING_LENGTH"));
265  return false;
266  }
267  #endif
268  json_auto<json_char> s;
269  s.set(JSONWorker::RemoveWhiteSpaceAndCommentsC(json, false));
270  return JSONValidator::isValidRoot(s.ptr);
271  }
272 
273  inline static bool is_valid_unformatted(const json_string & json) json_nothrow {
274  #ifdef JSON_SECURITY_MAX_STRING_LENGTH
275  if (json_unlikely(json.length() > JSON_SECURITY_MAX_STRING_LENGTH)){
276  JSON_FAIL(JSON_TEXT("Exceeding JSON_SECURITY_MAX_STRING_LENGTH"));
277  return false;
278  }
279  #endif
280  return JSONValidator::isValidRoot(json.c_str());
281  }
282  #ifdef JSON_DEPRECATED_FUNCTIONS
283  #ifdef JSON_NO_EXCEPTIONS
284  #error, JSON_DEPRECATED_FUNCTIONS requires JSON_NO_EXCEPTIONS be off
285  #endif
286  //if json is invalid, it throws a std::invalid_argument exception (differs from parse because this checks the entire tree)
287  inline static JSONNode json_deprecated(validate(const json_string & json), "libjson::validate is deprecated, use libjson::is_valid and libjson::parse instead");
288  #endif
289  #endif
290  #endif
291 
292  //When libjson errors, a callback allows the user to know what went wrong
293  #if defined JSON_DEBUG && !defined JSON_STDERROR
294  inline static void register_debug_callback(json_error_callback_t callback) json_nothrow {
295  JSONDebug::register_callback(callback);
296  }
297  #endif
298 
299  #ifdef JSON_MUTEX_CALLBACKS
300  #ifdef JSON_MUTEX_MANAGE
301  inline static void register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, json_mutex_callback_t destroy, void * manager_lock) json_nothrow {
302  JSONNode::register_mutex_callbacks(lock, unlock, manager_lock);
303  JSONNode::register_mutex_destructor(destroy);
304  }
305  #else
306  inline static void register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, void * manager_lock) json_nothrow {
307  JSONNode::register_mutex_callbacks(lock, unlock, manager_lock);
308  }
309  #endif
310 
311  inline static void set_global_mutex(void * mutex) json_nothrow {
312  JSONNode::set_global_mutex(mutex);
313  }
314  #endif
315 
316  #ifdef JSON_MEMORY_CALLBACKS
317  inline static void register_memory_callbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre) json_nothrow {
318  JSONMemory::registerMemoryCallbacks(mal, real, fre);
319  }
320  #endif
321 
322  }
323  #ifdef JSON_VALIDATE
324  #ifdef JSON_DEPRECATED_FUNCTIONS
325  //if json is invalid, it throws a std::invalid_argument exception (differs from parse because this checks the entire tree)
326  inline static JSONNode libjson::validate(const json_string & json) {
327  if (json_likely(is_valid(json))){
328  return parse(json);
329  }
330  throw std::invalid_argument("");
331  }
332  #endif
333  #endif
334 #endif //JSON_LIBRARY
335 
336 #endif //LIBJSON_H