16 #pragma GCC diagnostic push
17 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
18 #pragma GCC diagnostic ignored "-Wcast-function-type"
51 #if PY_VERSION_HEX < 0x02050000
54 #define PY_SSIZE_T_MAX INT_MAX
55 #define PY_SSIZE_T_MIN INT_MIN
57 #define SSIZE_T_F "%d"
59 #define SSIZE_T_F "%zd"
66 #define INFINITY HUGE_VAL
70 #define NAN (HUGE_VAL-HUGE_VAL)
74 #define Py_IS_NAN(X) ((X) != (X))
77 #define skipSpaces(d) while (*((d)->ptr) && isspace(*((d)->ptr))) (d)->ptr++
83 PyObject *tmp_str, *
result;
84 tmp_str = PyObject_Str(
obj);
87 result = PyUnicode_AsUTF8String(tmp_str);
108 left = jsondata->
end-jsondata->
ptr;
110 if (left >= 4 && strncmp(jsondata->
ptr,
"null", 4) == 0) {
126 left = jsondata->
end-jsondata->
ptr;
128 if (left >= 4 && strncmp(jsondata->
ptr,
"true", 4) == 0) {
132 }
else if (left >= 5 && strncmp(jsondata->
ptr,
"false", 5) == 0) {
147 int c, escaping, has_unicode, string_escape;
152 escaping = has_unicode = string_escape =
False;
153 ptr = jsondata->
ptr+1;
167 }
else if (
c ==
'"') {
169 }
else if (!isascii(
c)) {
184 string_escape =
True;
192 len = ptr-jsondata->
ptr-1;
195 object = PyUnicode_DecodeUnicodeEscape(jsondata->
ptr+1, len, NULL);
196 else if (string_escape)
197 object = PyBytes_DecodeEscape(jsondata->
ptr+1, len, NULL, 0, NULL);
199 object = PyBytes_FromStringAndSize(jsondata->
ptr+1, len);
201 if (
object == NULL) {
202 PyObject *
type, *value, *tb, *reason;
204 PyErr_Fetch(&
type, &value, &tb);
212 if (PyErr_GivenExceptionMatches(
type, PyExc_UnicodeDecodeError)) {
215 reason = PyObject_GetAttrString(value,
"reason");
217 if (reason && PyUnicode_Check(reason)) {
218 PyObject * bytes_reason = PyUnicode_AsUTF8String(reason);
220 reason = bytes_reason;
225 reason ? PyBytes_AsString(reason) :
"bad format");
239 jsondata->
ptr = ptr+1;
249 left = jsondata->
end-jsondata->
ptr;
251 if (left >= 8 && strncmp(jsondata->
ptr,
"Infinity", 8) == 0) {
253 object = PyFloat_FromDouble(
INFINITY);
255 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"+Infinity", 9) == 0) {
257 object = PyFloat_FromDouble(
INFINITY);
259 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"-Infinity", 9) == 0) {
261 object = PyFloat_FromDouble(-
INFINITY);
276 left = jsondata->
end-jsondata->
ptr;
278 if (left >= 3 && strncmp(jsondata->
ptr,
"NaN", 3) == 0) {
280 object = PyFloat_FromDouble(
NAN);
292 int c, is_float, should_stop;
297 is_float = should_stop =
False;
332 str = PyBytes_FromStringAndSize(jsondata->
ptr, ptr-jsondata->
ptr);
337 object = PyFloat_FromString(
str);
339 object = PyLong_FromString(PyBytes_AS_STRING(
str), NULL, 10);
344 if (
object == NULL) {
358 int c, expect_item, items,
result;
361 object = PyList_New(0);
363 start = jsondata->
ptr;
377 }
else if (
c ==
']') {
378 if (expect_item && items > 0) {
388 }
else if (
c ==
',') {
421 PyObject *
object, *key, *value;
422 int c, expect_key, items,
result;
425 object = PyDict_New();
429 start = jsondata->
ptr;
442 }
else if (
c ==
'}') {
443 if (expect_key && items > 0) {
453 }
else if (
c ==
',') {
480 if (*jsondata->
ptr !=
':') {
498 result = PyDict_SetItem(
object, key, value);
519 switch (*jsondata->
ptr) {
555 if (*(jsondata->
ptr+1) ==
'I') {
594 register PyBytesObject *
op = (PyBytesObject *)
string;
595 size_t newsize = 2+6*Py_SIZE(
op);
599 PyErr_SetString(PyExc_OverflowError,
"string is too large to make repr");
602 v = PyBytes_FromStringAndSize((
char *)NULL, newsize);
613 p = PyBytes_AS_STRING(v);
615 for (i = 0; i < Py_SIZE(
op); i++) {
618 assert(newsize-(p-PyBytes_AS_STRING(v)) >= 7);
620 if (
c == quote ||
c ==
'\\')
621 *p++ =
'\\', *p++ =
c;
623 *p++ =
'\\', *p++ =
't';
625 *p++ =
'\\', *p++ =
'n';
627 *p++ =
'\\', *p++ =
'r';
629 *p++ =
'\\', *p++ =
'f';
631 *p++ =
'\\', *p++ =
'b';
632 else if (c < ' ' || c >= 0x7f) {
636 sprintf(p,
"\\u%04x",
c&0xff);
642 assert(newsize-(p-PyBytes_AS_STRING(v)) >= 1);
645 _PyBytes_Resize(&v, (
int) (p-PyBytes_AS_STRING(v)));
663 static const char *hexdigit =
"0123456789abcdef";
664 #ifdef Py_UNICODE_WIDE
670 s = PyUnicode_AS_UNICODE(unicode);
671 size = PyUnicode_GET_SIZE(unicode);
674 PyErr_SetString(PyExc_OverflowError,
"unicode object is too large to make repr");
678 repr = PyByteArray_FromStringAndSize(NULL,
685 p = PyByteArray_AS_STRING(repr);
690 Py_UNICODE ch = *s++;
693 if ((ch == (Py_UNICODE)PyByteArray_AS_STRING(repr)[0] || ch ==
'\\')) {
698 #ifdef Py_UNICODE_WIDE
700 else if (ch >= 0x10000) {
703 *p++ = hexdigit[(ch>>28)&0x0000000F];
704 *p++ = hexdigit[(ch>>24)&0x0000000F];
705 *p++ = hexdigit[(ch>>20)&0x0000000F];
706 *p++ = hexdigit[(ch>>16)&0x0000000F];
707 *p++ = hexdigit[(ch>>12)&0x0000000F];
708 *p++ = hexdigit[(ch>>8)&0x0000000F];
709 *p++ = hexdigit[(ch>>4)&0x0000000F];
710 *p++ = hexdigit[ch&0x0000000F];
715 else if (ch >= 0xD800 && ch < 0xDC00) {
721 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
722 ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
725 *p++ = hexdigit[(ucs>>28)&0x0000000F];
726 *p++ = hexdigit[(ucs>>24)&0x0000000F];
727 *p++ = hexdigit[(ucs>>20)&0x0000000F];
728 *p++ = hexdigit[(ucs>>16)&0x0000000F];
729 *p++ = hexdigit[(ucs>>12)&0x0000000F];
730 *p++ = hexdigit[(ucs>>8)&0x0000000F];
731 *p++ = hexdigit[(ucs>>4)&0x0000000F];
732 *p++ = hexdigit[ucs&0x0000000F];
744 *p++ = hexdigit[(ch>>12)&0x000F];
745 *p++ = hexdigit[(ch>>8)&0x000F];
746 *p++ = hexdigit[(ch>>4)&0x000F];
747 *p++ = hexdigit[ch&0x000F];
749 }
else if (ch ==
'\t') {
752 }
else if (ch ==
'\n') {
755 }
else if (ch ==
'\r') {
758 }
else if (ch ==
'\f') {
761 }
else if (ch ==
'\b') {
767 else if (ch < ' ' || ch >= 0x7F) {
772 *p++ = hexdigit[(ch>>4)&0x000F];
773 *p++ = hexdigit[ch&0x000F];
781 *p++ = PyByteArray_AS_STRING(repr)[0];
785 PyObject *
result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr),
786 p - PyByteArray_AS_STRING(repr));
803 PyObject *pieces, *
result = NULL;
804 PyTupleObject *v = (PyTupleObject *)tuple;
808 return PyBytes_FromString(
"[]");
810 pieces = PyTuple_New(n);
815 for (i = 0; i < n; ++i) {
819 PyTuple_SET_ITEM(pieces, i, s);
824 s = PyBytes_FromString(
"[");
827 temp = PyTuple_GET_ITEM(pieces, 0);
828 PyBytes_ConcatAndDel(&s,
temp);
829 PyTuple_SET_ITEM(pieces, 0, s);
833 s = PyBytes_FromString(
"]");
836 temp = PyTuple_GET_ITEM(pieces, n-1);
837 PyBytes_ConcatAndDel(&
temp, s);
838 PyTuple_SET_ITEM(pieces, n-1,
temp);
843 s = PyBytes_FromString(
", ");
846 result = _PyBytes_Join(s, pieces);
866 PyObject *pieces = NULL, *
result = NULL;
867 PyListObject *v = (PyListObject *)
list;
869 i = Py_ReprEnter((PyObject *)v);
872 PyErr_SetString(
JSON_EncodeError,
"a list with references to itself is not JSON encodable");
877 if (Py_SIZE(v) == 0) {
878 result = PyBytes_FromString(
"[]");
882 pieces = PyList_New(0);
888 for (i = 0; i < Py_SIZE(v); ++i) {
893 status = PyList_Append(pieces, s);
900 assert(PyList_GET_SIZE(pieces) > 0);
901 s = PyBytes_FromString(
"[");
904 temp = PyList_GET_ITEM(pieces, 0);
905 PyBytes_ConcatAndDel(&s,
temp);
906 PyList_SET_ITEM(pieces, 0, s);
910 s = PyBytes_FromString(
"]");
913 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
914 PyBytes_ConcatAndDel(&
temp, s);
915 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1,
temp);
920 s = PyBytes_FromString(
", ");
923 result = _PyBytes_Join(s, pieces);
928 Py_ReprLeave((PyObject *)v);
944 PyObject *s, *
temp, *colon = NULL;
945 PyObject *pieces = NULL, *
result = NULL;
946 PyObject *key, *value;
947 PyDictObject *mp = (PyDictObject *)
dict;
949 i = Py_ReprEnter((PyObject *)mp);
953 "itself is not JSON encodable");
958 if (mp->ma_used == 0) {
959 result = PyBytes_FromString(
"{}");
963 pieces = PyList_New(0);
967 colon = PyBytes_FromString(
": ");
974 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
977 if (!PyBytes_Check(key) && !PyUnicode_Check(key)) {
978 PyErr_SetString(
JSON_EncodeError,
"JSON encodable dictionaries must have string/unicode keys");
985 PyBytes_Concat(&s, colon);
990 status = PyList_Append(pieces, s);
997 assert(PyList_GET_SIZE(pieces) > 0);
998 s = PyBytes_FromString(
"{");
1001 temp = PyList_GET_ITEM(pieces, 0);
1002 PyBytes_ConcatAndDel(&s,
temp);
1003 PyList_SET_ITEM(pieces, 0, s);
1007 s = PyBytes_FromString(
"}");
1010 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
1011 PyBytes_ConcatAndDel(&
temp, s);
1012 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1,
temp);
1017 s = PyBytes_FromString(
", ");
1020 result = _PyBytes_Join(s, pieces);
1026 Py_ReprLeave((PyObject *)mp);
1031 if (
object == Py_True) {
1032 return PyBytes_FromString(
"true");
1033 }
else if (
object == Py_False) {
1034 return PyBytes_FromString(
"false");
1035 }
else if (
object == Py_None) {
1036 return PyBytes_FromString(
"null");
1037 }
else if (PyBytes_Check(
object)) {
1039 }
else if (PyUnicode_Check(
object)) {
1041 }
else if (PyFloat_Check(
object)) {
1042 double val = PyFloat_AS_DOUBLE(
object);
1044 return PyBytes_FromString(
"NaN");
1045 }
else if (Py_IS_INFINITY(val)) {
1047 return PyBytes_FromString(
"Infinity");
1049 return PyBytes_FromString(
"-Infinity");
1055 else if (PyLong_Check(
object)) {
1058 else if (PyList_Check(
object)) {
1060 }
else if (PyTuple_Check(
object)) {
1062 }
else if (PyDict_Check(
object)) {
1080 static char *kwlist[] = { (
char*)
"json", (
char*)
"all_unicode", NULL };
1081 int all_unicode =
True;
1086 if (!PyArg_ParseTupleAndKeywords(
args, kwargs,
"O|i:decode", kwlist, &
string, &all_unicode))
1089 if (PyUnicode_Check(
string)) {
1093 str = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(
string),
1094 PyUnicode_GET_SIZE(
string));
1097 str = PyUnicode_AsRawUnicodeEscapeString(
string);
1107 if (PyBytes_AsStringAndSize(
str, &(jsondata.
str), NULL) == -1) {
1112 jsondata.
ptr = jsondata.
str;
1113 jsondata.
end = jsondata.
str+strlen(jsondata.
str);
1118 if (
object != NULL) {
1120 if (jsondata.
ptr < jsondata.
end) {
1137 PyDoc_STR(
"encode(object) -> generate the JSON representation for object.") },
1140 {
"decode", (PyCFunction)
JSON_decode, METH_VARARGS|METH_KEYWORDS,
1141 PyDoc_STR(
"decode(string, all_unicode=True) -> parse the JSON representation into\n"
1142 "python objects. The optional argument `all_unicode', specifies how to\n"
1143 "convert the strings in the JSON representation into python objects.\n"
1144 "If it is False (default on Python 2.x), it will return strings/bytes\n"
1145 "everywhere possible and unicode objects only where necessary, else\n"
1146 "it will return unicode objects everywhere (this is slower, but default\n"
1149 { NULL, NULL, 0, NULL }
1152 PyDoc_STRVAR(module_doc,
"Fast JSON encoder/decoder module.");
1154 #define MODULE_VERSION "1.0.5"
1157 JSON_Error = PyErr_NewException(
"cjson.Error", NULL, NULL);
1181 PyModuleDef_HEAD_INIT,
1207 #pragma GCC diagnostic pop