Skip to content

Commit 0319ade

Browse files
committed
Squashed 'libs/ujson4c/' content from commit 65d3e35
git-subtree-dir: libs/ujson4c git-subtree-split: 65d3e35864ee501a2b21fe5af8a45667d36a42be
0 parents  commit 0319ade

File tree

9 files changed

+6020
-0
lines changed

9 files changed

+6020
-0
lines changed

3rdparty/ultrajson.h

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
/*
2+
Copyright (c) 2011-2013, ESN Social Software AB and Jonas Tarnstrom
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* Neither the name of the ESN Social Software AB nor the
13+
names of its contributors may be used to endorse or promote products
14+
derived from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB OR JONAS TARNSTROM BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
28+
Portions of code from 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+
Numeric decoder derived from from TCL library
33+
http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms
34+
* Copyright (c) 1988-1993 The Regents of the University of California.
35+
* Copyright (c) 1994 Sun Microsystems, Inc.
36+
*/
37+
38+
/*
39+
Ultra fast JSON encoder and decoder
40+
Developed by Jonas Tarnstrom (jonas@esn.me).
41+
42+
Encoder notes:
43+
------------------
44+
45+
:: Cyclic references ::
46+
Cyclic referenced objects are not detected.
47+
Set JSONObjectEncoder.recursionMax to suitable value or make sure input object
48+
tree doesn't have cyclic references.
49+
50+
*/
51+
52+
#ifndef __ULTRAJSON_H__
53+
#define __ULTRAJSON_H__
54+
55+
#include <stdio.h>
56+
#include <wchar.h>
57+
58+
// Don't output any extra whitespaces when encoding
59+
#define JSON_NO_EXTRA_WHITESPACE
60+
61+
// Max decimals to encode double floating point numbers with
62+
#ifndef JSON_DOUBLE_MAX_DECIMALS
63+
#define JSON_DOUBLE_MAX_DECIMALS 15
64+
#endif
65+
66+
// Max recursion depth, default for encoder
67+
#ifndef JSON_MAX_RECURSION_DEPTH
68+
#define JSON_MAX_RECURSION_DEPTH 1024
69+
#endif
70+
71+
// Max recursion depth, default for decoder
72+
#ifndef JSON_MAX_OBJECT_DEPTH
73+
#define JSON_MAX_OBJECT_DEPTH 1024
74+
#endif
75+
76+
/*
77+
Dictates and limits how much stack space for buffers UltraJSON will use before resorting to provided heap functions */
78+
#ifndef JSON_MAX_STACK_BUFFER_SIZE
79+
#define JSON_MAX_STACK_BUFFER_SIZE 131072
80+
#endif
81+
82+
#ifdef _WIN32
83+
84+
typedef __int64 JSINT64;
85+
typedef unsigned __int64 JSUINT64;
86+
87+
typedef __int32 JSINT32;
88+
typedef unsigned __int32 JSUINT32;
89+
typedef unsigned __int8 JSUINT8;
90+
typedef unsigned __int16 JSUTF16;
91+
typedef unsigned __int32 JSUTF32;
92+
typedef __int64 JSLONG;
93+
94+
#define EXPORTFUNCTION __declspec(dllexport)
95+
96+
#define FASTCALL_MSVC __fastcall
97+
#define FASTCALL_ATTR
98+
#define INLINE_PREFIX __inline
99+
100+
#else
101+
102+
#include <stdint.h>
103+
typedef int64_t JSINT64;
104+
typedef uint64_t JSUINT64;
105+
106+
typedef int32_t JSINT32;
107+
typedef uint32_t JSUINT32;
108+
109+
#define FASTCALL_MSVC
110+
111+
#if !defined __x86_64__
112+
#define FASTCALL_ATTR __attribute__((fastcall))
113+
#else
114+
#define FASTCALL_ATTR
115+
#endif
116+
117+
#define INLINE_PREFIX inline
118+
119+
typedef uint8_t JSUINT8;
120+
typedef uint16_t JSUTF16;
121+
typedef uint32_t JSUTF32;
122+
123+
typedef int64_t JSLONG;
124+
125+
#define EXPORTFUNCTION
126+
#endif
127+
128+
#if !(defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__))
129+
130+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
131+
#define __LITTLE_ENDIAN__
132+
#else
133+
134+
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
135+
#define __BIG_ENDIAN__
136+
#endif
137+
138+
#endif
139+
140+
#endif
141+
142+
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
143+
#error "Endianess not supported"
144+
#endif
145+
146+
enum JSTYPES
147+
{
148+
JT_NULL, // NULL
149+
JT_TRUE, //boolean true
150+
JT_FALSE, //boolean false
151+
JT_INT, //(JSINT32 (signed 32-bit))
152+
JT_LONG, //(JSINT64 (signed 64-bit))
153+
JT_DOUBLE, //(double)
154+
JT_UTF8, //(char 8-bit)
155+
JT_ARRAY, // Array structure
156+
JT_OBJECT, // Key/Value structure
157+
JT_INVALID, // Internal, do not return nor expect
158+
};
159+
160+
typedef void * JSOBJ;
161+
typedef void * JSITER;
162+
163+
typedef struct __JSONTypeContext
164+
{
165+
int type;
166+
void *prv;
167+
} JSONTypeContext;
168+
169+
/*
170+
Function pointer declarations, suitable for implementing UltraJSON */
171+
typedef void (*JSPFN_ITERBEGIN)(JSOBJ obj, JSONTypeContext *tc);
172+
typedef int (*JSPFN_ITERNEXT)(JSOBJ obj, JSONTypeContext *tc);
173+
typedef void (*JSPFN_ITEREND)(JSOBJ obj, JSONTypeContext *tc);
174+
typedef JSOBJ (*JSPFN_ITERGETVALUE)(JSOBJ obj, JSONTypeContext *tc);
175+
typedef char *(*JSPFN_ITERGETNAME)(JSOBJ obj, JSONTypeContext *tc, size_t *outLen);
176+
typedef void *(*JSPFN_MALLOC)(size_t size);
177+
typedef void (*JSPFN_FREE)(void *pptr);
178+
typedef void *(*JSPFN_REALLOC)(void *base, size_t size);
179+
180+
typedef struct __JSONObjectEncoder
181+
{
182+
void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc);
183+
void (*endTypeContext)(JSOBJ obj, JSONTypeContext *tc);
184+
const char *(*getStringValue)(JSOBJ obj, JSONTypeContext *tc, size_t *_outLen);
185+
JSINT64 (*getLongValue)(JSOBJ obj, JSONTypeContext *tc);
186+
JSINT32 (*getIntValue)(JSOBJ obj, JSONTypeContext *tc);
187+
double (*getDoubleValue)(JSOBJ obj, JSONTypeContext *tc);
188+
189+
/*
190+
Begin iteration of an iteratable object (JS_ARRAY or JS_OBJECT)
191+
Implementor should setup iteration state in ti->prv
192+
*/
193+
JSPFN_ITERBEGIN iterBegin;
194+
195+
/*
196+
Retrieve next object in an iteration. Should return 0 to indicate iteration has reached end or 1 if there are more items.
197+
Implementor is responsible for keeping state of the iteration. Use ti->prv fields for this
198+
*/
199+
JSPFN_ITERNEXT iterNext;
200+
201+
/*
202+
Ends the iteration of an iteratable object.
203+
Any iteration state stored in ti->prv can be freed here
204+
*/
205+
JSPFN_ITEREND iterEnd;
206+
207+
/*
208+
Returns a reference to the value object of an iterator
209+
The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
210+
*/
211+
JSPFN_ITERGETVALUE iterGetValue;
212+
213+
/*
214+
Return name of iterator.
215+
The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
216+
*/
217+
JSPFN_ITERGETNAME iterGetName;
218+
219+
/*
220+
Release a value as indicated by setting ti->release = 1 in the previous getValue call.
221+
The ti->prv array should contain the necessary context to release the value
222+
*/
223+
void (*releaseObject)(JSOBJ obj);
224+
225+
/* Library functions
226+
Set to NULL to use STDLIB malloc,realloc,free */
227+
JSPFN_MALLOC malloc;
228+
JSPFN_REALLOC realloc;
229+
JSPFN_FREE free;
230+
231+
/*
232+
Configuration for max recursion, set to 0 to use default (see JSON_MAX_RECURSION_DEPTH)*/
233+
int recursionMax;
234+
235+
/*
236+
Configuration for max decimals of double floating poiunt numbers to encode (0-9) */
237+
int doublePrecision;
238+
239+
/*
240+
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 */
241+
int forceASCII;
242+
243+
/*
244+
If true, '<', '>', and '&' characters will be encoded as \u003c, \u003e, and \u0026, respectively. If false, no special encoding will be used. */
245+
int encodeHTMLChars;
246+
247+
/*
248+
Set to an error message if error occured */
249+
const char *errorMsg;
250+
JSOBJ errorObj;
251+
252+
/* Buffer stuff */
253+
char *start;
254+
char *offset;
255+
char *end;
256+
int heap;
257+
int level;
258+
259+
} JSONObjectEncoder;
260+
261+
262+
/*
263+
Encode an object structure into JSON.
264+
265+
Arguments:
266+
obj - An anonymous type representing the object
267+
enc - Function definitions for querying JSOBJ type
268+
buffer - Preallocated buffer to store result in. If NULL function allocates own buffer
269+
cbBuffer - Length of buffer (ignored if buffer is NULL)
270+
271+
Returns:
272+
Encoded JSON object as a null terminated char string.
273+
274+
NOTE:
275+
If the supplied buffer wasn't enough to hold the result the function will allocate a new buffer.
276+
Life cycle of the provided buffer must still be handled by caller.
277+
278+
If the return value doesn't equal the specified buffer caller must release the memory using
279+
JSONObjectEncoder.free or free() as specified when calling this function.
280+
*/
281+
EXPORTFUNCTION char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *buffer, size_t cbBuffer);
282+
283+
284+
285+
typedef struct __JSONObjectDecoder
286+
{
287+
JSOBJ (*newString)(void *prv, wchar_t *start, wchar_t *end);
288+
void (*objectAddKey)(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value);
289+
void (*arrayAddItem)(void *prv, JSOBJ obj, JSOBJ value);
290+
JSOBJ (*newTrue)(void *prv);
291+
JSOBJ (*newFalse)(void *prv);
292+
JSOBJ (*newNull)(void *prv);
293+
JSOBJ (*newObject)(void *prv);
294+
JSOBJ (*newArray)(void *prv);
295+
JSOBJ (*newInt)(void *prv, JSINT32 value);
296+
JSOBJ (*newLong)(void *prv, JSINT64 value);
297+
JSOBJ (*newDouble)(void *prv, double value);
298+
void (*releaseObject)(void *prv, JSOBJ obj);
299+
JSPFN_MALLOC malloc;
300+
JSPFN_FREE free;
301+
JSPFN_REALLOC realloc;
302+
char *errorStr;
303+
char *errorOffset;
304+
int preciseFloat;
305+
void *prv;
306+
} JSONObjectDecoder;
307+
308+
EXPORTFUNCTION JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuffer);
309+
310+
#endif

0 commit comments

Comments
 (0)