changeset 32524:d2eabc48cf80

maint: code cleanup in jsonencode.cc * jsonencode.cc (encode_numeric): Avoid unnecessary call to scalar_value() if octave_value is really a boolean. Use fabs() to avoid two comparisons two positive and negative value of limit. * jsonencode_BIST.tst: Fix commenting to use Matlab-compatible style.
author Rik <rik@octave.org>
date Sat, 02 Dec 2023 18:03:12 -0800
parents dc06bb3bf1ed
children 42bb43092702
files libinterp/corefcn/jsonencode.cc test/json/jsonencode_BIST.tst
diffstat 2 files changed, 20 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/jsonencode.cc	Fri Dec 01 16:37:40 2023 -0800
+++ b/libinterp/corefcn/jsonencode.cc	Sat Dec 02 18:03:12 2023 -0800
@@ -61,22 +61,26 @@
 encode_numeric (T& writer, const octave_value& obj,
                 const bool& ConvertInfAndNaN)
 {
-  double value = obj.scalar_value ();
+  if (obj.isfloat ())
+    {
+      double value = obj.scalar_value ();
 
-  if (obj.is_bool_scalar ())
+      // Any numeric input from the interpreter will be in double type so in
+      // order to detect ints, we will check if the floor of the input and the
+      // input are equal using fabs (A - B) < epsilon method as it is more
+      // accurate.  If value > 999999, MATLAB will encode it in scientific
+      // notation (double).
+      if (fabs (floor (value) - value) < std::numeric_limits<double>::epsilon ()
+          && fabs (value) <= 999999)
+        writer.Int64 (value);
+      // Possibly write NULL for non-finite values (-Inf, Inf, NaN, NA)
+      else if (ConvertInfAndNaN && ! octave::math::isfinite (value))
+        writer.Null ();
+      else
+        writer.Double (value);
+    }
+  else if (obj.is_bool_scalar ())
     writer.Bool (obj.bool_value ());
-  // Any numeric input from the interpreter will be in double type so in order
-  // to detect ints, we will check if the floor of the input and the input are
-  // equal using fabs (A - B) < epsilon method as it is more accurate.
-  // If value > 999999, MATLAB will encode it in scientific notation (double)
-  else if (fabs (floor (value) - value) < std::numeric_limits<double>::epsilon ()
-           && value <= 999999 && value >= -999999)
-    writer.Int64 (value);
-  // Possibly write NULL for non-finite values (-Inf, Inf, NaN, NA)
-  else if (ConvertInfAndNaN && ! octave::math::isfinite (value))
-    writer.Null ();
-  else if (obj.isfloat ())
-    writer.Double (value);
   else
     error ("jsonencode: unsupported type");
 }
--- a/test/json/jsonencode_BIST.tst	Fri Dec 01 16:37:40 2023 -0800
+++ b/test/json/jsonencode_BIST.tst	Sat Dec 02 18:03:12 2023 -0800
@@ -15,8 +15,8 @@
 %! assert (isequal (jsonencode (logical (1)), 'true'));
 %! assert (isequal (jsonencode (logical (0)), 'false'));
 %! assert (isequal (jsonencode (50.025), '50.025'));
-%% FIXME: Uncomment when bug #64960 is fixed
-%!# assert (isequal (jsonencode (single (50.025)), '50.025'));
+%! %% FIXME: Uncomment when bug #64960 is fixed
+%! %% assert (isequal (jsonencode (single (50.025)), '50.025'));
 %! assert (isequal (jsonencode (NaN), 'null'));
 %! assert (isequal (jsonencode (NA), 'null'));    % Octave-only test
 %! assert (isequal (jsonencode (Inf), 'null'));