1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-11-24 01:04:19 +01:00

Added support for decoding from a Unicode input string through conversion

This commit is contained in:
Jonas Tarnstrom 2011-12-14 14:50:27 +01:00
parent 6cd4ba528a
commit e35efaea85
3 changed files with 30 additions and 5 deletions

@ -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)
{

@ -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

@ -1 +1 @@
#define UJSON_VERSION "1.10"
#define UJSON_VERSION "1.11"