ENH: Format decimal.Decimal
as full precision strings in .to_json(...)
by Tolker-KU · Pull Request #60698 · pandas-dev/pandas (original) (raw)
Thanks for working on this
Thanks for taking your time to review this. I'm planing to open an issue discussing if this should be hidden behind a flag, such that the default behaviour remains unchanged. This draft is just a proof of concept.
The decimal module has no C API, unfortunately. I found a recent issue in the cpython repo discussing an implementation but it hasn't happened yet. As far as I recall the maintainers of cpython thought this would bloat the C API without any real gain.
I do agree on your comments and will update the patch. It should be noted that most of the implementation is copy/pasted from here. Maybe this has some bugs as-well?
static char *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *outLen) { |
---|
PyObject *obj = (PyObject *)_obj; |
PyObject *str = PyObject_CallMethod(obj, "isoformat", NULL); |
if (str == NULL) { |
*outLen = 0; |
if (!PyErr_Occurred()) { |
PyErr_SetString(PyExc_ValueError, "Failed to convert time"); |
} |
((JSONObjectEncoder *)tc->encoder)->errorMsg = ""; |
return NULL; |
} |
if (PyUnicode_Check(str)) { |
PyObject *tmp = str; |
str = PyUnicode_AsUTF8String(str); |
Py_DECREF(tmp); |
} |
GET_TC(tc)->newObj = str; |
*outLen = PyBytes_GET_SIZE(str); |
char *outValue = PyBytes_AS_STRING(str); |
return outValue; |
} |