1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-11-24 05:12:02 +01:00

Allow a kwarg to be passed in specifying the double precision

This commit is contained in:
Mike Krieger 2012-01-03 16:33:34 -08:00
parent 72088a10de
commit 6b89292e5e
3 changed files with 43 additions and 13 deletions

@ -681,14 +681,22 @@ char *Object_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen)
PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs)
{ {
static char *kwlist[] = { "obj", "ensure_ascii", NULL}; static char *kwlist[] = { "obj", "ensure_ascii", "double_precision", NULL};
char buffer[65536]; char buffer[65536];
char *ret; char *ret;
PyObject *newobj; PyObject *newobj;
PyObject *oinput = NULL; PyObject *oinput = NULL;
PyObject *oensureAscii = NULL; PyObject *oensureAscii = NULL;
int idoublePrecision = 5; // default double precision setting
PRINTMARK();
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi", kwlist, &oinput, &oensureAscii, &idoublePrecision))
{
return NULL;
}
JSONObjectEncoder encoder = JSONObjectEncoder encoder =
{ {
Object_beginTypeContext, //void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc); Object_beginTypeContext, //void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc);
@ -707,17 +715,10 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs)
PyObject_Realloc, //JSPFN_REALLOC realloc; PyObject_Realloc, //JSPFN_REALLOC realloc;
PyObject_Free, //JSPFN_FREE free; PyObject_Free, //JSPFN_FREE free;
-1, //recursionMax -1, //recursionMax
5, //default decimal precision idoublePrecision,
1, //forceAscii 1, //forceAscii
}; };
PRINTMARK();
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &oinput, &oensureAscii))
{
return NULL;
}
if (oensureAscii != NULL && !PyObject_IsTrue(oensureAscii)) if (oensureAscii != NULL && !PyObject_IsTrue(oensureAscii))
{ {
encoder.forceASCII = 0; encoder.forceASCII = 0;

@ -29,7 +29,6 @@ class UltraJSONTests(TestCase):
output = ujson.encode(input) output = ujson.encode(input)
self.assertEquals(round(input, 5), round(json.loads(output), 5)) self.assertEquals(round(input, 5), round(json.loads(output), 5))
self.assertEquals(round(input, 5), round(ujson.decode(output), 5)) self.assertEquals(round(input, 5), round(ujson.decode(output), 5))
pass
def test_encodeWithDecimal(self): def test_encodeWithDecimal(self):
input = 1.0 input = 1.0
@ -41,7 +40,6 @@ class UltraJSONTests(TestCase):
output = ujson.encode(input) output = ujson.encode(input)
self.assertEquals(round(input, 5), round(json.loads(output), 5)) self.assertEquals(round(input, 5), round(json.loads(output), 5))
self.assertEquals(round(input, 5), round(ujson.decode(output), 5)) self.assertEquals(round(input, 5), round(ujson.decode(output), 5))
pass
def test_encodeArrayOfNestedArrays(self): def test_encodeArrayOfNestedArrays(self):
input = [[[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]] ] input = [[[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]], [[[]]] ]
@ -56,7 +54,39 @@ class UltraJSONTests(TestCase):
self.assertEquals(input, json.loads(output)) self.assertEquals(input, json.loads(output))
#self.assertEquals(output, json.dumps(input)) #self.assertEquals(output, json.dumps(input))
self.assertEquals(input, ujson.decode(output)) self.assertEquals(input, ujson.decode(output))
def test_doublePrecisionTest(self):
input = 30.012345678
output = ujson.encode(input, double_precision = 9)
self.assertEquals(input, json.loads(output))
self.assertEquals(input, ujson.decode(output))
output = ujson.encode(input, double_precision = 3)
self.assertEquals(round(input, 3), json.loads(output))
self.assertEquals(round(input, 3), ujson.decode(output))
output = ujson.encode(input)
self.assertEquals(round(input, 5), json.loads(output))
self.assertEquals(round(input, 5), ujson.decode(output))
def test_invalidDoublePrecision(self):
input = 30.12345678901234567890
output = ujson.encode(input, double_precision = 20)
# should snap to the max, which is 9
self.assertEquals(round(input, 9), json.loads(output))
self.assertEquals(round(input, 9), ujson.decode(output))
output = ujson.encode(input, double_precision = -1)
# also should snap to the max, which is 9
self.assertEquals(round(input, 9), json.loads(output))
self.assertEquals(round(input, 9), ujson.decode(output))
# will throw typeError
self.assertRaises(TypeError, ujson.encode, input, double_precision = '9')
# will throw typeError
self.assertRaises(TypeError, ujson.encode, input, double_precision = None)
def test_encodeStringConversion(self): def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t" input = "A string \\ / \b \f \n \r \t"
output = ujson.encode(input) output = ujson.encode(input)
@ -676,4 +706,3 @@ if __name__ == '__main__':
unittest.main() unittest.main()
heap = hp.heapu() heap = hp.heapu()
print heap print heap

@ -16,7 +16,7 @@ PyObject* JSONFileToObj(PyObject* self, PyObject *file);
static PyMethodDef ujsonMethods[] = { static PyMethodDef ujsonMethods[] = {
{"encode", objToJSON, METH_VARARGS | METH_KEYWORDS, "Converts arbitrary object recursivly into JSON. Use ensure_ascii=false to output UTF-8"}, {"encode", objToJSON, METH_VARARGS | METH_KEYWORDS, "Converts arbitrary object recursivly into JSON. Use ensure_ascii=false to output UTF-8. Pass in double_precision to alter the maximum digit precision with doubles"},
{"decode", JSONToObj, METH_O, "Converts JSON as string to dict object structure"}, {"decode", JSONToObj, METH_O, "Converts JSON as string to dict object structure"},
{"dumps", objToJSON, METH_VARARGS | METH_KEYWORDS, "Converts arbitrary object recursivly into JSON. Use ensure_ascii=false to output UTF-8"}, {"dumps", objToJSON, METH_VARARGS | METH_KEYWORDS, "Converts arbitrary object recursivly into JSON. Use ensure_ascii=false to output UTF-8"},
{"loads", JSONToObj, METH_O, "Converts JSON as string to dict object structure"}, {"loads", JSONToObj, METH_O, "Converts JSON as string to dict object structure"},