summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshakefu <shakefu@gmail.com>2013-04-17 00:16:10 -0700
committershakefu <shakefu@gmail.com>2013-04-17 00:16:10 -0700
commit5291f8e0a778a4abe3f68b85a49b77f0831a4242 (patch)
tree3d17acc4dfeedf5eaa630aaa5788bf8e125ab3c3
parentd4d6469787614d2adac90bfe85c6ada28fa7deb3 (diff)
downloadsimplejson-5291f8e0a778a4abe3f68b85a49b77f0831a4242.tar.gz
Initial stab at for_json hook for C extension.
-rw-r--r--simplejson/_speedups.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c
index 403e08d..ec4be4f 100644
--- a/simplejson/_speedups.c
+++ b/simplejson/_speedups.c
@@ -260,6 +260,8 @@ static PyObject *
encoder_encode_float(PyEncoderObject *s, PyObject *obj);
static int
_is_namedtuple(PyObject *obj);
+static int
+_has_for_json_hook(PyObject *obj);
static PyObject *
moduleinit(void);
@@ -427,6 +429,20 @@ _is_namedtuple(PyObject *obj)
}
static int
+_has_for_json_hook(PyObject *obj)
+{
+ int rval = 0;
+ PyObject *for_json = PyObject_GetAttrString(obj, "for_json");
+ if (for_json == NULL) {
+ PyErr_Clear();
+ return 0;
+ }
+ rval = PyCallable_Check(for_json);
+ Py_DECREF(for_json);
+ return rval;
+}
+
+static int
_convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr)
{
/* PyObject to Py_ssize_t converter */
@@ -2801,6 +2817,17 @@ encoder_listencode_obj(PyEncoderObject *s, JSON_Accu *rval, PyObject *obj, Py_ss
if (encoded != NULL)
rv = _steal_accumulate(rval, encoded);
}
+ else if (_has_for_json_hook(obj)) {
+ PyObject *newobj;
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return rv;
+ newobj = PyObject_CallMethod(obj, "for_json", NULL);
+ if (newobj != NULL) {
+ rv = encoder_listencode_obj(s, rval, newobj, indent_level);
+ Py_DECREF(newobj);
+ }
+ Py_LeaveRecursiveCall();
+ }
else if (s->namedtuple_as_object && _is_namedtuple(obj)) {
PyObject *newobj;
if (Py_EnterRecursiveCall(" while encoding a JSON object"))