Permalink
Browse files

Added support for decoding from a Unicode input string through conver…

…sion
  • Loading branch information...
1 parent 6cd4ba5 commit e35efaea850a63788962c2db51229a198bcc796d @jskorpan jskorpan committed Dec 14, 2011
Showing with 30 additions and 5 deletions.
  1. +23 −3 python/JSONtoObj.c
  2. +6 −1 python/tests.py
  3. +1 −1 python/version.h
View
@@ -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)
{
View
@@ -221,7 +221,12 @@ def test_encodeToUTF8(self):
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
View
@@ -1 +1 @@
-#define UJSON_VERSION "1.10"
+#define UJSON_VERSION "1.11"

0 comments on commit e35efae

Please sign in to comment.