43 #if PY_VERSION_HEX < 0x02050000
46 #define PY_SSIZE_T_MAX INT_MAX
47 #define PY_SSIZE_T_MIN INT_MIN
49 #define SSIZE_T_F "%d"
51 #define SSIZE_T_F "%zd"
58 #define INFINITY HUGE_VAL
62 #define NAN (HUGE_VAL-HUGE_VAL)
66 #define Py_IS_NAN(X) ((X) != (X))
69 #define skipSpaces(d) while (*((d)->ptr) && isspace(*((d)->ptr))) (d)->ptr++
73 static void getRowAndCol(
char *begin,
char *current,
int *row,
int *col) {
76 while (current > begin) {
87 left = jsondata->
end-jsondata->
ptr;
89 if (left >= 4 && strncmp(jsondata->
ptr,
"null", 4) == 0) {
105 left = jsondata->
end-jsondata->
ptr;
107 if (left >= 4 && strncmp(jsondata->
ptr,
"true", 4) == 0) {
111 }
else if (left >= 5 && strncmp(jsondata->
ptr,
"false", 5) == 0) {
126 int c, escaping, has_unicode, string_escape;
131 escaping = has_unicode = string_escape =
False;
132 ptr = jsondata->
ptr+1;
146 }
else if (c ==
'"') {
148 }
else if (!isascii(c)) {
163 string_escape =
True;
171 len = ptr-jsondata->
ptr-1;
174 object = PyUnicode_DecodeUnicodeEscape(jsondata->
ptr+1, len, NULL);
175 else if (string_escape)
176 object = PyString_DecodeEscape(jsondata->
ptr+1, len, NULL, 0, NULL);
178 object = PyString_FromStringAndSize(jsondata->
ptr+1, len);
180 if (
object == NULL) {
181 PyObject *type, *value, *tb, *reason;
183 PyErr_Fetch(&type, &value, &tb);
191 if (PyErr_GivenExceptionMatches(type, PyExc_UnicodeDecodeError)) {
194 reason = PyObject_GetAttrString(value,
"reason");
198 reason ? PyString_AsString(reason) :
"bad format");
212 jsondata->
ptr = ptr+1;
222 left = jsondata->
end-jsondata->
ptr;
224 if (left >= 8 && strncmp(jsondata->
ptr,
"Infinity", 8) == 0) {
226 object = PyFloat_FromDouble(
INFINITY);
228 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"+Infinity", 9) == 0) {
230 object = PyFloat_FromDouble(
INFINITY);
232 }
else if (left >= 9 && strncmp(jsondata->
ptr,
"-Infinity", 9) == 0) {
234 object = PyFloat_FromDouble(-
INFINITY);
249 left = jsondata->
end-jsondata->
ptr;
251 if (left >= 3 && strncmp(jsondata->
ptr,
"NaN", 3) == 0) {
253 object = PyFloat_FromDouble(
NAN);
265 int c, is_float, should_stop;
270 is_float = should_stop =
False;
305 str = PyString_FromStringAndSize(jsondata->
ptr, ptr-jsondata->
ptr);
310 object = PyFloat_FromString(str, NULL);
312 object = PyInt_FromString(PyString_AS_STRING(str), NULL, 10);
317 if (
object == NULL) {
331 int c, expect_item, items, result;
334 object = PyList_New(0);
336 start = jsondata->
ptr;
350 }
else if (c ==
']') {
351 if (expect_item && items > 0) {
361 }
else if (c ==
',') {
377 result = PyList_Append(
object, item);
394 PyObject *
object, *key, *value;
395 int c, expect_key, items, result;
398 object = PyDict_New();
402 start = jsondata->
ptr;
415 }
else if (c ==
'}') {
416 if (expect_key && items > 0) {
426 }
else if (c ==
',') {
453 if (*jsondata->
ptr !=
':') {
471 result = PyDict_SetItem(
object, key, value);
492 switch (*jsondata->
ptr) {
528 if (*(jsondata->
ptr+1) ==
'I') {
567 register PyStringObject *op = (PyStringObject *)
string;
568 size_t newsize = 2+6*op->ob_size;
572 PyErr_SetString(PyExc_OverflowError,
"string is too large to make repr");
575 v = PyString_FromStringAndSize((
char *)NULL, newsize);
586 p = PyString_AS_STRING(v);
588 for (i = 0; i < op->ob_size; i++) {
591 assert(newsize-(p-PyString_AS_STRING(v)) >= 7);
593 if (c == quote || c ==
'\\')
594 *p++ =
'\\', *p++ = c;
596 *p++ =
'\\', *p++ =
't';
598 *p++ =
'\\', *p++ =
'n';
600 *p++ =
'\\', *p++ =
'r';
602 *p++ =
'\\', *p++ =
'f';
604 *p++ =
'\\', *p++ =
'b';
605 else if (c < ' ' || c >= 0x7f) {
609 sprintf(p,
"\\u%04x", c&0xff);
615 assert(newsize-(p-PyString_AS_STRING(v)) >= 1);
618 _PyString_Resize(&v, (
int) (p-PyString_AS_STRING(v)));
636 static const char *hexdigit =
"0123456789abcdef";
638 s = PyUnicode_AS_UNICODE(unicode);
639 size = PyUnicode_GET_SIZE(unicode);
642 PyErr_SetString(PyExc_OverflowError,
"unicode object is too large to make repr");
646 repr = PyString_FromStringAndSize(NULL, 2+6*size+1);
650 p = PyString_AS_STRING(repr);
655 Py_UNICODE ch = *s++;
658 if ((ch == (Py_UNICODE)PyString_AS_STRING(repr)[0] || ch ==
'\\')) {
662 #ifdef Py_UNICODE_WIDE
664 }
else if (ch >= 0x10000) {
665 int offset = p-PyString_AS_STRING(repr);
668 if (offset+12 > PyString_GET_SIZE(repr)) {
669 if (_PyString_Resize(&repr, PyString_GET_SIZE(repr)+100))
671 p = PyString_AS_STRING(repr)+offset;
676 *p++ = hexdigit[(ch>>28)&0x0000000F];
677 *p++ = hexdigit[(ch>>24)&0x0000000F];
678 *p++ = hexdigit[(ch>>20)&0x0000000F];
679 *p++ = hexdigit[(ch>>16)&0x0000000F];
680 *p++ = hexdigit[(ch>>12)&0x0000000F];
681 *p++ = hexdigit[(ch>>8)&0x0000000F];
682 *p++ = hexdigit[(ch>>4)&0x0000000F];
683 *p++ = hexdigit[ch&0x0000000F];
687 }
else if (ch >= 0xD800 && ch < 0xDC00) {
693 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
694 ucs = (((ch&0x03FF)<<10)|(ch2&0x03FF))+0x00010000;
697 *p++ = hexdigit[(ucs>>28)&0x0000000F];
698 *p++ = hexdigit[(ucs>>24)&0x0000000F];
699 *p++ = hexdigit[(ucs>>20)&0x0000000F];
700 *p++ = hexdigit[(ucs>>16)&0x0000000F];
701 *p++ = hexdigit[(ucs>>12)&0x0000000F];
702 *p++ = hexdigit[(ucs>>8)&0x0000000F];
703 *p++ = hexdigit[(ucs>>4)&0x0000000F];
704 *p++ = hexdigit[ucs&0x0000000F];
714 *p++ = hexdigit[(ch>>12)&0x000F];
715 *p++ = hexdigit[(ch>>8)&0x000F];
716 *p++ = hexdigit[(ch>>4)&0x000F];
717 *p++ = hexdigit[ch&0x000F];
719 }
else if (ch ==
'\t') {
722 }
else if (ch ==
'\n') {
725 }
else if (ch ==
'\r') {
728 }
else if (ch ==
'\f') {
731 }
else if (ch ==
'\b') {
737 else if (ch < ' ' || ch >= 0x7F) {
742 *p++ = hexdigit[(ch>>4)&0x000F];
743 *p++ = hexdigit[ch&0x000F];
751 *p++ = PyString_AS_STRING(repr)[0];
754 _PyString_Resize(&repr, p-PyString_AS_STRING(repr));
769 PyObject *pieces, *result = NULL;
770 PyTupleObject *v = (PyTupleObject *)tuple;
774 return PyString_FromString(
"[]");
776 pieces = PyTuple_New(n);
781 for (i = 0; i < n; ++i) {
785 PyTuple_SET_ITEM(pieces, i, s);
790 s = PyString_FromString(
"[");
793 temp = PyTuple_GET_ITEM(pieces, 0);
794 PyString_ConcatAndDel(&s, temp);
795 PyTuple_SET_ITEM(pieces, 0, s);
799 s = PyString_FromString(
"]");
802 temp = PyTuple_GET_ITEM(pieces, n-1);
803 PyString_ConcatAndDel(&temp, s);
804 PyTuple_SET_ITEM(pieces, n-1, temp);
809 s = PyString_FromString(
", ");
812 result = _PyString_Join(s, pieces);
832 PyObject *pieces = NULL, *result = NULL;
833 PyListObject *v = (PyListObject *)list;
835 i = Py_ReprEnter((PyObject *)v);
838 PyErr_SetString(
JSON_EncodeError,
"a list with references to itself is not JSON encodable");
843 if (v->ob_size == 0) {
844 result = PyString_FromString(
"[]");
848 pieces = PyList_New(0);
854 for (i = 0; i < v->ob_size; ++i) {
859 status = PyList_Append(pieces, s);
866 assert(PyList_GET_SIZE(pieces) > 0);
867 s = PyString_FromString(
"[");
870 temp = PyList_GET_ITEM(pieces, 0);
871 PyString_ConcatAndDel(&s, temp);
872 PyList_SET_ITEM(pieces, 0, s);
876 s = PyString_FromString(
"]");
879 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
880 PyString_ConcatAndDel(&temp, s);
881 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1, temp);
886 s = PyString_FromString(
", ");
889 result = _PyString_Join(s, pieces);
894 Py_ReprLeave((PyObject *)v);
910 PyObject *s, *temp, *colon = NULL;
911 PyObject *pieces = NULL, *result = NULL;
912 PyObject *key, *value;
913 PyDictObject *mp = (PyDictObject *)dict;
915 i = Py_ReprEnter((PyObject *)mp);
919 "itself is not JSON encodable");
924 if (mp->ma_used == 0) {
925 result = PyString_FromString(
"{}");
929 pieces = PyList_New(0);
933 colon = PyString_FromString(
": ");
940 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
943 if (!PyString_Check(key) && !PyUnicode_Check(key)) {
944 PyErr_SetString(
JSON_EncodeError,
"JSON encodable dictionaries must have string/unicode keys");
951 PyString_Concat(&s, colon);
956 status = PyList_Append(pieces, s);
963 assert(PyList_GET_SIZE(pieces) > 0);
964 s = PyString_FromString(
"{");
967 temp = PyList_GET_ITEM(pieces, 0);
968 PyString_ConcatAndDel(&s, temp);
969 PyList_SET_ITEM(pieces, 0, s);
973 s = PyString_FromString(
"}");
976 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces)-1);
977 PyString_ConcatAndDel(&temp, s);
978 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces)-1, temp);
983 s = PyString_FromString(
", ");
986 result = _PyString_Join(s, pieces);
992 Py_ReprLeave((PyObject *)mp);
997 if (
object == Py_True) {
998 return PyString_FromString(
"true");
999 }
else if (
object == Py_False) {
1000 return PyString_FromString(
"false");
1001 }
else if (
object == Py_None) {
1002 return PyString_FromString(
"null");
1003 }
else if (PyString_Check(
object)) {
1005 }
else if (PyUnicode_Check(
object)) {
1007 }
else if (PyFloat_Check(
object)) {
1008 double val = PyFloat_AS_DOUBLE(
object);
1010 return PyString_FromString(
"NaN");
1011 }
else if (Py_IS_INFINITY(val)) {
1013 return PyString_FromString(
"Infinity");
1015 return PyString_FromString(
"-Infinity");
1018 return PyObject_Str(
object);
1020 }
else if (PyInt_Check(
object) || PyLong_Check(
object)) {
1021 return PyObject_Str(
object);
1022 }
else if (PyList_Check(
object)) {
1024 }
else if (PyTuple_Check(
object)) {
1026 }
else if (PyDict_Check(
object)) {
1042 static PyObject *
JSON_decode(PyObject *
self, PyObject *args, PyObject *kwargs) {
1043 static char *kwlist[] = {
"json",
"all_unicode", NULL };
1044 int all_unicode =
False;
1045 PyObject *
object, *string, *str;
1048 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O|i:decode", kwlist, &
string, &all_unicode))
1051 if (PyUnicode_Check(
string)) {
1052 str = PyUnicode_AsRawUnicodeEscapeString(
string);
1061 if (PyString_AsStringAndSize(str, &(jsondata.
str), NULL) == -1) {
1066 jsondata.
ptr = jsondata.
str;
1067 jsondata.
end = jsondata.
str+strlen(jsondata.
str);
1072 if (
object != NULL) {
1074 if (jsondata.
ptr < jsondata.
end) {
1091 PyDoc_STR(
"encode(object) -> generate the JSON representation for object.") },
1093 {
"decode", (PyCFunction)
JSON_decode, METH_VARARGS|METH_KEYWORDS,
1094 PyDoc_STR(
"decode(string, all_unicode=False) -> parse the JSON representation into\n"
1095 "python objects. The optional argument `all_unicode', specifies how to\n"
1096 "convert the strings in the JSON representation into python objects.\n"
1097 "If it is False (default), it will return strings everywhere possible\n"
1098 "and unicode objects only where necessary, else it will return unicode\n"
1099 "objects everywhere (this is slower).") },
1101 { NULL, NULL, 0, NULL }
1104 PyDoc_STRVAR(module_doc,
"Fast JSON encoder/decoder module.");
1107 #define MODULE_VERSION "1.0.5"
1116 JSON_Error = PyErr_NewException(
"cjson.Error", NULL, NULL);
static PyObject * decode_number(JSONData *jsondata)
PyDoc_STRVAR(module_doc,"Fast JSON encoder/decoder module.")
static PyObject * decode_nan(JSONData *jsondata)
static PyObject * decode_object(JSONData *jsondata)
static PyObject * decode_array(JSONData *jsondata)
static PyObject * JSON_Error
static PyObject * encode_unicode(PyObject *object)
PyMODINIT_FUNC initcjson(void)
static PyObject * encode_dict(PyObject *object)
static PyObject * encode_list(PyObject *object)
static void getRowAndCol(char *begin, char *current, int *row, int *col)
static PyMethodDef cjson_methods[]
static PyObject * decode_inf(JSONData *jsondata)
static PyObject * JSON_encode(PyObject *self, PyObject *object)
static PyObject * decode_bool(JSONData *jsondata)
static PyObject * decode_null(JSONData *jsondata)
static PyObject * JSON_EncodeError
static PyObject * decode_json(JSONData *jsondata)
static PyObject * encode_tuple(PyObject *object)
static PyObject * encode_object(PyObject *object)
static PyObject * JSON_DecodeError
static PyObject * JSON_decode(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * decode_string(JSONData *jsondata)
static PyObject * encode_string(PyObject *object)