libjson.h 13 KB

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