4 #include "_internal/Source/JSONDefs.h"
13 #ifdef JSON_LIBRARY //compiling the library, hide the interface
18 #ifdef JSON_NO_C_CONSTS
21 #define TOCONST_CSTR(x) const_cast<const json_char *>(x)
23 #define json_const const
24 #define TOCONST_CSTR(x) x
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);
36 #ifdef JSON_READ_PRIORITY
37 JSONNODE * json_parse(json_const json_char * json);
38 JSONNODE * json_parse_unformatted(json_const json_char * json);
40 json_char * json_strip_white_space(json_const json_char * json);
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");
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);
48 #if defined JSON_DEBUG && !defined JSON_STDERROR
50 void json_register_debug_callback(json_error_callback_t callback);
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);
56 void json_register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock,
void * manager_lock);
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);
63 #ifdef JSON_MEMORY_CALLBACKS
64 void json_register_memory_callbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre);
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);
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);
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);
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);
100 json_char * json_get_comment(json_const JSONNODE * node);
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);
107 JSONNODE * json_as_node(json_const JSONNODE * node);
108 JSONNODE * json_as_array(json_const JSONNODE * node);
111 void * json_as_binary(json_const JSONNODE * node,
unsigned long * size);
113 #ifdef JSON_WRITE_PRIORITY
114 json_char * json_write(json_const JSONNODE * node);
115 json_char * json_write_formatted(json_const JSONNODE * node);
119 void json_set_name(JSONNODE * node, json_const json_char * name);
121 void json_set_comment(JSONNODE * node, json_const json_char * comment);
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);
131 void json_set_binary(JSONNODE * node, json_const
void * data,
unsigned long length);
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);
138 void json_cast(JSONNODE * node,
char type);
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);
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);
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);
163 JSONNODE_ITERATOR json_begin(JSONNODE * node);
164 JSONNODE_ITERATOR json_end(JSONNODE * node);
168 json_bool_t json_equal(JSONNODE * node, JSONNODE * node2);
175 #error Turning off JSON_LIBRARY requires C++
177 #include "_internal/Source/JSONNode.h"
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"
185 #ifndef JSON_NO_EXCEPTIONS
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);
198 inline static std::string decode64(
const json_string & encoded) json_nothrow {
199 return JSONBase64::json_decode64(encoded);
204 inline static json_string strip_white_space(
const json_string & json) json_nothrow {
205 return JSONWorker::RemoveWhiteSpaceAndComments(json,
false);
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());
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());
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());
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());
240 #ifdef JSON_READ_PRIORITY
242 inline static JSONNode parse(
const json_string & json) json_throws(std::invalid_argument) {
245 json_auto<json_char> buffer(JSONWorker::RemoveWhiteSpace(json, len,
false));
246 return JSONPreparse::isValidRoot(buffer.ptr);
248 return JSONWorker::parse(json);
252 inline static JSONNode parse_unformatted(
const json_string & json) json_throws(std::invalid_argument) {
254 return JSONPreparse::isValidRoot(json);
256 return JSONWorker::parse_unformatted(json);
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"));
268 json_auto<json_char> s;
269 s.set(JSONWorker::RemoveWhiteSpaceAndCommentsC(json,
false));
270 return JSONValidator::isValidRoot(s.ptr);
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"));
280 return JSONValidator::isValidRoot(json.c_str());
282 #ifdef JSON_DEPRECATED_FUNCTIONS
283 #ifdef JSON_NO_EXCEPTIONS
284 #error, JSON_DEPRECATED_FUNCTIONS requires JSON_NO_EXCEPTIONS be off
287 inline static JSONNode json_deprecated(validate(
const json_string & json),
"libjson::validate is deprecated, use libjson::is_valid and libjson::parse instead");
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);
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);
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);
311 inline static void set_global_mutex(
void * mutex) json_nothrow {
312 JSONNode::set_global_mutex(mutex);
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);
324 #ifdef JSON_DEPRECATED_FUNCTIONS
326 inline static JSONNode libjson::validate(
const json_string & json) {
327 if (json_likely(is_valid(json))){
330 throw std::invalid_argument(
"");
334 #endif //JSON_LIBRARY