Crossfire Server, Trunk  R20513
cjson.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2007 Dan Pascu.
3  * Author: Dan Pascu <dan@ag-projects.com>
4  *
5  * Fast JSON encoder/decoder implementation for Python
6  * This file is under GPL licence
7  */
8 
9 #include <cfpython.h>
10 #include <Python.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <math.h>
15 
16 #if defined(IS_PY26)
17 # include <bytesobject.h>
18 #elif !defined(IS_PY3K)
19 /* Python versions 2.5 or older doesn't have bytesobject.h */
20 # define PyBytesObject PyStringObject
21 
22 # define PyBytes_Check PyString_Check
23 # define PyBytes_AS_STRING PyString_AS_STRING
24 # define PyBytes_GET_SIZE PyString_GET_SIZE
25 
26 # define PyBytes_FromStringAndSize PyString_FromStringAndSize
27 # define PyBytes_FromString PyString_FromString
28 # define PyBytes_Size PyString_Size
29 # define PyBytes_AsString PyString_AsString
30 # define PyBytes_Concat PyString_Concat
31 # define PyBytes_ConcatAndDel PyString_ConcatAndDel
32 # define _PyBytes_Resize _PyString_Resize
33 # define PyBytes_DecodeEscape PyString_DecodeEscape
34 # define _PyBytes_Join _PyString_Join
35 # define PyBytes_AsStringAndSize PyString_AsStringAndSize
36 #endif
37 
38 typedef struct JSONData {
39  char *str; /* the actual json string */
40  char *end; /* pointer to the string end */
41  char *ptr; /* pointer to the current parsing position */
42  int all_unicode; /* make all output strings unicode if true */
43 } JSONData;
44 
45 static PyObject *encode_object(PyObject *object);
46 static PyObject *encode_string(PyObject *object);
47 static PyObject *encode_unicode(PyObject *object);
48 static PyObject *encode_tuple(PyObject *object);
49 static PyObject *encode_list(PyObject *object);
50 static PyObject *encode_dict(PyObject *object);
51 
52 static PyObject *decode_json(JSONData *jsondata);
53 static PyObject *decode_null(JSONData *jsondata);
54 static PyObject *decode_bool(JSONData *jsondata);
55 static PyObject *decode_string(JSONData *jsondata);
56 static PyObject *decode_inf(JSONData *jsondata);
57 static PyObject *decode_nan(JSONData *jsondata);
58 static PyObject *decode_number(JSONData *jsondata);
59 static PyObject *decode_array(JSONData *jsondata);
60 static PyObject *decode_object(JSONData *jsondata);
61 
62 static PyObject *JSON_Error;
63 static PyObject *JSON_EncodeError;
64 static PyObject *JSON_DecodeError;
65 
66 #if PY_VERSION_HEX < 0x02050000
67 typedef int Py_ssize_t;
68 
69 #define PY_SSIZE_T_MAX INT_MAX
70 #define PY_SSIZE_T_MIN INT_MIN
71 
72 #define SSIZE_T_F "%d"
73 #else
74 #define SSIZE_T_F "%zd"
75 #endif
76 
77 #define True 1
78 #define False 0
79 
80 #ifndef INFINITY
81 #define INFINITY HUGE_VAL
82 #endif
83 
84 #ifndef NAN
85 #define NAN (HUGE_VAL-HUGE_VAL)
86 #endif
87 
88 #ifndef Py_IS_NAN
89 #define Py_IS_NAN(X) ((X) != (X))
90 #endif
91 
92 #define skipSpaces(d) while (*((d)->ptr) && isspace(*((d)->ptr))) (d)->ptr++
93 
94 /* ------------------------------ Utility ----------------------------- */
95 
96 #ifdef IS_PY3K
97 
98 static PyObject* cjson_PyObject_Str(PyObject* obj) {
99  PyObject *tmp_str, *result;
100  tmp_str = PyObject_Str(obj);
101  if (!tmp_str)
102  return NULL;
103  result = PyUnicode_AsUTF8String(tmp_str);
104  Py_DECREF(tmp_str);
105  return result;
106 }
107 #else
108 # define cjson_PyObject_Str PyObject_Str
109 #endif
110 
111 /* ------------------------------ Decoding ----------------------------- */
112 
113 static void getRowAndCol(char *begin, char *current, int *row, int *col) {
114  *col = 1;
115  *row = 1;
116  while (current > begin) {
117  if (*current == '\n')
118  (*row)++;
119  if (*row < 2)
120  (*col)++;
121  current--;
122  }
123 }
124 static PyObject *decode_null(JSONData *jsondata) {
125  ptrdiff_t left;
126 
127  left = jsondata->end-jsondata->ptr;
128 
129  if (left >= 4 && strncmp(jsondata->ptr, "null", 4) == 0) {
130  jsondata->ptr += 4;
131  Py_INCREF(Py_None);
132  return Py_None;
133  } else {
134  int row, col;
135 
136  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
137  PyErr_Format(JSON_DecodeError, "cannot parse JSON description: %.20s" "(row "SSIZE_T_F", col "SSIZE_T_F")", jsondata->ptr, (Py_ssize_t)row, (Py_ssize_t)col);
138  return NULL;
139  }
140 }
141 
142 static PyObject *decode_bool(JSONData *jsondata) {
143  ptrdiff_t left;
144 
145  left = jsondata->end-jsondata->ptr;
146 
147  if (left >= 4 && strncmp(jsondata->ptr, "true", 4) == 0) {
148  jsondata->ptr += 4;
149  Py_INCREF(Py_True);
150  return Py_True;
151  } else if (left >= 5 && strncmp(jsondata->ptr, "false", 5) == 0) {
152  jsondata->ptr += 5;
153  Py_INCREF(Py_False);
154  return Py_False;
155  } else {
156  int row, col;
157 
158  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
159  PyErr_Format(JSON_DecodeError, "cannot parse JSON description: %.20s" "(row "SSIZE_T_F", col "SSIZE_T_F")", jsondata->ptr, (Py_ssize_t)row, (Py_ssize_t)col);
160  return NULL;
161  }
162 }
163 
164 static PyObject *decode_string(JSONData *jsondata) {
165  PyObject *object;
166  int c, escaping, has_unicode, string_escape;
167  Py_ssize_t len;
168  char *ptr;
169 
170  /* look for the closing quote */
171  escaping = has_unicode = string_escape = False;
172  ptr = jsondata->ptr+1;
173  while (True) {
174  c = *ptr;
175  if (c == 0) {
176  int row, col;
177 
178  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
179  PyErr_Format(JSON_DecodeError, "unterminated string starting at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
180  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
181  return NULL;
182  }
183  if (!escaping) {
184  if (c == '\\') {
185  escaping = True;
186  } else if (c == '"') {
187  break;
188  } else if (!isascii(c)) {
189  has_unicode = True;
190  }
191  } else {
192  switch (c) {
193  case 'u':
194  has_unicode = True;
195  break;
196  case '"':
197  case 'r':
198  case 'n':
199  case 't':
200  case 'b':
201  case 'f':
202  case '\\':
203  string_escape = True;
204  break;
205  }
206  escaping = False;
207  }
208  ptr++;
209  }
210 
211  len = ptr-jsondata->ptr-1;
212 
213  if (has_unicode || jsondata->all_unicode)
214  object = PyUnicode_DecodeUnicodeEscape(jsondata->ptr+1, len, NULL);
215  else if (string_escape)
216  object = PyBytes_DecodeEscape(jsondata->ptr+1, len, NULL, 0, NULL);
217  else
218  object = PyBytes_FromStringAndSize(jsondata->ptr+1, len);
219 
220  if (object == NULL) {
221  PyObject *type, *value, *tb, *reason;
222 
223  PyErr_Fetch(&type, &value, &tb);
224  if (type == NULL) {
225  int row, col;
226 
227  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
228  PyErr_Format(JSON_DecodeError, "invalid string starting at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
229  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
230  } else {
231  if (PyErr_GivenExceptionMatches(type, PyExc_UnicodeDecodeError)) {
232  int row, col;
233 
234  reason = PyObject_GetAttrString(value, "reason");
235 #ifdef IS_PY3K
236  /* If we are using Python3 we need to convert to bytes here. */
237  if (reason && PyUnicode_Check(reason)) {
238  PyObject * bytes_reason = PyUnicode_AsUTF8String(reason);
239  Py_DECREF(reason);
240  reason = bytes_reason;
241  }
242 #endif
243  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
244  PyErr_Format(JSON_DecodeError, "cannot decode string starting" " at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F"): %s",
245  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col,
246  reason ? PyBytes_AsString(reason) : "bad format");
247  Py_XDECREF(reason);
248  } else {
249  int row, col;
250 
251  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
252  PyErr_Format(JSON_DecodeError, "invalid string starting at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
253  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
254  }
255  }
256  Py_XDECREF(type);
257  Py_XDECREF(value);
258  Py_XDECREF(tb);
259  } else {
260  jsondata->ptr = ptr+1;
261  }
262 
263  return object;
264 }
265 
266 static PyObject *decode_inf(JSONData *jsondata) {
267  PyObject *object;
268  ptrdiff_t left;
269 
270  left = jsondata->end-jsondata->ptr;
271 
272  if (left >= 8 && strncmp(jsondata->ptr, "Infinity", 8) == 0) {
273  jsondata->ptr += 8;
274  object = PyFloat_FromDouble(INFINITY);
275  return object;
276  } else if (left >= 9 && strncmp(jsondata->ptr, "+Infinity", 9) == 0) {
277  jsondata->ptr += 9;
278  object = PyFloat_FromDouble(INFINITY);
279  return object;
280  } else if (left >= 9 && strncmp(jsondata->ptr, "-Infinity", 9) == 0) {
281  jsondata->ptr += 9;
282  object = PyFloat_FromDouble(-INFINITY);
283  return object;
284  } else {
285  int row, col;
286 
287  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
288  PyErr_Format(JSON_DecodeError, "cannot parse JSON description: %.20s (row "SSIZE_T_F", col "SSIZE_T_F")", jsondata->ptr, (Py_ssize_t)row, (Py_ssize_t)col);
289  return NULL;
290  }
291 }
292 
293 static PyObject *decode_nan(JSONData *jsondata) {
294  PyObject *object;
295  ptrdiff_t left;
296 
297  left = jsondata->end-jsondata->ptr;
298 
299  if (left >= 3 && strncmp(jsondata->ptr, "NaN", 3) == 0) {
300  jsondata->ptr += 3;
301  object = PyFloat_FromDouble(NAN);
302  return object;
303  } else {
304  int row, col;
305  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
306  PyErr_Format(JSON_DecodeError, "cannot parse JSON description: %.20s(row "SSIZE_T_F", col "SSIZE_T_F")", jsondata->ptr, (Py_ssize_t)row, (Py_ssize_t)col);
307  return NULL;
308  }
309 }
310 
311 static PyObject *decode_number(JSONData *jsondata) {
312  PyObject *object, *str;
313  int c, is_float, should_stop;
314  char *ptr;
315 
316  /* check if we got a floating point number */
317  ptr = jsondata->ptr;
318  is_float = should_stop = False;
319  while (True) {
320  c = *ptr;
321  if (c == 0)
322  break;
323  switch (c) {
324  case '0':
325  case '1':
326  case '2':
327  case '3':
328  case '4':
329  case '5':
330  case '6':
331  case '7':
332  case '8':
333  case '9':
334  case '-':
335  case '+':
336  break;
337 
338  case '.':
339  case 'e':
340  case 'E':
341  is_float = True;
342  break;
343 
344  default:
345  should_stop = True;
346  }
347  if (should_stop) {
348  break;
349  }
350  ptr++;
351  }
352 
353  str = PyBytes_FromStringAndSize(jsondata->ptr, ptr-jsondata->ptr);
354  if (str == NULL)
355  return NULL;
356 
357  if (is_float) {
358 #ifdef IS_PY3K
359  object = PyFloat_FromString(str);
360 #else
361  object = PyFloat_FromString(str, NULL);
362 #endif
363  } else {
364 #ifdef IS_PY3K
365  object = PyLong_FromString(PyBytes_AS_STRING(str), NULL, 10);
366 #else
367  object = PyInt_FromString(PyBytes_AS_STRING(str), NULL, 10);
368 #endif
369  }
370 
371  Py_DECREF(str);
372 
373  if (object == NULL) {
374  int row, col;
375 
376  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
377  PyErr_Format(JSON_DecodeError, "invalid number starting at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")", (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
378  } else {
379  jsondata->ptr = ptr;
380  }
381 
382  return object;
383 }
384 
385 static PyObject *decode_array(JSONData *jsondata) {
386  PyObject *object, *item;
387  int c, expect_item, items, result;
388  char *start;
389 
390  object = PyList_New(0);
391 
392  start = jsondata->ptr;
393  jsondata->ptr++;
394  expect_item = True;
395  items = 0;
396  while (True) {
397  skipSpaces(jsondata);
398  c = *jsondata->ptr;
399  if (c == 0) {
400  int row, col;
401 
402  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
403  PyErr_Format(JSON_DecodeError, "unterminated array starting at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
404  (Py_ssize_t)(start-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
405  goto failure;
406  } else if (c == ']') {
407  if (expect_item && items > 0) {
408  int row, col;
409 
410  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
411  PyErr_Format(JSON_DecodeError, "expecting array item at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
412  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
413  goto failure;
414  }
415  jsondata->ptr++;
416  break;
417  } else if (c == ',') {
418  if (expect_item) {
419  int row, col;
420 
421  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
422  PyErr_Format(JSON_DecodeError, "expecting array item at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
423  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
424  goto failure;
425  }
426  expect_item = True;
427  jsondata->ptr++;
428  continue;
429  } else {
430  item = decode_json(jsondata);
431  if (item == NULL)
432  goto failure;
433  result = PyList_Append(object, item);
434  Py_DECREF(item);
435  if (result == -1)
436  goto failure;
437  expect_item = False;
438  items++;
439  }
440  }
441 
442  return object;
443 
444 failure:
445  Py_DECREF(object);
446  return NULL;
447 }
448 
449 static PyObject *decode_object(JSONData *jsondata) {
450  PyObject *object, *key, *value;
451  int c, expect_key, items, result;
452  char *start;
453 
454  object = PyDict_New();
455 
456  expect_key = True;
457  items = 0;
458  start = jsondata->ptr;
459  jsondata->ptr++;
460 
461  while (True) {
462  skipSpaces(jsondata);
463  c = *jsondata->ptr;
464  if (c == 0) {
465  int row, col;
466 
467  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
468  PyErr_Format(JSON_DecodeError, "unterminated object starting at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
469  (Py_ssize_t)(start-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
470  goto failure;
471  } else if (c == '}') {
472  if (expect_key && items > 0) {
473  int row, col;
474 
475  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
476  PyErr_Format(JSON_DecodeError, "expecting object property name at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
477  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
478  goto failure;
479  }
480  jsondata->ptr++;
481  break;
482  } else if (c == ',') {
483  if (expect_key) {
484  int row, col;
485 
486  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
487  PyErr_Format(JSON_DecodeError, "expecting object property name at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
488  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
489  goto failure;
490  }
491  expect_key = True;
492  jsondata->ptr++;
493  continue;
494  } else {
495  if (c != '"') {
496  int row, col;
497 
498  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
499  PyErr_Format(JSON_DecodeError, "expecting property name in object at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
500  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
501  goto failure;
502  }
503 
504  key = decode_json(jsondata);
505  if (key == NULL)
506  goto failure;
507 
508  skipSpaces(jsondata);
509  if (*jsondata->ptr != ':') {
510  int row, col;
511 
512  getRowAndCol(jsondata->str, jsondata->ptr, &row, &col);
513  PyErr_Format(JSON_DecodeError, "missing colon after object property name at position "SSIZE_T_F"(row "SSIZE_T_F", col "SSIZE_T_F")",
514  (Py_ssize_t)(jsondata->ptr-jsondata->str), (Py_ssize_t)row, (Py_ssize_t)col);
515  Py_DECREF(key);
516  goto failure;
517  } else {
518  jsondata->ptr++;
519  }
520 
521  value = decode_json(jsondata);
522  if (value == NULL) {
523  Py_DECREF(key);
524  goto failure;
525  }
526 
527  result = PyDict_SetItem(object, key, value);
528  Py_DECREF(key);
529  Py_DECREF(value);
530  if (result == -1)
531  goto failure;
532  expect_key = False;
533  items++;
534  }
535  }
536 
537  return object;
538 
539 failure:
540  Py_DECREF(object);
541  return NULL;
542 }
543 
544 static PyObject *decode_json(JSONData *jsondata) {
545  PyObject *object;
546 
547  skipSpaces(jsondata);
548  switch (*jsondata->ptr) {
549  case 0:
550  PyErr_SetString(JSON_DecodeError, "empty JSON description");
551  return NULL;
552 
553  case '{':
554  object = decode_object(jsondata);
555  break;
556 
557  case '[':
558  object = decode_array(jsondata);
559  break;
560 
561  case '"':
562  object = decode_string(jsondata);
563  break;
564 
565  case 't':
566  case 'f':
567  object = decode_bool(jsondata);
568  break;
569 
570  case 'n':
571  object = decode_null(jsondata);
572  break;
573 
574  case 'N':
575  object = decode_nan(jsondata);
576  break;
577 
578  case 'I':
579  object = decode_inf(jsondata);
580  break;
581 
582  case '+':
583  case '-':
584  if (*(jsondata->ptr+1) == 'I') {
585  object = decode_inf(jsondata);
586  } else {
587  object = decode_number(jsondata);
588  }
589  break;
590 
591  case '0':
592  case '1':
593  case '2':
594  case '3':
595  case '4':
596  case '5':
597  case '6':
598  case '7':
599  case '8':
600  case '9':
601  object = decode_number(jsondata);
602  break;
603 
604  default:
605  PyErr_SetString(JSON_DecodeError, "cannot parse JSON description");
606  return NULL;
607  }
608 
609  return object;
610 }
611 
612 /* ------------------------------ Encoding ----------------------------- */
613 
614 /*
615  * This function is an almost verbatim copy of PyString_Repr() from
616  * Python's stringobject.c with the following differences:
617  *
618  * - it always quotes the output using double quotes.
619  * - it also quotes \b and \f
620  * - it replaces any non ASCII character hh with \u00hh instead of \xhh
621  */
622 static PyObject *encode_string(PyObject *string) {
623  register PyBytesObject *op = (PyBytesObject *)string;
624  size_t newsize = 2+6*Py_SIZE(op);
625  PyObject *v;
626 
627  if (Py_SIZE(op) > (PY_SSIZE_T_MAX-2)/6) {
628  PyErr_SetString(PyExc_OverflowError, "string is too large to make repr");
629  return NULL;
630  }
631  v = PyBytes_FromStringAndSize((char *)NULL, newsize);
632  if (v == NULL) {
633  return NULL;
634  } else {
635  register Py_ssize_t i;
636  register char c;
637  register char *p;
638  int quote;
639 
640  quote = '"';
641 
642  p = PyBytes_AS_STRING(v);
643  *p++ = quote;
644  for (i = 0; i < Py_SIZE(op); i++) {
645  /* There's at least enough room for a hex escape
646  and a closing quote. */
647  assert(newsize-(p-PyBytes_AS_STRING(v)) >= 7);
648  c = op->ob_sval[i];
649  if (c == quote || c == '\\')
650  *p++ = '\\', *p++ = c;
651  else if (c == '\t')
652  *p++ = '\\', *p++ = 't';
653  else if (c == '\n')
654  *p++ = '\\', *p++ = 'n';
655  else if (c == '\r')
656  *p++ = '\\', *p++ = 'r';
657  else if (c == '\f')
658  *p++ = '\\', *p++ = 'f';
659  else if (c == '\b')
660  *p++ = '\\', *p++ = 'b';
661  else if (c < ' ' || c >= 0x7f) {
662  /* For performance, we don't want to call
663  * PyOS_snprintf here (extra layers of
664  * function call). */
665  sprintf(p, "\\u%04x", c&0xff);
666  p += 6;
667  }
668  else
669  *p++ = c;
670  }
671  assert(newsize-(p-PyBytes_AS_STRING(v)) >= 1);
672  *p++ = quote;
673  *p = '\0';
674  _PyBytes_Resize(&v, (int) (p-PyBytes_AS_STRING(v)));
675  return v;
676  }
677 }
678 
679 /*
680  * This function is an almost verbatim copy of unicodeescape_string() from
681  * Python's unicodeobject.c with the following differences:
682  *
683  * - it always quotes the output using double quotes.
684  * - it uses \u00hh instead of \xhh in output.
685  * - it also quotes \b and \f
686  */
687 #if defined(IS_PY26) || defined(IS_PY3K)
688 static PyObject *encode_unicode(PyObject *unicode) {
689  PyObject *repr;
690  Py_UNICODE *s;
691  Py_ssize_t size;
692  char *p;
693  static const char *hexdigit = "0123456789abcdef";
694 #ifdef Py_UNICODE_WIDE
695  static const Py_ssize_t expandsize = 10;
696 #else
697  static const Py_ssize_t expandsize = 6;
698 #endif
699 
700  s = PyUnicode_AS_UNICODE(unicode);
701  size = PyUnicode_GET_SIZE(unicode);
702 
703  if (size > (PY_SSIZE_T_MAX-2-1)/expandsize) {
704  PyErr_SetString(PyExc_OverflowError, "unicode object is too large to make repr");
705  return NULL;
706  }
707 
708  repr = PyByteArray_FromStringAndSize(NULL,
709  2
710  + expandsize*size
711  + 1);
712  if (repr == NULL)
713  return NULL;
714 
715  p = PyByteArray_AS_STRING(repr);
716 
717  *p++ = '"';
718 
719  while (size-- > 0) {
720  Py_UNICODE ch = *s++;
721 
722  /* Escape quotes */
723  if ((ch == (Py_UNICODE)PyByteArray_AS_STRING(repr)[0] || ch == '\\')) {
724  *p++ = '\\';
725  *p++ = (char)ch;
726  continue;
727  }
728 #ifdef Py_UNICODE_WIDE
729  /* Map 21-bit characters to '\U00xxxxxx' */
730  else if (ch >= 0x10000) {
731  *p++ = '\\';
732  *p++ = 'U';
733  *p++ = hexdigit[(ch>>28)&0x0000000F];
734  *p++ = hexdigit[(ch>>24)&0x0000000F];
735  *p++ = hexdigit[(ch>>20)&0x0000000F];
736  *p++ = hexdigit[(ch>>16)&0x0000000F];
737  *p++ = hexdigit[(ch>>12)&0x0000000F];
738  *p++ = hexdigit[(ch>>8)&0x0000000F];
739  *p++ = hexdigit[(ch>>4)&0x0000000F];
740  *p++ = hexdigit[ch&0x0000000F];
741  continue;
742  }
743 #else
744  /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */
745  else if (ch >= 0xD800 && ch < 0xDC00) {
746  Py_UNICODE ch2;
747  Py_UCS4 ucs;
748 
749  ch2 = *s++;
750  size--;
751  if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
752  ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
753  *p++ = '\\';
754  *p++ = 'U';
755  *p++ = hexdigit[(ucs>>28)&0x0000000F];
756  *p++ = hexdigit[(ucs>>24)&0x0000000F];
757  *p++ = hexdigit[(ucs>>20)&0x0000000F];
758  *p++ = hexdigit[(ucs>>16)&0x0000000F];
759  *p++ = hexdigit[(ucs>>12)&0x0000000F];
760  *p++ = hexdigit[(ucs>>8)&0x0000000F];
761  *p++ = hexdigit[(ucs>>4)&0x0000000F];
762  *p++ = hexdigit[ucs&0x0000000F];
763  continue;
764  }
765  /* Fall through: isolated surrogates are copied as-is */
766  s--;
767  size++;
768  }
769 #endif
770  /* Map 16-bit characters to '\uxxxx' */
771  if (ch >= 256) {
772  *p++ = '\\';
773  *p++ = 'u';
774  *p++ = hexdigit[(ch>>12)&0x000F];
775  *p++ = hexdigit[(ch>>8)&0x000F];
776  *p++ = hexdigit[(ch>>4)&0x000F];
777  *p++ = hexdigit[ch&0x000F];
778  /* Map special whitespace to '\t', \n', '\r', '\f', '\b' */
779  } else if (ch == '\t') {
780  *p++ = '\\';
781  *p++ = 't';
782  } else if (ch == '\n') {
783  *p++ = '\\';
784  *p++ = 'n';
785  } else if (ch == '\r') {
786  *p++ = '\\';
787  *p++ = 'r';
788  } else if (ch == '\f') {
789  *p++ = '\\';
790  *p++ = 'f';
791  } else if (ch == '\b') {
792  *p++ = '\\';
793  *p++ = 'b';
794  }
795 
796  /* Map non-printable US ASCII to '\u00hh' */
797  else if (ch < ' ' || ch >= 0x7F) {
798  *p++ = '\\';
799  *p++ = 'u';
800  *p++ = '0';
801  *p++ = '0';
802  *p++ = hexdigit[(ch>>4)&0x000F];
803  *p++ = hexdigit[ch&0x000F];
804  }
805 
806  /* Copy everything else as-is */
807  else
808  *p++ = (char)ch;
809  }
810 
811  *p++ = PyByteArray_AS_STRING(repr)[0];
812 
813  *p = '\0';
814  {
815  PyObject *result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr),
816  p - PyByteArray_AS_STRING(repr));
817  Py_DECREF(repr);
818  return result;
819  }
820 }
821 #else
822 static PyObject *encode_unicode(PyObject *unicode) {
823  PyObject *repr;
824  Py_UNICODE *s;
825  Py_ssize_t size;
826  char *p;
827  static const char *hexdigit = "0123456789abcdef";
828 #ifdef Py_UNICODE_WIDE
829  static const Py_ssize_t expandsize = 10;
830 #else
831  static const Py_ssize_t expandsize = 6;
832 #endif
833 
834  s = PyUnicode_AS_UNICODE(unicode);
835  size = PyUnicode_GET_SIZE(unicode);
836 
837  if (size > (PY_SSIZE_T_MAX-2-1)/expandsize) {
838  PyErr_SetString(PyExc_OverflowError, "unicode object is too large to make repr");
839  return NULL;
840  }
841 
842  repr = PyString_FromStringAndSize(NULL, 2+expandsize*size+1);
843  if (repr == NULL)
844  return NULL;
845 
846  p = PyString_AS_STRING(repr);
847 
848  *p++ = '"';
849 
850  while (size-- > 0) {
851  Py_UNICODE ch = *s++;
852 
853  /* Escape quotes */
854  if ((ch == (Py_UNICODE)PyString_AS_STRING(repr)[0] || ch == '\\')) {
855  *p++ = '\\';
856  *p++ = (char)ch;
857  continue;
858  }
859 #ifdef Py_UNICODE_WIDE
860  /* Map 21-bit characters to '\U00xxxxxx' */
861  else if (ch >= 0x10000) {
862  int offset = p-PyString_AS_STRING(repr);
863 
864  /* Resize the string if necessary */
865  if (offset+12 > PyString_GET_SIZE(repr)) {
866  if (_PyString_Resize(&repr, PyString_GET_SIZE(repr)+100))
867  return NULL;
868  p = PyString_AS_STRING(repr)+offset;
869  }
870 
871  *p++ = '\\';
872  *p++ = 'U';
873  *p++ = hexdigit[(ch>>28)&0x0000000F];
874  *p++ = hexdigit[(ch>>24)&0x0000000F];
875  *p++ = hexdigit[(ch>>20)&0x0000000F];
876  *p++ = hexdigit[(ch>>16)&0x0000000F];
877  *p++ = hexdigit[(ch>>12)&0x0000000F];
878  *p++ = hexdigit[(ch>>8)&0x0000000F];
879  *p++ = hexdigit[(ch>>4)&0x0000000F];
880  *p++ = hexdigit[ch&0x0000000F];
881  continue;
882  }
883 #endif
884  /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */
885  else if (ch >= 0xD800 && ch < 0xDC00) {
886  Py_UNICODE ch2;
887  Py_UCS4 ucs;
888 
889  ch2 = *s++;
890  size--;
891  if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
892  ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
893  *p++ = '\\';
894  *p++ = 'U';
895  *p++ = hexdigit[(ucs>>28)&0x0000000F];
896  *p++ = hexdigit[(ucs>>24)&0x0000000F];
897  *p++ = hexdigit[(ucs>>20)&0x0000000F];
898  *p++ = hexdigit[(ucs>>16)&0x0000000F];
899  *p++ = hexdigit[(ucs>>12)&0x0000000F];
900  *p++ = hexdigit[(ucs>>8)&0x0000000F];
901  *p++ = hexdigit[(ucs>>4)&0x0000000F];
902  *p++ = hexdigit[ucs&0x0000000F];
903  continue;
904  }
905  /* Fall through: isolated surrogates are copied as-is */
906  s--;
907  size++;
908  /* Map 16-bit characters to '\uxxxx' */
909  } if (ch >= 256) {
910  *p++ = '\\';
911  *p++ = 'u';
912  *p++ = hexdigit[(ch>>12)&0x000F];
913  *p++ = hexdigit[(ch>>8)&0x000F];
914  *p++ = hexdigit[(ch>>4)&0x000F];
915  *p++ = hexdigit[ch&0x000F];
916  /* Map special whitespace to '\t', \n', '\r', '\f', '\b' */
917  } else if (ch == '\t') {
918  *p++ = '\\';
919  *p++ = 't';
920  } else if (ch == '\n') {
921  *p++ = '\\';
922  *p++ = 'n';
923  } else if (ch == '\r') {
924  *p++ = '\\';
925  *p++ = 'r';
926  } else if (ch == '\f') {
927  *p++ = '\\';
928  *p++ = 'f';
929  } else if (ch == '\b') {
930  *p++ = '\\';
931  *p++ = 'b';
932  }
933 
934  /* Map non-printable US ASCII to '\u00hh' */
935  else if (ch < ' ' || ch >= 0x7F) {
936  *p++ = '\\';
937  *p++ = 'u';
938  *p++ = '0';
939  *p++ = '0';
940  *p++ = hexdigit[(ch>>4)&0x000F];
941  *p++ = hexdigit[ch&0x000F];
942  }
943 
944  /* Copy everything else as-is */
945  else
946  *p++ = (char)ch;
947  }
948 
949  *p++ = PyString_AS_STRING(repr)[0];
950 
951  *p = '\0';
952  _PyString_Resize(&repr, p-PyString_AS_STRING(repr));
953  return repr;
954 }
955 #endif
956 
957 /*
958  * This function is an almost verbatim copy of tuplerepr() from
959  * Python's tupleobject.c with the following differences:
960  *
961  * - it uses encode_object() to get the object's JSON reprezentation.
962  * - it uses [] as decorations isntead of () (to masquerade as a JSON array).
963  */
964 
965 static PyObject *encode_tuple(PyObject *tuple) {
966  Py_ssize_t i, n;
967  PyObject *s, *temp;
968  PyObject *pieces, *result = NULL;
969  PyTupleObject *v = (PyTupleObject *)tuple;
970 
971  n = Py_SIZE(v);
972  if (n == 0)
973  return PyBytes_FromString("[]");
974 
975  pieces = PyTuple_New(n);
976  if (pieces == NULL)
977  return NULL;
978 
979  /* Do repr() on each element. */
980  for (i = 0; i < n; ++i) {
981  s = encode_object(v->ob_item[i]);
982  if (s == NULL)
983  goto Done;
984  PyTuple_SET_ITEM(pieces, i, s);
985  }
986 
987  /* Add "[]" decorations to the first and last items. */
988  assert(n > 0);
989  s = PyBytes_FromString("[");
990  if (s == NULL)
991  goto Done;
992  temp = PyTuple_GET_ITEM(pieces, 0);
993  PyBytes_ConcatAndDel(&s, temp);
994  PyTuple_SET_ITEM(pieces, 0, s);
995  if (s == NULL)
996  goto Done;
997 
998  s = PyBytes_FromString("]");
999  if (s == NULL)
1000  goto Done;
1001  temp = PyTuple_GET_ITEM(pieces, n-1);
1002  PyBytes_ConcatAndDel(&temp, s);
1003  PyTuple_SET_ITEM(pieces, n-1, temp);
1004  if (temp == NULL)
1005  goto Done;
1006 
1007  /* Paste them all together with ", " between. */
1008  s = PyBytes_FromString(", ");
1009  if (s == NULL)
1010  goto Done;
1011  result = _PyBytes_Join(s, pieces);
1012  Py_DECREF(s);
1013 
1014 Done:
1015  Py_DECREF(pieces);
1016  return result;
1017 }
1018 
1019 /*
1020  * This function is an almost verbatim copy of list_repr() from
1021  * Python's listobject.c with the following differences:
1022  *
1023  * - it uses encode_object() to get the object's JSON reprezentation.
1024  * - it doesn't use the ellipsis to represent a list with references
1025  * to itself, instead it raises an exception as such lists cannot be
1026  * represented in JSON.
1027  */
1028 static PyObject *encode_list(PyObject *list) {
1029  Py_ssize_t i;
1030  PyObject *s, *temp;
1031  PyObject *pieces = NULL, *result = NULL;
1032  PyListObject *v = (PyListObject *)list;
1033 
1034  i = Py_ReprEnter((PyObject *)v);
1035  if (i != 0) {
1036  if (i > 0) {
1037  PyErr_SetString(JSON_EncodeError, "a list with references to itself is not JSON encodable");
1038  }
1039  return NULL;
1040  }
1041 
1042  if (Py_SIZE(v) == 0) {
1043  result = PyBytes_FromString("[]");
1044  goto Done;
1045  }
1046 
1047  pieces = PyList_New(0);
1048  if (pieces == NULL)
1049  goto Done;
1050 
1051  /* Do repr() on each element. Note that this may mutate the list,
1052  * so must refetch the list size on each iteration. */
1053  for (i = 0; i < Py_SIZE(v); ++i) {
1054  int status;
1055  s = encode_object(v->ob_item[i]);
1056  if (s == NULL)
1057  goto Done;
1058  status = PyList_Append(pieces, s);
1059  Py_DECREF(s); /* append created a new ref */
1060  if (status < 0)
1061  goto Done;
1062  }
1063 
1064  /* Add "[]" decorations to the first and last items. */
1065  assert(PyList_GET_SIZE(pieces) > 0);
1066  s = PyBytes_FromString("[");
1067  if (s == NULL)
1068  goto Done;
1069  temp = PyList_GET_ITEM(pieces, 0);
1070  PyBytes_ConcatAndDel(&s, temp);
1071  PyList_SET_ITEM(pieces, 0, s);
1072  if (s == NULL)
1073  goto Done;
1074 
1075  s = PyBytes_FromString("]");
1076  if (s == NULL)
1077  goto Done;
1078  temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
1079  PyBytes_ConcatAndDel(&temp, s);
1080  PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1, temp);
1081  if (temp == NULL)
1082  goto Done;
1083 
1084  /* Paste them all together with ", " between. */
1085  s = PyBytes_FromString(", ");
1086  if (s == NULL)
1087  goto Done;
1088  result = _PyBytes_Join(s, pieces);
1089  Py_DECREF(s);
1090 
1091 Done:
1092  Py_XDECREF(pieces);
1093  Py_ReprLeave((PyObject *)v);
1094  return result;
1095 }
1096 
1097 /*
1098  * This function is an almost verbatim copy of dict_repr() from
1099  * Python's dictobject.c with the following differences:
1100  *
1101  * - it uses encode_object() to get the object's JSON reprezentation.
1102  * - only accept strings for keys.
1103  * - it doesn't use the ellipsis to represent a dictionary with references
1104  * to itself, instead it raises an exception as such dictionaries cannot
1105  * be represented in JSON.
1106  */
1107 static PyObject *encode_dict(PyObject *dict) {
1108  Py_ssize_t i;
1109  PyObject *s, *temp, *colon = NULL;
1110  PyObject *pieces = NULL, *result = NULL;
1111  PyObject *key, *value;
1112  PyDictObject *mp = (PyDictObject *)dict;
1113 
1114  i = Py_ReprEnter((PyObject *)mp);
1115  if (i != 0) {
1116  if (i > 0) {
1117  PyErr_SetString(JSON_EncodeError, "a dict with references to "
1118  "itself is not JSON encodable");
1119  }
1120  return NULL;
1121  }
1122 
1123  if (mp->ma_used == 0) {
1124  result = PyBytes_FromString("{}");
1125  goto Done;
1126  }
1127 
1128  pieces = PyList_New(0);
1129  if (pieces == NULL)
1130  goto Done;
1131 
1132  colon = PyBytes_FromString(": ");
1133  if (colon == NULL)
1134  goto Done;
1135 
1136  /* Do repr() on each key+value pair, and insert ": " between them.
1137  * Note that repr may mutate the dict. */
1138  i = 0;
1139  while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1140  int status;
1141 
1142  if (!PyBytes_Check(key) && !PyUnicode_Check(key)) {
1143  PyErr_SetString(JSON_EncodeError, "JSON encodable dictionaries must have string/unicode keys");
1144  goto Done;
1145  }
1146 
1147  /* Prevent repr from deleting value during key format. */
1148  Py_INCREF(value);
1149  s = encode_object(key);
1150  PyBytes_Concat(&s, colon);
1151  PyBytes_ConcatAndDel(&s, encode_object(value));
1152  Py_DECREF(value);
1153  if (s == NULL)
1154  goto Done;
1155  status = PyList_Append(pieces, s);
1156  Py_DECREF(s); /* append created a new ref */
1157  if (status < 0)
1158  goto Done;
1159  }
1160 
1161  /* Add "{}" decorations to the first and last items. */
1162  assert(PyList_GET_SIZE(pieces) > 0);
1163  s = PyBytes_FromString("{");
1164  if (s == NULL)
1165  goto Done;
1166  temp = PyList_GET_ITEM(pieces, 0);
1167  PyBytes_ConcatAndDel(&s, temp);
1168  PyList_SET_ITEM(pieces, 0, s);
1169  if (s == NULL)
1170  goto Done;
1171 
1172  s = PyBytes_FromString("}");
1173  if (s == NULL)
1174  goto Done;
1175  temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
1176  PyBytes_ConcatAndDel(&temp, s);
1177  PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1, temp);
1178  if (temp == NULL)
1179  goto Done;
1180 
1181  /* Paste them all together with ", " between. */
1182  s = PyBytes_FromString(", ");
1183  if (s == NULL)
1184  goto Done;
1185  result = _PyBytes_Join(s, pieces);
1186  Py_DECREF(s);
1187 
1188 Done:
1189  Py_XDECREF(pieces);
1190  Py_XDECREF(colon);
1191  Py_ReprLeave((PyObject *)mp);
1192  return result;
1193 }
1194 
1195 static PyObject *encode_object(PyObject *object) {
1196  if (object == Py_True) {
1197  return PyBytes_FromString("true");
1198  } else if (object == Py_False) {
1199  return PyBytes_FromString("false");
1200  } else if (object == Py_None) {
1201  return PyBytes_FromString("null");
1202  } else if (PyBytes_Check(object)) {
1203  return encode_string(object);
1204  } else if (PyUnicode_Check(object)) {
1205  return encode_unicode(object);
1206  } else if (PyFloat_Check(object)) {
1207  double val = PyFloat_AS_DOUBLE(object);
1208  if (Py_IS_NAN(val)) {
1209  return PyBytes_FromString("NaN");
1210  } else if (Py_IS_INFINITY(val)) {
1211  if (val > 0) {
1212  return PyBytes_FromString("Infinity");
1213  } else {
1214  return PyBytes_FromString("-Infinity");
1215  }
1216  } else {
1217  return cjson_PyObject_Str(object);
1218  }
1219  }
1220 #ifdef IS_PY3K
1221  else if (PyLong_Check(object)) {
1222  return cjson_PyObject_Str(object);
1223  }
1224 #else
1225  else if (PyInt_Check(object) || PyLong_Check(object)) {
1226  return cjson_PyObject_Str(object);
1227  }
1228 #endif
1229  else if (PyList_Check(object)) {
1230  return encode_list(object);
1231  } else if (PyTuple_Check(object)) {
1232  return encode_tuple(object);
1233  } else if (PyDict_Check(object)) { /* use PyMapping_Check(object) instead? -Dan */
1234  return encode_dict(object);
1235  } else {
1236  PyErr_SetString(JSON_EncodeError, "object is not JSON encodable");
1237  return NULL;
1238  }
1239 }
1240 
1241 /* Encode object into its JSON representation */
1242 
1243 static PyObject *JSON_encode(PyObject *self, PyObject *object) {
1244  return encode_object(object);
1245 }
1246 
1247 /* Decode JSON representation into pyhton objects */
1248 
1249 static PyObject *JSON_decode(PyObject *self, PyObject *args, PyObject *kwargs) {
1250  static char *kwlist[] = { "json", "all_unicode", NULL };
1251 #ifdef IS_PY3K
1252  int all_unicode = True; /* by default return unicode always */
1253 #else
1254  int all_unicode = False; /* by default return unicode only when needed */
1255 #endif
1256  PyObject *object, *string, *str;
1257  JSONData jsondata;
1258 
1259  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:decode", kwlist, &string, &all_unicode))
1260  return NULL;
1261 
1262  if (PyUnicode_Check(string)) {
1263 #ifdef IS_PY3K
1264  /* HACK: Workaround for crash bug in Python3's PyUnicode_AsRawUnicodeEscapeString... */
1265  str = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(string),
1266  PyUnicode_GET_SIZE(string));
1267 #else
1268  str = PyUnicode_AsRawUnicodeEscapeString(string);
1269 #endif
1270  if (str == NULL) {
1271  return NULL;
1272  }
1273  } else {
1274  Py_INCREF(string);
1275  str = string;
1276  }
1277 
1278  if (PyBytes_AsStringAndSize(str, &(jsondata.str), NULL) == -1) {
1279  Py_DECREF(str);
1280  return NULL; /* not a string object or it contains null bytes */
1281  }
1282 
1283  jsondata.ptr = jsondata.str;
1284  jsondata.end = jsondata.str+strlen(jsondata.str);
1285  jsondata.all_unicode = all_unicode;
1286 
1287  object = decode_json(&jsondata);
1288 
1289  if (object != NULL) {
1290  skipSpaces(&jsondata);
1291  if (jsondata.ptr < jsondata.end) {
1292  PyErr_Format(JSON_DecodeError, "extra data after JSON description at position "SSIZE_T_F, (Py_ssize_t)(jsondata.ptr-jsondata.str));
1293  Py_DECREF(str);
1294  Py_DECREF(object);
1295  return NULL;
1296  }
1297  }
1298 
1299  Py_DECREF(str);
1300 
1301  return object;
1302 }
1303 
1304 /* List of functions defined in the module */
1305 
1306 static PyMethodDef cjson_methods[] = {
1307  { "encode", (PyCFunction)JSON_encode, METH_O,
1308  PyDoc_STR("encode(object) -> generate the JSON representation for object.") },
1309 
1310 /* On Python 3.x we normally want Unicode. */
1311  { "decode", (PyCFunction)JSON_decode, METH_VARARGS|METH_KEYWORDS,
1312 #ifdef IS_PY3K
1313  PyDoc_STR("decode(string, all_unicode=True) -> parse the JSON representation into\n"
1314  "python objects. The optional argument `all_unicode', specifies how to\n"
1315  "convert the strings in the JSON representation into python objects.\n"
1316  "If it is False (default on Python 2.x), it will return strings/bytes\n"
1317  "everywhere possible and unicode objects only where necessary, else\n"
1318  "it will return unicode objects everywhere (this is slower, but default\n"
1319  "on Python 3.x).")
1320 #else
1321  PyDoc_STR("decode(string, all_unicode=False) -> parse the JSON representation into\n"
1322  "python objects. The optional argument `all_unicode', specifies how to\n"
1323  "convert the strings in the JSON representation into python objects.\n"
1324  "If it is False (default on Python 2.x), it will return strings/bytes\n"
1325  "everywhere possible and unicode objects only where necessary, else\n"
1326  "it will return unicode objects everywhere (this is slower, but default\n"
1327  "on Python 3.x).")
1328 #endif
1329  },
1330  { NULL, NULL, 0, NULL } /* sentinel */
1331 };
1332 
1333 PyDoc_STRVAR(module_doc, "Fast JSON encoder/decoder module.");
1334 
1335 #define MODULE_VERSION "1.0.5"
1336 
1337 static void initcjson_shared(PyObject *m) {
1338  JSON_Error = PyErr_NewException("cjson.Error", NULL, NULL);
1339  if (JSON_Error == NULL)
1340  return;
1341  Py_INCREF(JSON_Error);
1342  PyModule_AddObject(m, "Error", JSON_Error);
1343 
1344  JSON_EncodeError = PyErr_NewException("cjson.EncodeError", JSON_Error, NULL);
1345  if (JSON_EncodeError == NULL)
1346  return;
1347  Py_INCREF(JSON_EncodeError);
1348  PyModule_AddObject(m, "EncodeError", JSON_EncodeError);
1349 
1350  JSON_DecodeError = PyErr_NewException("cjson.DecodeError", JSON_Error, NULL);
1351  if (JSON_DecodeError == NULL)
1352  return;
1353  Py_INCREF(JSON_DecodeError);
1354  PyModule_AddObject(m, "DecodeError", JSON_DecodeError);
1355 
1356  /* Module version (the MODULE_VERSION macro is defined by setup.py) */
1357  PyModule_AddStringConstant(m, "__version__", MODULE_VERSION);
1358 }
1359 
1360 
1361 #ifdef IS_PY3K
1362 static PyModuleDef cjsonModule = {
1363  PyModuleDef_HEAD_INIT,
1364  "cjson", /* m_name */
1365  module_doc, /* m_doc */
1366  -1, /* m_size */
1367  cjson_methods, /* m_methods */
1368  NULL, /* m_reload */
1369  NULL, /* m_traverse */
1370  NULL, /* m_clear */
1371  NULL /* m_free */
1372 };
1373 
1374 PyObject* PyInit_cjson(void)
1375 {
1376  PyObject *m;
1377 
1378  m = PyModule_Create(&cjsonModule);
1379 
1380  if (m == NULL)
1381  return NULL;
1382 
1383  initcjson_shared(m);
1384 
1385  return m;
1386 }
1387 #else
1388 /* Initialization function for the module (*must *be called initcjson) */
1389 PyMODINIT_FUNC initcjson(void) {
1390  PyObject *m;
1391 
1392  m = Py_InitModule3("cjson", cjson_methods, module_doc);
1393 
1394  if (m == NULL)
1395  return;
1396 
1397  initcjson_shared(m);
1398 }
1399 #endif
static PyObject * decode_number(JSONData *jsondata)
Definition: cjson.c:311
Main Crossfire structure, one ingame object.
Definition: object.h:274
PyDoc_STRVAR(module_doc,"Fast JSON encoder/decoder module.")
#define True
Definition: cjson.c:77
Definition: cjson.c:38
#define PyBytesObject
Definition: cjson.c:20
static PyObject * decode_nan(JSONData *jsondata)
Definition: cjson.c:293
static PyObject * decode_object(JSONData *jsondata)
Definition: cjson.c:449
static PyObject * decode_array(JSONData *jsondata)
Definition: cjson.c:385
#define PyBytes_AsStringAndSize
Definition: cjson.c:35
static PyObject * JSON_Error
Definition: cjson.c:62
#define PyBytes_AS_STRING
Definition: cjson.c:23
static void initcjson_shared(PyObject *m)
Definition: cjson.c:1337
static PyObject * encode_unicode(PyObject *object)
Definition: cjson.c:822
char * ptr
Definition: cjson.c:41
#define PyBytes_AsString
Definition: cjson.c:29
PyMODINIT_FUNC initcjson(void)
Definition: cjson.c:1389
#define NAN
Definition: cjson.c:85
#define PyBytes_FromStringAndSize
Definition: cjson.c:26
static PyObject * encode_dict(PyObject *object)
Definition: cjson.c:1107
struct obj object
Main Crossfire structure, one ingame object.
#define _PyBytes_Resize
Definition: cjson.c:32
static PyObject * encode_list(PyObject *object)
Definition: cjson.c:1028
static void getRowAndCol(char *begin, char *current, int *row, int *col)
Definition: cjson.c:113
#define skipSpaces(d)
Definition: cjson.c:92
static PyMethodDef cjson_methods[]
Definition: cjson.c:1306
#define PyBytes_Concat
Definition: cjson.c:30
static PyObject * decode_inf(JSONData *jsondata)
Definition: cjson.c:266
struct JSONData JSONData
#define PyBytes_DecodeEscape
Definition: cjson.c:33
#define PY_SSIZE_T_MAX
Definition: cjson.c:69
static PyObject * JSON_encode(PyObject *self, PyObject *object)
Definition: cjson.c:1243
#define PyBytes_FromString
Definition: cjson.c:27
#define PyBytes_Check
Definition: cjson.c:22
int all_unicode
Definition: cjson.c:42
#define PyBytes_ConcatAndDel
Definition: cjson.c:31
char * str
Definition: cjson.c:39
#define INFINITY
Definition: cjson.c:81
#define Py_IS_NAN(X)
Definition: cjson.c:89
#define MODULE_VERSION
Definition: cjson.c:1335
#define False
Definition: cjson.c:78
static PyObject * decode_bool(JSONData *jsondata)
Definition: cjson.c:142
#define SSIZE_T_F
Definition: cjson.c:72
static PyObject * decode_null(JSONData *jsondata)
Definition: cjson.c:124
static PyObject * JSON_EncodeError
Definition: cjson.c:63
int Py_ssize_t
Definition: cjson.c:67
#define cjson_PyObject_Str
Definition: cjson.c:108
static PyObject * decode_json(JSONData *jsondata)
Definition: cjson.c:544
#define Py_SIZE(ob)
Definition: cfpython.h:63
static PyObject * encode_tuple(PyObject *object)
Definition: cjson.c:965
static PyObject * encode_object(PyObject *object)
Definition: cjson.c:1195
static PyObject * JSON_DecodeError
Definition: cjson.c:64
static PyObject * JSON_decode(PyObject *self, PyObject *args, PyObject *kwargs)
Definition: cjson.c:1249
#define _PyBytes_Join
Definition: cjson.c:34
char * end
Definition: cjson.c:40
static PyObject * decode_string(JSONData *jsondata)
Definition: cjson.c:164
static PyObject * encode_string(PyObject *object)
Definition: cjson.c:622