45 #if PY_VERSION_HEX < 0x02050000
48 #define PY_SSIZE_T_MAX INT_MAX
49 #define PY_SSIZE_T_MIN INT_MIN
51 #define SSIZE_T_F "%d"
53 #define SSIZE_T_F "%zd"
60 #define INFINITY HUGE_VAL
64 #define NAN (HUGE_VAL-HUGE_VAL)
68 #define Py_IS_NAN(X) ((X) != (X))
71 #define skipSpaces(d) while (*((d)->ptr) && isspace(*((d)->ptr))) (d)->ptr++
77 PyObject *tmp_str, *
result;
78 tmp_str = PyObject_Str(
obj);
81 result = PyUnicode_AsUTF8String(tmp_str);
102 left = jsondata->
end-jsondata->
ptr;
104 if (left >= 4 && strncmp(jsondata->
ptr,
"null", 4) == 0) {
120 left = jsondata->
end-jsondata->
ptr;
122 if (left >= 4 && strncmp(jsondata->
ptr,
"true", 4) == 0) {
126 }
else if (left >= 5 && strncmp(jsondata->
ptr,
"false", 5) == 0) {
141 int c, escaping, has_unicode, string_escape;
146 escaping = has_unicode = string_escape =
False;
147 ptr = jsondata->
ptr+1;
161 }
else if (
c ==
'"') {
163 }
else if (!isascii(
c)) {
178 string_escape =
True;
186 len = ptr-jsondata->
ptr-1;
189 object = PyUnicode_DecodeUnicodeEscape(jsondata->
ptr+1, len, NULL);
190 else if (string_escape)
191 object = PyBytes_DecodeEscape(jsondata->
ptr+1, len, NULL, 0, NULL);
193 object = PyBytes_FromStringAndSize(jsondata->
ptr+1, len);
195 if (
object == NULL) {
196 PyObject *
type, *value, *tb, *reason;
198 PyErr_Fetch(&
type, &value, &tb);
206 if (PyErr_GivenExceptionMatches(
type, PyExc_UnicodeDecodeError)) {
209 reason = PyObject_GetAttrString(value,
"reason");
211 if (reason && PyUnicode_Check(reason)) {
212 PyObject * bytes_reason = PyUnicode_AsUTF8String(reason);
214 reason = bytes_reason;
219 reason ? PyBytes_AsString(reason) :
"bad format");
233 jsondata->
ptr = ptr+1;
243 left = jsondata->
end-jsondata->
ptr;
245 if (left >= 8 && strncmp(jsondata->
ptr,
"Infinity", 8) == 0) {
247 object = PyFloat_FromDouble(
INFINITY);
249 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"+Infinity", 9) == 0) {
251 object = PyFloat_FromDouble(
INFINITY);
253 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"-Infinity", 9) == 0) {
255 object = PyFloat_FromDouble(-
INFINITY);
270 left = jsondata->
end-jsondata->
ptr;
272 if (left >= 3 && strncmp(jsondata->
ptr,
"NaN", 3) == 0) {
274 object = PyFloat_FromDouble(
NAN);
286 int c, is_float, should_stop;
291 is_float = should_stop =
False;
326 str = PyBytes_FromStringAndSize(jsondata->
ptr, ptr-jsondata->
ptr);
331 object = PyFloat_FromString(
str);
333 object = PyLong_FromString(PyBytes_AS_STRING(
str), NULL, 10);
338 if (
object == NULL) {
352 int c, expect_item, items,
result;
355 object = PyList_New(0);
357 start = jsondata->
ptr;
371 }
else if (
c ==
']') {
372 if (expect_item && items > 0) {
382 }
else if (
c ==
',') {
415 PyObject *
object, *key, *value;
416 int c, expect_key, items,
result;
419 object = PyDict_New();
423 start = jsondata->
ptr;
436 }
else if (
c ==
'}') {
437 if (expect_key && items > 0) {
447 }
else if (
c ==
',') {
474 if (*jsondata->
ptr !=
':') {
492 result = PyDict_SetItem(
object, key, value);
513 switch (*jsondata->
ptr) {
549 if (*(jsondata->
ptr+1) ==
'I') {
588 register PyBytesObject *
op = (PyBytesObject *)
string;
589 size_t newsize = 2+6*Py_SIZE(
op);
593 PyErr_SetString(PyExc_OverflowError,
"string is too large to make repr");
596 v = PyBytes_FromStringAndSize((
char *)NULL, newsize);
607 p = PyBytes_AS_STRING(v);
609 for (i = 0; i < Py_SIZE(
op); i++) {
612 assert(newsize-(p-PyBytes_AS_STRING(v)) >= 7);
615 *p++ =
'\\', *p++ =
c;
617 *p++ =
'\\', *p++ =
't';
619 *p++ =
'\\', *p++ =
'n';
621 *p++ =
'\\', *p++ =
'r';
623 *p++ =
'\\', *p++ =
'f';
625 *p++ =
'\\', *p++ =
'b';
626 else if (c < ' ' || c >= 0x7f) {
630 sprintf(p,
"\\u%04x",
c&0xff);
636 assert(newsize-(p-PyBytes_AS_STRING(v)) >= 1);
639 _PyBytes_Resize(&v, (
int) (p-PyBytes_AS_STRING(v)));
657 static const char *hexdigit =
"0123456789abcdef";
658 #ifdef Py_UNICODE_WIDE
664 s = PyUnicode_AS_UNICODE(unicode);
665 size = PyUnicode_GET_SIZE(unicode);
668 PyErr_SetString(PyExc_OverflowError,
"unicode object is too large to make repr");
672 repr = PyByteArray_FromStringAndSize(NULL,
679 p = PyByteArray_AS_STRING(repr);
684 Py_UNICODE ch = *s++;
687 if ((ch == (Py_UNICODE)PyByteArray_AS_STRING(repr)[0] || ch ==
'\\')) {
692 #ifdef Py_UNICODE_WIDE
694 else if (ch >= 0x10000) {
697 *p++ = hexdigit[(ch>>28)&0x0000000F];
698 *p++ = hexdigit[(ch>>24)&0x0000000F];
699 *p++ = hexdigit[(ch>>20)&0x0000000F];
700 *p++ = hexdigit[(ch>>16)&0x0000000F];
701 *p++ = hexdigit[(ch>>12)&0x0000000F];
702 *p++ = hexdigit[(ch>>8)&0x0000000F];
703 *p++ = hexdigit[(ch>>4)&0x0000000F];
704 *p++ = hexdigit[ch&0x0000000F];
709 else if (ch >= 0xD800 && ch < 0xDC00) {
715 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
716 ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
719 *p++ = hexdigit[(ucs>>28)&0x0000000F];
720 *p++ = hexdigit[(ucs>>24)&0x0000000F];
721 *p++ = hexdigit[(ucs>>20)&0x0000000F];
722 *p++ = hexdigit[(ucs>>16)&0x0000000F];
723 *p++ = hexdigit[(ucs>>12)&0x0000000F];
724 *p++ = hexdigit[(ucs>>8)&0x0000000F];
725 *p++ = hexdigit[(ucs>>4)&0x0000000F];
726 *p++ = hexdigit[ucs&0x0000000F];
738 *p++ = hexdigit[(ch>>12)&0x000F];
739 *p++ = hexdigit[(ch>>8)&0x000F];
740 *p++ = hexdigit[(ch>>4)&0x000F];
741 *p++ = hexdigit[ch&0x000F];
743 }
else if (ch ==
'\t') {
746 }
else if (ch ==
'\n') {
749 }
else if (ch ==
'\r') {
752 }
else if (ch ==
'\f') {
755 }
else if (ch ==
'\b') {
761 else if (ch < ' ' || ch >= 0x7F) {
766 *p++ = hexdigit[(ch>>4)&0x000F];
767 *p++ = hexdigit[ch&0x000F];
775 *p++ = PyByteArray_AS_STRING(repr)[0];
779 PyObject *
result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr),
780 p - PyByteArray_AS_STRING(repr));
797 PyObject *pieces, *
result = NULL;
798 PyTupleObject *v = (PyTupleObject *)tuple;
802 return PyBytes_FromString(
"[]");
804 pieces = PyTuple_New(
n);
809 for (i = 0; i <
n; ++i) {
813 PyTuple_SET_ITEM(pieces, i, s);
818 s = PyBytes_FromString(
"[");
821 temp = PyTuple_GET_ITEM(pieces, 0);
822 PyBytes_ConcatAndDel(&s,
temp);
823 PyTuple_SET_ITEM(pieces, 0, s);
827 s = PyBytes_FromString(
"]");
830 temp = PyTuple_GET_ITEM(pieces,
n-1);
831 PyBytes_ConcatAndDel(&
temp, s);
832 PyTuple_SET_ITEM(pieces,
n-1,
temp);
837 s = PyBytes_FromString(
", ");
840 result = _PyBytes_Join(s, pieces);
860 PyObject *pieces = NULL, *
result = NULL;
861 PyListObject *v = (PyListObject *)
list;
863 i = Py_ReprEnter((PyObject *)v);
866 PyErr_SetString(
JSON_EncodeError,
"a list with references to itself is not JSON encodable");
871 if (Py_SIZE(v) == 0) {
872 result = PyBytes_FromString(
"[]");
876 pieces = PyList_New(0);
882 for (i = 0; i < Py_SIZE(v); ++i) {
887 status = PyList_Append(pieces, s);
894 assert(PyList_GET_SIZE(pieces) > 0);
895 s = PyBytes_FromString(
"[");
898 temp = PyList_GET_ITEM(pieces, 0);
899 PyBytes_ConcatAndDel(&s,
temp);
900 PyList_SET_ITEM(pieces, 0, s);
904 s = PyBytes_FromString(
"]");
907 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
908 PyBytes_ConcatAndDel(&
temp, s);
909 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1,
temp);
914 s = PyBytes_FromString(
", ");
917 result = _PyBytes_Join(s, pieces);
922 Py_ReprLeave((PyObject *)v);
938 PyObject *s, *
temp, *colon = NULL;
939 PyObject *pieces = NULL, *
result = NULL;
940 PyObject *key, *value;
941 PyDictObject *mp = (PyDictObject *)
dict;
943 i = Py_ReprEnter((PyObject *)mp);
947 "itself is not JSON encodable");
952 if (mp->ma_used == 0) {
953 result = PyBytes_FromString(
"{}");
957 pieces = PyList_New(0);
961 colon = PyBytes_FromString(
": ");
968 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
971 if (!PyBytes_Check(key) && !PyUnicode_Check(key)) {
972 PyErr_SetString(
JSON_EncodeError,
"JSON encodable dictionaries must have string/unicode keys");
979 PyBytes_Concat(&s, colon);
984 status = PyList_Append(pieces, s);
991 assert(PyList_GET_SIZE(pieces) > 0);
992 s = PyBytes_FromString(
"{");
995 temp = PyList_GET_ITEM(pieces, 0);
996 PyBytes_ConcatAndDel(&s,
temp);
997 PyList_SET_ITEM(pieces, 0, s);
1001 s = PyBytes_FromString(
"}");
1004 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
1005 PyBytes_ConcatAndDel(&
temp, s);
1006 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1,
temp);
1011 s = PyBytes_FromString(
", ");
1014 result = _PyBytes_Join(s, pieces);
1020 Py_ReprLeave((PyObject *)mp);
1025 if (
object == Py_True) {
1026 return PyBytes_FromString(
"true");
1027 }
else if (
object == Py_False) {
1028 return PyBytes_FromString(
"false");
1029 }
else if (
object == Py_None) {
1030 return PyBytes_FromString(
"null");
1031 }
else if (PyBytes_Check(
object)) {
1033 }
else if (PyUnicode_Check(
object)) {
1035 }
else if (PyFloat_Check(
object)) {
1036 double val = PyFloat_AS_DOUBLE(
object);
1038 return PyBytes_FromString(
"NaN");
1039 }
else if (Py_IS_INFINITY(val)) {
1041 return PyBytes_FromString(
"Infinity");
1043 return PyBytes_FromString(
"-Infinity");
1049 else if (PyLong_Check(
object)) {
1052 else if (PyList_Check(
object)) {
1054 }
else if (PyTuple_Check(
object)) {
1056 }
else if (PyDict_Check(
object)) {
1074 static char *kwlist[] = {
"json",
"all_unicode", NULL };
1075 int all_unicode =
True;
1080 if (!PyArg_ParseTupleAndKeywords(
args, kwargs,
"O|i:decode", kwlist, &
string, &all_unicode))
1083 if (PyUnicode_Check(
string)) {
1087 str = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(
string),
1088 PyUnicode_GET_SIZE(
string));
1091 str = PyUnicode_AsRawUnicodeEscapeString(
string);
1101 if (PyBytes_AsStringAndSize(
str, &(jsondata.
str), NULL) == -1) {
1106 jsondata.
ptr = jsondata.
str;
1107 jsondata.
end = jsondata.
str+strlen(jsondata.
str);
1112 if (
object != NULL) {
1114 if (jsondata.
ptr < jsondata.
end) {
1131 PyDoc_STR(
"encode(object) -> generate the JSON representation for object.") },
1134 {
"decode", (PyCFunction)
JSON_decode, METH_VARARGS|METH_KEYWORDS,
1135 PyDoc_STR(
"decode(string, all_unicode=True) -> parse the JSON representation into\n"
1136 "python objects. The optional argument `all_unicode', specifies how to\n"
1137 "convert the strings in the JSON representation into python objects.\n"
1138 "If it is False (default on Python 2.x), it will return strings/bytes\n"
1139 "everywhere possible and unicode objects only where necessary, else\n"
1140 "it will return unicode objects everywhere (this is slower, but default\n"
1143 { NULL, NULL, 0, NULL }
1146 PyDoc_STRVAR(module_doc,
"Fast JSON encoder/decoder module.");
1148 #define MODULE_VERSION "1.0.5"
1151 JSON_Error = PyErr_NewException(
"cjson.Error", NULL, NULL);
1175 PyModuleDef_HEAD_INIT,