44 #if PY_VERSION_HEX < 0x02050000
47 #define PY_SSIZE_T_MAX INT_MAX
48 #define PY_SSIZE_T_MIN INT_MIN
50 #define SSIZE_T_F "%d"
52 #define SSIZE_T_F "%zd"
59 #define INFINITY HUGE_VAL
63 #define NAN (HUGE_VAL-HUGE_VAL)
67 #define Py_IS_NAN(X) ((X) != (X))
70 #define skipSpaces(d) while (*((d)->ptr) && isspace(*((d)->ptr))) (d)->ptr++
76 PyObject *tmp_str, *
result;
77 tmp_str = PyObject_Str(
obj);
80 result = PyUnicode_AsUTF8String(tmp_str);
101 left = jsondata->
end-jsondata->
ptr;
103 if (left >= 4 && strncmp(jsondata->
ptr,
"null", 4) == 0) {
119 left = jsondata->
end-jsondata->
ptr;
121 if (left >= 4 && strncmp(jsondata->
ptr,
"true", 4) == 0) {
125 }
else if (left >= 5 && strncmp(jsondata->
ptr,
"false", 5) == 0) {
140 int c, escaping, has_unicode, string_escape;
145 escaping = has_unicode = string_escape =
False;
146 ptr = jsondata->
ptr+1;
160 }
else if (
c ==
'"') {
162 }
else if (!isascii(
c)) {
177 string_escape =
True;
185 len = ptr-jsondata->
ptr-1;
188 object = PyUnicode_DecodeUnicodeEscape(jsondata->
ptr+1, len, NULL);
189 else if (string_escape)
190 object = PyBytes_DecodeEscape(jsondata->
ptr+1, len, NULL, 0, NULL);
192 object = PyBytes_FromStringAndSize(jsondata->
ptr+1, len);
194 if (
object == NULL) {
205 if (PyErr_GivenExceptionMatches(
type, PyExc_UnicodeDecodeError)) {
208 reason = PyObject_GetAttrString(
value,
"reason");
210 if (reason && PyUnicode_Check(reason)) {
211 PyObject * bytes_reason = PyUnicode_AsUTF8String(reason);
213 reason = bytes_reason;
218 reason ? PyBytes_AsString(reason) :
"bad format");
232 jsondata->
ptr = ptr+1;
242 left = jsondata->
end-jsondata->
ptr;
244 if (left >= 8 && strncmp(jsondata->
ptr,
"Infinity", 8) == 0) {
246 object = PyFloat_FromDouble(
INFINITY);
248 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"+Infinity", 9) == 0) {
250 object = PyFloat_FromDouble(
INFINITY);
252 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"-Infinity", 9) == 0) {
254 object = PyFloat_FromDouble(-
INFINITY);
269 left = jsondata->
end-jsondata->
ptr;
271 if (left >= 3 && strncmp(jsondata->
ptr,
"NaN", 3) == 0) {
273 object = PyFloat_FromDouble(
NAN);
285 int c, is_float, should_stop;
290 is_float = should_stop =
False;
325 str = PyBytes_FromStringAndSize(jsondata->
ptr, ptr-jsondata->
ptr);
330 object = PyFloat_FromString(
str);
332 object = PyLong_FromString(PyBytes_AS_STRING(
str), NULL, 10);
337 if (
object == NULL) {
351 int c, expect_item, items,
result;
354 object = PyList_New(0);
356 start = jsondata->
ptr;
370 }
else if (
c ==
']') {
371 if (expect_item && items > 0) {
381 }
else if (
c ==
',') {
415 int c, expect_key, items,
result;
418 object = PyDict_New();
422 start = jsondata->
ptr;
435 }
else if (
c ==
'}') {
436 if (expect_key && items > 0) {
446 }
else if (
c ==
',') {
473 if (*jsondata->
ptr !=
':') {
512 switch (*jsondata->
ptr) {
548 if (*(jsondata->
ptr+1) ==
'I') {
587 register PyBytesObject *
op = (PyBytesObject *)
string;
588 size_t newsize = 2+6*Py_SIZE(
op);
592 PyErr_SetString(PyExc_OverflowError,
"string is too large to make repr");
595 v = PyBytes_FromStringAndSize((
char *)NULL, newsize);
606 p = PyBytes_AS_STRING(v);
608 for (i = 0; i < Py_SIZE(
op); i++) {
611 assert(newsize-(p-PyBytes_AS_STRING(v)) >= 7);
613 if (
c == quote ||
c ==
'\\')
614 *p++ =
'\\', *p++ =
c;
616 *p++ =
'\\', *p++ =
't';
618 *p++ =
'\\', *p++ =
'n';
620 *p++ =
'\\', *p++ =
'r';
622 *p++ =
'\\', *p++ =
'f';
624 *p++ =
'\\', *p++ =
'b';
625 else if (c < ' ' || c >= 0x7f) {
629 sprintf(p,
"\\u%04x",
c&0xff);
635 assert(newsize-(p-PyBytes_AS_STRING(v)) >= 1);
638 _PyBytes_Resize(&v, (
int) (p-PyBytes_AS_STRING(v)));
656 static const char *hexdigit =
"0123456789abcdef";
657 #ifdef Py_UNICODE_WIDE
663 s = PyUnicode_AS_UNICODE(unicode);
664 size = PyUnicode_GET_SIZE(unicode);
667 PyErr_SetString(PyExc_OverflowError,
"unicode object is too large to make repr");
671 repr = PyByteArray_FromStringAndSize(NULL,
678 p = PyByteArray_AS_STRING(repr);
683 Py_UNICODE ch = *s++;
686 if ((ch == (Py_UNICODE)PyByteArray_AS_STRING(repr)[0] || ch ==
'\\')) {
691 #ifdef Py_UNICODE_WIDE
693 else if (ch >= 0x10000) {
696 *p++ = hexdigit[(ch>>28)&0x0000000F];
697 *p++ = hexdigit[(ch>>24)&0x0000000F];
698 *p++ = hexdigit[(ch>>20)&0x0000000F];
699 *p++ = hexdigit[(ch>>16)&0x0000000F];
700 *p++ = hexdigit[(ch>>12)&0x0000000F];
701 *p++ = hexdigit[(ch>>8)&0x0000000F];
702 *p++ = hexdigit[(ch>>4)&0x0000000F];
703 *p++ = hexdigit[ch&0x0000000F];
708 else if (ch >= 0xD800 && ch < 0xDC00) {
714 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
715 ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
718 *p++ = hexdigit[(ucs>>28)&0x0000000F];
719 *p++ = hexdigit[(ucs>>24)&0x0000000F];
720 *p++ = hexdigit[(ucs>>20)&0x0000000F];
721 *p++ = hexdigit[(ucs>>16)&0x0000000F];
722 *p++ = hexdigit[(ucs>>12)&0x0000000F];
723 *p++ = hexdigit[(ucs>>8)&0x0000000F];
724 *p++ = hexdigit[(ucs>>4)&0x0000000F];
725 *p++ = hexdigit[ucs&0x0000000F];
737 *p++ = hexdigit[(ch>>12)&0x000F];
738 *p++ = hexdigit[(ch>>8)&0x000F];
739 *p++ = hexdigit[(ch>>4)&0x000F];
740 *p++ = hexdigit[ch&0x000F];
742 }
else if (ch ==
'\t') {
745 }
else if (ch ==
'\n') {
748 }
else if (ch ==
'\r') {
751 }
else if (ch ==
'\f') {
754 }
else if (ch ==
'\b') {
760 else if (ch < ' ' || ch >= 0x7F) {
765 *p++ = hexdigit[(ch>>4)&0x000F];
766 *p++ = hexdigit[ch&0x000F];
774 *p++ = PyByteArray_AS_STRING(repr)[0];
778 PyObject *
result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr),
779 p - PyByteArray_AS_STRING(repr));
796 PyObject *pieces, *
result = NULL;
797 PyTupleObject *v = (PyTupleObject *)tuple;
801 return PyBytes_FromString(
"[]");
803 pieces = PyTuple_New(n);
808 for (i = 0; i < n; ++i) {
812 PyTuple_SET_ITEM(pieces, i, s);
817 s = PyBytes_FromString(
"[");
820 temp = PyTuple_GET_ITEM(pieces, 0);
821 PyBytes_ConcatAndDel(&s,
temp);
822 PyTuple_SET_ITEM(pieces, 0, s);
826 s = PyBytes_FromString(
"]");
829 temp = PyTuple_GET_ITEM(pieces, n-1);
830 PyBytes_ConcatAndDel(&
temp, s);
831 PyTuple_SET_ITEM(pieces, n-1,
temp);
836 s = PyBytes_FromString(
", ");
839 result = _PyBytes_Join(s, pieces);
859 PyObject *pieces = NULL, *
result = NULL;
860 PyListObject *v = (PyListObject *)
list;
862 i = Py_ReprEnter((PyObject *)v);
865 PyErr_SetString(
JSON_EncodeError,
"a list with references to itself is not JSON encodable");
870 if (Py_SIZE(v) == 0) {
871 result = PyBytes_FromString(
"[]");
875 pieces = PyList_New(0);
881 for (i = 0; i < Py_SIZE(v); ++i) {
886 status = PyList_Append(pieces, s);
893 assert(PyList_GET_SIZE(pieces) > 0);
894 s = PyBytes_FromString(
"[");
897 temp = PyList_GET_ITEM(pieces, 0);
898 PyBytes_ConcatAndDel(&s,
temp);
899 PyList_SET_ITEM(pieces, 0, s);
903 s = PyBytes_FromString(
"]");
906 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
907 PyBytes_ConcatAndDel(&
temp, s);
908 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1,
temp);
913 s = PyBytes_FromString(
", ");
916 result = _PyBytes_Join(s, pieces);
921 Py_ReprLeave((PyObject *)v);
937 PyObject *s, *
temp, *colon = NULL;
938 PyObject *pieces = NULL, *
result = NULL;
940 PyDictObject *mp = (PyDictObject *)
dict;
942 i = Py_ReprEnter((PyObject *)mp);
946 "itself is not JSON encodable");
951 if (mp->ma_used == 0) {
952 result = PyBytes_FromString(
"{}");
956 pieces = PyList_New(0);
960 colon = PyBytes_FromString(
": ");
967 while (PyDict_Next((PyObject *)mp, &i, &
key, &
value)) {
970 if (!PyBytes_Check(
key) && !PyUnicode_Check(
key)) {
971 PyErr_SetString(
JSON_EncodeError,
"JSON encodable dictionaries must have string/unicode keys");
978 PyBytes_Concat(&s, colon);
983 status = PyList_Append(pieces, s);
990 assert(PyList_GET_SIZE(pieces) > 0);
991 s = PyBytes_FromString(
"{");
994 temp = PyList_GET_ITEM(pieces, 0);
995 PyBytes_ConcatAndDel(&s,
temp);
996 PyList_SET_ITEM(pieces, 0, s);
1000 s = PyBytes_FromString(
"}");
1003 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
1004 PyBytes_ConcatAndDel(&
temp, s);
1005 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1,
temp);
1010 s = PyBytes_FromString(
", ");
1013 result = _PyBytes_Join(s, pieces);
1019 Py_ReprLeave((PyObject *)mp);
1024 if (
object == Py_True) {
1025 return PyBytes_FromString(
"true");
1026 }
else if (
object == Py_False) {
1027 return PyBytes_FromString(
"false");
1028 }
else if (
object == Py_None) {
1029 return PyBytes_FromString(
"null");
1030 }
else if (PyBytes_Check(
object)) {
1032 }
else if (PyUnicode_Check(
object)) {
1034 }
else if (PyFloat_Check(
object)) {
1035 double val = PyFloat_AS_DOUBLE(
object);
1037 return PyBytes_FromString(
"NaN");
1038 }
else if (Py_IS_INFINITY(val)) {
1040 return PyBytes_FromString(
"Infinity");
1042 return PyBytes_FromString(
"-Infinity");
1048 else if (PyLong_Check(
object)) {
1051 else if (PyList_Check(
object)) {
1053 }
else if (PyTuple_Check(
object)) {
1055 }
else if (PyDict_Check(
object)) {
1073 static char *kwlist[] = {
"json",
"all_unicode", NULL };
1074 int all_unicode =
True;
1079 if (!PyArg_ParseTupleAndKeywords(
args, kwargs,
"O|i:decode", kwlist, &
string, &all_unicode))
1082 if (PyUnicode_Check(
string)) {
1086 str = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(
string),
1087 PyUnicode_GET_SIZE(
string));
1090 str = PyUnicode_AsRawUnicodeEscapeString(
string);
1100 if (PyBytes_AsStringAndSize(
str, &(jsondata.
str), NULL) == -1) {
1105 jsondata.
ptr = jsondata.
str;
1106 jsondata.
end = jsondata.
str+strlen(jsondata.
str);
1111 if (
object != NULL) {
1113 if (jsondata.
ptr < jsondata.
end) {
1130 PyDoc_STR(
"encode(object) -> generate the JSON representation for object.") },
1133 {
"decode", (PyCFunction)
JSON_decode, METH_VARARGS|METH_KEYWORDS,
1134 PyDoc_STR(
"decode(string, all_unicode=True) -> parse the JSON representation into\n"
1135 "python objects. The optional argument `all_unicode', specifies how to\n"
1136 "convert the strings in the JSON representation into python objects.\n"
1137 "If it is False (default on Python 2.x), it will return strings/bytes\n"
1138 "everywhere possible and unicode objects only where necessary, else\n"
1139 "it will return unicode objects everywhere (this is slower, but default\n"
1142 { NULL, NULL, 0, NULL }
1145 PyDoc_STRVAR(module_doc,
"Fast JSON encoder/decoder module.");
1147 #define MODULE_VERSION "1.0.5"
1150 JSON_Error = PyErr_NewException(
"cjson.Error", NULL, NULL);
1174 PyModuleDef_HEAD_INIT,