ultrajson.h
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011-2012, ESN Social Software AB and Jonas Tarnstrom
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the ESN Social Software AB nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB OR JONAS TARNSTROM BE LIABLE 
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Portions of code from:
00028 MODP_ASCII - Ascii transformations (upper/lower, etc)
00029 http://code.google.com/p/stringencoders/
00030 Copyright (c) 2007  Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved.
00031 
00032 */
00033 
00034 /*
00035 Ultra fast JSON encoder and decoder
00036 Developed by Jonas Tarnstrom (jonas@esn.me).
00037 
00038 Encoder notes:
00039 ------------------
00040 
00041 :: Cyclic references ::
00042 Cyclic referenced objects are not detected. 
00043 Set JSONObjectEncoder.recursionMax to suitable value or make sure input object 
00044 tree doesn't have cyclic references.
00045 
00046 */
00047 
00048 #ifndef __ULTRAJSON_H__
00049 #define __ULTRAJSON_H__
00050 
00051 #include <stdio.h>
00052 #include <wchar.h>
00053 
00054 //#define JSON_DECODE_NUMERIC_AS_DOUBLE
00055 
00056 // Don't output any extra whitespaces when encoding
00057 #define JSON_NO_EXTRA_WHITESPACE
00058 
00059 // Max decimals to encode double floating point numbers with
00060 #ifndef JSON_DOUBLE_MAX_DECIMALS
00061 #define JSON_DOUBLE_MAX_DECIMALS 15
00062 #endif
00063 
00064 // Max recursion depth, default for encoder
00065 #ifndef JSON_MAX_RECURSION_DEPTH
00066 #define JSON_MAX_RECURSION_DEPTH 1024
00067 #endif
00068 
00069 /*
00070 Dictates and limits how much stack space for buffers UltraJSON will use before resorting to provided heap functions */
00071 #ifndef JSON_MAX_STACK_BUFFER_SIZE
00072 #define JSON_MAX_STACK_BUFFER_SIZE 131072
00073 #endif
00074 
00075 #ifdef _WIN32
00076 
00077 typedef __int64 JSINT64;
00078 typedef unsigned __int64 JSUINT64;
00079 
00080 typedef __int32 JSINT32;
00081 typedef unsigned __int32 JSUINT32;
00082 typedef unsigned __int8 JSUINT8;
00083 typedef unsigned __int16 JSUTF16;
00084 typedef unsigned __int32 JSUTF32;
00085 typedef __int64 JSLONG;
00086 
00087 #define EXPORTFUNCTION __declspec(dllexport)
00088 
00089 #define FASTCALL_MSVC __fastcall
00090 #define FASTCALL_ATTR 
00091 #define INLINE_PREFIX __inline
00092 
00093 #else
00094 
00095 #include <sys/types.h>
00096 typedef int64_t JSINT64;
00097 typedef u_int64_t JSUINT64;
00098 
00099 typedef int32_t JSINT32;
00100 typedef u_int32_t JSUINT32;
00101 
00102 #define FASTCALL_MSVC 
00103 #define FASTCALL_ATTR __attribute__((fastcall))
00104 #define INLINE_PREFIX inline
00105 
00106 typedef u_int8_t JSUINT8;
00107 typedef u_int16_t JSUTF16;
00108 typedef u_int32_t JSUTF32;
00109 
00110 typedef int64_t JSLONG;
00111 
00112 #define EXPORTFUNCTION
00113 #endif
00114 
00115 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
00116 #define __LITTLE_ENDIAN__
00117 #else
00118 
00119 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
00120 #define __BIG_ENDIAN__
00121 #endif
00122 
00123 #endif
00124 
00125 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
00126 #error "Endianess not supported"
00127 #endif
00128 
00129 enum JSTYPES
00130 {
00131     JT_NULL,        // NULL
00132     JT_TRUE,        //boolean true
00133     JT_FALSE,       //boolean false
00134     JT_INT,         //(JSINT32 (signed 32-bit))
00135     JT_LONG,        //(JSINT64 (signed 64-bit))
00136     JT_DOUBLE,  //(double)
00137     JT_UTF8,        //(char 8-bit)
00138     JT_ARRAY,       // Array structure
00139     JT_OBJECT,  // Key/Value structure 
00140     JT_INVALID, // Internal, do not return nor expect
00141 };
00142 
00143 typedef void * JSOBJ;
00144 typedef void * JSITER;
00145 
00146 typedef struct __JSONTypeContext
00147 {
00148     int type;
00149     void *prv;
00150 } JSONTypeContext;
00151 
00152 /*
00153 Function pointer declarations, suitable for implementing UltraJSON */
00154 typedef void (*JSPFN_ITERBEGIN)(JSOBJ obj, JSONTypeContext *tc);
00155 typedef int (*JSPFN_ITERNEXT)(JSOBJ obj, JSONTypeContext *tc);
00156 typedef void (*JSPFN_ITEREND)(JSOBJ obj, JSONTypeContext *tc);
00157 typedef JSOBJ (*JSPFN_ITERGETVALUE)(JSOBJ obj, JSONTypeContext *tc);
00158 typedef char *(*JSPFN_ITERGETNAME)(JSOBJ obj, JSONTypeContext *tc, size_t *outLen);
00159 typedef void *(*JSPFN_MALLOC)(size_t size);
00160 typedef void (*JSPFN_FREE)(void *pptr);
00161 typedef void *(*JSPFN_REALLOC)(void *base, size_t size);
00162 
00163 typedef struct __JSONObjectEncoder
00164 {
00165     void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc);
00166     void (*endTypeContext)(JSOBJ obj, JSONTypeContext *tc);
00167     const char *(*getStringValue)(JSOBJ obj, JSONTypeContext *tc, size_t *_outLen);
00168     JSINT64 (*getLongValue)(JSOBJ obj, JSONTypeContext *tc);
00169     JSINT32 (*getIntValue)(JSOBJ obj, JSONTypeContext *tc);
00170     double (*getDoubleValue)(JSOBJ obj, JSONTypeContext *tc);
00171 
00172     /*
00173     Begin iteration of an iteratable object (JS_ARRAY or JS_OBJECT) 
00174     Implementor should setup iteration state in ti->prv 
00175     */
00176     JSPFN_ITERBEGIN iterBegin;
00177 
00178     /*
00179     Retrieve next object in an iteration. Should return 0 to indicate iteration has reached end or 1 if there are more items.
00180     Implementor is responsible for keeping state of the iteration. Use ti->prv fields for this
00181     */
00182     JSPFN_ITERNEXT iterNext;
00183 
00184     /*
00185     Ends the iteration of an iteratable object.
00186     Any iteration state stored in ti->prv can be freed here
00187     */
00188     JSPFN_ITEREND iterEnd;
00189 
00190     /*
00191     Returns a reference to the value object of an iterator
00192     The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
00193     */
00194     JSPFN_ITERGETVALUE iterGetValue;
00195     
00196     /*
00197     Return name of iterator. 
00198     The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
00199     */
00200     JSPFN_ITERGETNAME iterGetName;
00201     
00202     /*
00203     Release a value as indicated by setting ti->release = 1 in the previous getValue call.
00204     The ti->prv array should contain the necessary context to release the value
00205     */
00206     void (*releaseObject)(JSOBJ obj);
00207 
00208     /* Library functions 
00209     Set to NULL to use STDLIB malloc,realloc,free */
00210     JSPFN_MALLOC malloc;
00211     JSPFN_REALLOC realloc;
00212     JSPFN_FREE free;
00213 
00214     /*
00215     Configuration for max recursion, set to 0 to use default (see JSON_MAX_RECURSION_DEPTH)*/
00216     int recursionMax;
00217 
00218     /*
00219     Configuration for max decimals of double floating poiunt numbers to encode (0-9) */
00220     int doublePrecision;
00221 
00222     /*
00223     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 */
00224     int forceASCII;
00225 
00226 
00227     /*
00228     Set to an error message if error occured */
00229     const char *errorMsg;
00230     JSOBJ errorObj;
00231 
00232     /* Buffer stuff */
00233     char *start;
00234     char *offset;
00235     char *end;
00236     int heap;
00237     int level;
00238 
00239 } JSONObjectEncoder;
00240 
00241 
00242 /*
00243 Encode an object structure into JSON.
00244 
00245 Arguments:
00246 obj - An anonymous type representing the object
00247 enc - Function definitions for querying JSOBJ type
00248 buffer - Preallocated buffer to store result in. If NULL function allocates own buffer
00249 cbBuffer - Length of buffer (ignored if buffer is NULL)
00250 
00251 Returns:
00252 Encoded JSON object as a null terminated char string. 
00253 
00254 NOTE:
00255 If the supplied buffer wasn't enough to hold the result the function will allocate a new buffer. 
00256 Life cycle of the provided buffer must still be handled by caller. 
00257 
00258 If the return value doesn't equal the specified buffer caller must release the memory using
00259 JSONObjectEncoder.free or free() as specified when calling this function.
00260 */
00261 EXPORTFUNCTION char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *buffer, size_t cbBuffer);
00262 
00263 
00264 
00265 typedef struct __JSONObjectDecoder
00266 {
00267     JSOBJ (*newString)(wchar_t *start, wchar_t *end);
00268     void (*objectAddKey)(JSOBJ obj, JSOBJ name, JSOBJ value);
00269     void (*arrayAddItem)(JSOBJ obj, JSOBJ value);
00270     JSOBJ (*newTrue)(void);
00271     JSOBJ (*newFalse)(void);
00272     JSOBJ (*newNull)(void);
00273     JSOBJ (*newObject)(void);
00274     JSOBJ (*newArray)(void);
00275     JSOBJ (*newInt)(JSINT32 value);
00276     JSOBJ (*newLong)(JSINT64 value);
00277     JSOBJ (*newDouble)(double value);
00278     void (*releaseObject)(JSOBJ obj);
00279     JSPFN_MALLOC malloc;
00280     JSPFN_FREE free;
00281     JSPFN_REALLOC realloc;
00282 
00283     char *errorStr;
00284     char *errorOffset;
00285 
00286 
00287 
00288 } JSONObjectDecoder;
00289 
00290 EXPORTFUNCTION JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuffer);
00291 
00292 #endif


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jan 2 2014 11:53:35