Accept iso_dates as a kwarg to print dates in ISO 8601 format #101

Closed
wants to merge 2 commits into
from

Projects

None yet

3 participants

@mattrobenolt

No description provided.

@alex alex commented on the diff Aug 1, 2013
python/objToJSON.c
@@ -841,6 +867,11 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs)
encoder.encodeHTMLChars = 1;
}
+ if (oisoDates != NULL && PyObject_IsTrue(oisoDates))
@alex
alex Aug 1, 2013

You need to check for errors in teh result of PyObject_IsTrue: http://docs.python.org/2/c-api/object.html#PyObject_IsTrue when it returns -1 that means an exception has occurred.

@alex alex commented on the diff Aug 1, 2013
tests/tests.py
@@ -316,6 +316,14 @@ def test_encodeDatetimeConversion(self):
self.assertEquals(int(expected), ujson.decode(output))
pass
+ def test_encodeDatetimeConversionISO(self):
+ ts = time.time()
+ input = datetime.datetime.fromtimestamp(ts)
+ output = ujson.encode(input, iso_dates=True)
+ expected = input.isoformat()
+ self.assertEquals(expected, ujson.decode(output))
+ pass
@alex
alex Aug 1, 2013

I don't understand why pass is left here.

@mattrobenolt
mattrobenolt Aug 1, 2013

I left to literally follow the convention in the other tests. I'd prefer to remove them all, but meh. I don't like it.

@alex alex commented on the diff Aug 1, 2013
python/objToJSON.c
@@ -178,6 +178,16 @@ static void *PyDateToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size
return NULL;
}
+static void *PyDateToISO(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
+{
+ PyObject *obj = (PyObject *) _obj;
+ PyObject *iso;
+
+ iso = PyObject_CallMethod(obj, "isoformat", NULL);
+ *_outLen = PyString_GET_SIZE(iso);
@alex
alex Aug 1, 2013

Before you use this you need to check that a) it returns a string, and b) it's non-null indicating that an exception didn't occur. I'm also not totally convinced this function reference counts correctly.

@jskorpan
Member

Hi
I'm very much interested in bringing this one on board, I've added generic means to which the integrator (Python) can pass private arguments through ujson and have them show up in the JSONTypeContext struct as encoder_prv value.

Could you adapt your code to setup an Options struct in the Python integration code which you then pass as the prv argument in the JSONObjectEncoder struct. Ujson will transfer that prv argment into encoder_prv in JSONTypeContext when calling the Python functions.

Looking forward to a pull request

@jskorpan jskorpan closed this Apr 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment