ultrajson.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. Copyright (c) 2011, Jonas Tarnstrom and ESN Social Software AB
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. All advertising materials mentioning features or use of this software
  12. must display the following acknowledgement:
  13. This product includes software developed by ESN Social Software AB (www.esn.me).
  14. 4. Neither the name of the ESN Social Software AB nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY ESN SOCIAL SOFTWARE AB ''AS IS'' AND ANY
  18. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. Portions of code from:
  28. MODP_ASCII - Ascii transformations (upper/lower, etc)
  29. http://code.google.com/p/stringencoders/
  30. Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved.
  31. */
  32. /*
  33. Ultra fast JSON encoder and decoder
  34. Developed by Jonas Tarnstrom ([email protected]).
  35. Encoder notes:
  36. ------------------
  37. :: Cyclic references ::
  38. Cyclic referenced objects are not detected.
  39. Set JSONObjectEncoder.recursionMax to suitable value or make sure input object
  40. tree doesn't have cyclic references.
  41. */
  42. #ifndef __ULTRAJSON_H__
  43. #define __ULTRAJSON_H__
  44. #include <stdio.h>
  45. #include <wchar.h>
  46. //#define JSON_DECODE_NUMERIC_AS_DOUBLE
  47. // Don't output any extra whitespaces when encoding
  48. #define JSON_NO_EXTRA_WHITESPACE
  49. // Max decimals to encode double floating point numbers with
  50. #ifndef JSON_DOUBLE_MAX_DECIMALS
  51. #define JSON_DOUBLE_MAX_DECIMALS 9
  52. #endif
  53. // Max recursion depth, default for encoder
  54. #ifndef JSON_MAX_RECURSION_DEPTH
  55. #define JSON_MAX_RECURSION_DEPTH 256
  56. #endif
  57. /*
  58. Dictates and limits how much stack space for buffers UltraJSON will use before resorting to provided heap functions */
  59. #ifndef JSON_MAX_STACK_BUFFER_SIZE
  60. #define JSON_MAX_STACK_BUFFER_SIZE 131072
  61. #endif
  62. #ifdef _WIN32
  63. typedef __int64 JSINT64;
  64. typedef unsigned __int64 JSUINT64;
  65. typedef unsigned __int32 uint32_t;
  66. typedef __int32 JSINT32;
  67. typedef uint32_t JSUINT32;
  68. typedef unsigned __int8 JSUINT8;
  69. typedef unsigned __int16 JSUTF16;
  70. typedef unsigned __int32 JSUTF32;
  71. typedef __int64 JSLONG;
  72. #define EXPORTFUNCTION __declspec(dllexport)
  73. #define FASTCALL_MSVC __fastcall
  74. #define FASTCALL_ATTR
  75. #define INLINE_PREFIX __inline
  76. #else
  77. #include <sys/types.h>
  78. typedef int64_t JSINT64;
  79. typedef u_int64_t JSUINT64;
  80. typedef int32_t JSINT32;
  81. typedef u_int32_t JSUINT32;
  82. #define FASTCALL_MSVC
  83. #define FASTCALL_ATTR __attribute__((fastcall))
  84. #define INLINE_PREFIX inline
  85. typedef u_int32_t uint32_t;
  86. typedef u_int8_t JSUINT8;
  87. typedef u_int16_t JSUTF16;
  88. typedef u_int32_t JSUTF32;
  89. typedef int64_t JSLONG;
  90. #define EXPORTFUNCTION
  91. #endif
  92. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  93. #define __LITTLE_ENDIAN__
  94. #else
  95. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  96. #define __BIG_ENDIAN__
  97. #endif
  98. #endif
  99. #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
  100. #error "Endianess not supported"
  101. #endif
  102. enum JSTYPES
  103. {
  104. JT_NULL, // NULL
  105. JT_TRUE, //boolean true
  106. JT_FALSE, //boolean false
  107. JT_INT, //(JSINT32 (signed 32-bit))
  108. JT_LONG, //(JSINT64 (signed 64-bit))
  109. JT_DOUBLE, //(double)
  110. JT_UTF8, //(char 8-bit)
  111. JT_ARRAY, // Array structure
  112. JT_OBJECT, // Key/Value structure
  113. JT_INVALID, // Internal, do not return nor expect
  114. };
  115. typedef void * JSOBJ;
  116. typedef void * JSITER;
  117. typedef struct __JSONTypeContext
  118. {
  119. int type;
  120. void *prv[32];
  121. } JSONTypeContext;
  122. /*
  123. Function pointer declarations, suitable for implementing UltraJSON */
  124. typedef void (*JSPFN_ITERBEGIN)(JSOBJ obj, JSONTypeContext *tc);
  125. typedef int (*JSPFN_ITERNEXT)(JSOBJ obj, JSONTypeContext *tc);
  126. typedef void (*JSPFN_ITEREND)(JSOBJ obj, JSONTypeContext *tc);
  127. typedef JSOBJ (*JSPFN_ITERGETVALUE)(JSOBJ obj, JSONTypeContext *tc);
  128. typedef char *(*JSPFN_ITERGETNAME)(JSOBJ obj, JSONTypeContext *tc, size_t *outLen);
  129. typedef void *(*JSPFN_MALLOC)(size_t size);
  130. typedef void (*JSPFN_FREE)(void *pptr);
  131. typedef void *(*JSPFN_REALLOC)(void *base, size_t size);
  132. typedef struct __JSONObjectEncoder
  133. {
  134. void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc);
  135. void (*endTypeContext)(JSOBJ obj, JSONTypeContext *tc);
  136. const char *(*getStringValue)(JSOBJ obj, JSONTypeContext *tc, size_t *_outLen);
  137. JSINT64 (*getLongValue)(JSOBJ obj, JSONTypeContext *tc);
  138. JSINT32 (*getIntValue)(JSOBJ obj, JSONTypeContext *tc);
  139. double (*getDoubleValue)(JSOBJ obj, JSONTypeContext *tc);
  140. /*
  141. Begin iteration of an iteratable object (JS_ARRAY or JS_OBJECT)
  142. Implementor should setup iteration state in ti->prv
  143. */
  144. JSPFN_ITERBEGIN iterBegin;
  145. /*
  146. Retrieve next object in an iteration. Should return 0 to indicate iteration has reached end or 1 if there are more items.
  147. Implementor is responsible for keeping state of the iteration. Use ti->prv fields for this
  148. */
  149. JSPFN_ITERNEXT iterNext;
  150. /*
  151. Ends the iteration of an iteratable object.
  152. Any iteration state stored in ti->prv can be freed here
  153. */
  154. JSPFN_ITEREND iterEnd;
  155. /*
  156. Returns a reference to the value object of an iterator
  157. The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
  158. */
  159. JSPFN_ITERGETVALUE iterGetValue;
  160. /*
  161. Return name of iterator.
  162. The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
  163. */
  164. JSPFN_ITERGETNAME iterGetName;
  165. /*
  166. Release a value as indicated by setting ti->release = 1 in the previous getValue call.
  167. The ti->prv array should contain the necessary context to release the value
  168. */
  169. void (*releaseObject)(JSOBJ obj);
  170. /* Library functions
  171. Set to NULL to use STDLIB malloc,realloc,free */
  172. JSPFN_MALLOC malloc;
  173. JSPFN_REALLOC realloc;
  174. JSPFN_FREE free;
  175. /*
  176. Configuration for max recursion, set to 0 to use default (see JSON_MAX_RECURSION_DEPTH)*/
  177. int recursionMax;
  178. /*
  179. Configuration for max decimals of double floating poiunt numbers to encode (0-9) */
  180. int doublePrecision;
  181. /*
  182. If true output will be ASCII with all characters above 127 encoded as \uXXXX. If false output will be UTF-8 or what ever charset strings are brought as */
  183. int forceASCII;
  184. /*
  185. Set to an error message if error occured */
  186. const char *errorMsg;
  187. JSOBJ errorObj;
  188. /* Buffer stuff */
  189. char *start;
  190. char *offset;
  191. char *end;
  192. int heap;
  193. int level;
  194. } JSONObjectEncoder;
  195. /*
  196. Encode an object structure into JSON.
  197. Arguments:
  198. obj - An anonymous type representing the object
  199. enc - Function definitions for querying JSOBJ type
  200. buffer - Preallocated buffer to store result in. If NULL function allocates own buffer
  201. cbBuffer - Length of buffer (ignored if buffer is NULL)
  202. Returns:
  203. Encoded JSON object as a null terminated char string.
  204. NOTE:
  205. If the supplied buffer wasn't enough to hold the result the function will allocate a new buffer.
  206. Life cycle of the provided buffer must still be handled by caller.
  207. If the return value doesn't equal the specified buffer caller must release the memory using
  208. JSONObjectEncoder.free or free() as specified when calling this function.
  209. */
  210. EXPORTFUNCTION char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *buffer, size_t cbBuffer);
  211. typedef struct __JSONObjectDecoder
  212. {
  213. JSOBJ (*newString)(wchar_t *start, wchar_t *end);
  214. void (*objectAddKey)(JSOBJ obj, JSOBJ name, JSOBJ value);
  215. void (*arrayAddItem)(JSOBJ obj, JSOBJ value);
  216. JSOBJ (*newTrue)(void);
  217. JSOBJ (*newFalse)(void);
  218. JSOBJ (*newNull)(void);
  219. JSOBJ (*newObject)(void);
  220. JSOBJ (*newArray)(void);
  221. JSOBJ (*newInt)(JSINT32 value);
  222. JSOBJ (*newLong)(JSINT64 value);
  223. JSOBJ (*newDouble)(double value);
  224. void (*releaseObject)(JSOBJ obj);
  225. JSPFN_MALLOC malloc;
  226. JSPFN_FREE free;
  227. JSPFN_REALLOC realloc;
  228. char *errorStr;
  229. char *errorOffset;
  230. } JSONObjectDecoder;
  231. EXPORTFUNCTION JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuffer);
  232. #endif