bugGNU Octave - Bugs: bug #64949, jsonencode() not supporting single...

 
 

bug #64949: jsonencode() not supporting single floats

Submitter:  Jesper Pedersen <jesper_p>
Submitted:  Thu 30 Nov 2023 09:46:37 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * 8.4.0 Operating System:  * Any
Fixed Release:  10.1.0 (current default) Planned Release:  10.1.0 (current default)
* Mandatory Fields

Add a New Comment Rich Markup
   

Sat 02 Dec 2023 12:41:10 AM UTC, comment #1: 

Thank you for not just reporting a bug, but finding a solution as well.

I made a small change in coding style to use isfloat() as a way to check for either a double or single input.


+++ b/libinterp/corefcn/jsonencode.cc
@@ -75,7 +75,7 @@ encode_numeric (T& writer, const octave_
   // Possibly write NULL for non-finite values (-Inf, Inf, NaN, NA)
   else if (ConvertInfAndNaN && ! octave::math::isfinite (value))
     writer.Null ();
-  else if (obj.is_double_type ())
+  else if (obj.isfloat ())
     writer.Double (value);
   else
     error ("jsonencode: unsupported type");


I checked this change in on the development branch of Octave here: http://hg.savannah.gnu.org/hgweb/octave/rev/dc06bb3bf1ed

I filed a separate bug report about the artifacts when encoding values of class single.  See bug #64960.

Marking this report as Fixed and closing.

Rik <rik5>
Group administrator
Thu 30 Nov 2023 09:46:37 AM UTC, original submission:  

Passing single-floats to jsonencode() throws an error (it works in Matlab). E.g.:

octave:1> jsonencode(single(123.456))
error: jsonencode: unsupported type


I've mostly been a user of Octave rather than a contributor so bear with me. However, I looked into the code and found that I could fix the issue by a simple modification to jsonencode.cc

diff -r f511a54000f7 libinterp/corefcn/jsonencode.cc
--- a/libinterp/corefcn/jsonencode.cc   Tue Nov 28 22:02:57 2023 -0800
+++ b/libinterp/corefcn/jsonencode.cc   Thu Nov 30 10:41:25 2023 +0100
@@ -75,7 +75,7 @@
   // Possibly write NULL for non-finite values (-Inf, Inf, NaN, NA)
   else if (ConvertInfAndNaN && ! octave::math::isfinite (value))
     writer.Null ();
-  else if (obj.is_double_type ())
+  else if (obj.is_double_type () || obj.is_single_type ())
     writer.Double (value);
   else
     error ("jsonencode: unsupported type");


I am aware that the underlying RapidJSON only supports double which means that I get some conversion artifacts like below. However, I feel that most users would prefer this simple solution over the current limitation. Or maybe someone has an idea of how to avoid below artifacts?

Behaviour after testing the above fix:


octave:1> jsonencode(single(123.456))
ans = 123.45600128173828



Jesper Pedersen <jesper_p>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jesper_p (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-12-02 rik5 StatusNone Fixed
        Open/ClosedOpen Closed
        Fixed ReleaseNone 10.1.0 (current default)
        Planned ReleaseNone 10.1.0 (current default)

    Back to the top

    Powered by Savane 3.13-04b1.
    Corresponding source code