-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Format decimal.Decimal
as full precision strings in .to_json(...)
#60698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -373,6 +373,27 @@ static char *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *outLen) { | |
return outValue; | ||
} | ||
|
||
static char *PyDecimalToUTF8Callback(JSOBJ _obj, JSONTypeContext *tc, | ||
size_t *len) { | ||
PyObject *obj = (PyObject *)_obj; | ||
PyObject *format_spec = PyUnicode_FromStringAndSize("f", 1); | ||
PyObject *str = PyObject_Format(obj, format_spec); | ||
Py_DECREF(format_spec); | ||
|
||
if (str == NULL) { | ||
((JSONObjectEncoder *)tc->encoder)->errorMsg = ""; | ||
return NULL; | ||
} | ||
|
||
GET_TC(tc)->newObj = str; | ||
|
||
Py_ssize_t s_len; | ||
char *outValue = (char *)PyUnicode_AsUTF8AndSize(str, &s_len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be ideal if our functions retuned |
||
*len = s_len; | ||
|
||
return outValue; | ||
} | ||
|
||
//============================================================================= | ||
// Numpy array iteration functions | ||
//============================================================================= | ||
|
@@ -1467,8 +1488,18 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) { | |
tc->type = JT_UTF8; | ||
return; | ||
} else if (object_is_decimal_type(obj)) { | ||
pc->doubleValue = PyFloat_AsDouble(obj); | ||
tc->type = JT_DOUBLE; | ||
PyObject *is_nan_py = PyObject_RichCompare(obj, obj, Py_NE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do the test cases cover this code? If not, can you add a NaN case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should cover it: |
||
if (is_nan_py == NULL) { | ||
goto INVALID; | ||
} | ||
int is_nan = (is_nan_py == Py_True); | ||
Py_DECREF(is_nan_py); | ||
if (is_nan) { | ||
tc->type = JT_NULL; | ||
return; | ||
} | ||
pc->PyTypeToUTF8 = PyDecimalToUTF8Callback; | ||
tc->type = JT_UTF8; | ||
return; | ||
} else if (PyDateTime_Check(obj) || PyDate_Check(obj)) { | ||
if (object_is_nat_type(obj)) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.