diff --git a/python/JSONtoObj.c b/python/JSONtoObj.c index 9987f27..14338ee 100644 --- a/python/JSONtoObj.c +++ b/python/JSONtoObj.c @@ -74,6 +74,7 @@ static void Object_releaseObject(JSOBJ obj) PyObject* JSONToObj(PyObject* self, PyObject *arg) { PyObject *ret; + PyObject *sarg; JSONObjectDecoder decoder = { Object_newString, @@ -93,16 +94,35 @@ PyObject* JSONToObj(PyObject* self, PyObject *arg) PyObject_Realloc }; - if (!PyString_Check(arg)) + if (PyString_Check(arg)) { - PyErr_Format(PyExc_TypeError, "Expected string"); + sarg = arg; + } + else + if (PyUnicode_Check(arg)) + { + sarg = PyUnicode_AsUTF8String(arg); + if (sarg == NULL) + { + //Exception raised above us by codec according to docs + return NULL; + } + } + else + { + PyErr_Format(PyExc_TypeError, "Expected String or Unicode"); return NULL; } decoder.errorStr = NULL; decoder.errorOffset = NULL; - ret = JSON_DecodeObject(&decoder, PyString_AS_STRING(arg), PyString_GET_SIZE(arg)); + ret = JSON_DecodeObject(&decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg)); + + if (sarg != arg) + { + Py_DECREF(sarg); + } if (decoder.errorStr) { diff --git a/python/tests.py b/python/tests.py index 9de5fa1..0b20e71 100644 --- a/python/tests.py +++ b/python/tests.py @@ -221,7 +221,12 @@ class UltraJSONTests(TestCase): dec = ujson.decode(enc) self.assertEquals(enc, json.dumps(input, encoding="utf-8", ensure_ascii=False)) self.assertEquals(dec, json.loads(enc)) - + + def test_decodeFromUnicode(self): + input = u"{\"obj\": 31337}" + dec1 = ujson.decode(input) + dec2 = ujson.decode(str(input)) + self.assertEquals(dec1, dec2) def test_encodeRecursionMax(self): # 8 is the max recursion depth diff --git a/python/version.h b/python/version.h index 8e542b7..e9bea5e 100644 --- a/python/version.h +++ b/python/version.h @@ -1 +1 @@ -#define UJSON_VERSION "1.10" +#define UJSON_VERSION "1.11"