From 9f10e1a0eef0dd5572a34a76617d50df0e3dd357 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Fri, 21 Sep 2018 21:50:56 +0200 Subject: [PATCH] Support bignums when serializing JSON * src/json.c (lisp_to_json): Support bignums. * test/src/json-tests.el (json-serialize/bignum): New test. --- src/json.c | 10 +++++++--- test/src/json-tests.el | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/json.c b/src/json.c index 976783d785c..8b365e3795c 100644 --- a/src/json.c +++ b/src/json.c @@ -488,10 +488,14 @@ lisp_to_json (Lisp_Object lisp, struct json_configuration *conf) return json_check (json_false ()); else if (EQ (lisp, Qt)) return json_check (json_true ()); - else if (FIXNUMP (lisp)) + else if (INTEGERP (lisp)) { - CHECK_TYPE_RANGED_INTEGER (json_int_t, lisp); - return json_check (json_integer (XFIXNUM (lisp))); + intmax_t low = TYPE_MINIMUM (json_int_t); + intmax_t high = TYPE_MAXIMUM (json_int_t); + intmax_t value; + if (! integer_to_intmax (lisp, &value) || value < low || high < value) + args_out_of_range_3 (lisp, make_int (low), make_int (high)); + return json_check (json_integer (value)); } else if (FLOATP (lisp)) return json_check (json_real (XFLOAT_DATA (lisp))); diff --git a/test/src/json-tests.el b/test/src/json-tests.el index 8bd679b886e..911bc49730d 100644 --- a/test/src/json-tests.el +++ b/test/src/json-tests.el @@ -278,5 +278,13 @@ Test with both unibyte and multibyte strings." :type 'no-catch) (should (equal calls 1))))) +(ert-deftest json-serialize/bignum () + (skip-unless (fboundp 'json-serialize)) + (should (equal (json-serialize (vector (1+ most-positive-fixnum) + (1- most-negative-fixnum))) + (format "[%d,%d]" + (1+ most-positive-fixnum) + (1- most-negative-fixnum))))) + (provide 'json-tests) ;;; json-tests.el ends here -- 2.39.2