bugGNU Octave - Bugs: bug #63622, jsonencode breaks down on (nested)...

 
 

bug #63622: jsonencode breaks down on (nested) empty struct arrays

Submitter:  None
Submitted:  Thu 05 Jan 2023 10:13:33 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Segfault, Bus Error, etc.
Status:  Fixed Assigned to:  None
Originator Name:  dennisvang Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 7.1.0
Operating System:  * Any Fixed Release:  8.1.0
Planned Release:  8.1.0
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 10 Jan 2023 10:37:06 PM UTC, comment #3: 

The issue is that a 0x0 struct array is not being treated as an empty object.  In Matlab, jsonencode correctly returns '[]' for an empty struct array, whereas Octave is returning an empty string ''.

I fixed that on the stable branch here: http://hg.savannah.gnu.org/hgweb/octave/rev/b4bde1e47dde.  This will be a part of the 8.1 release.

Marking bug as fixed and closing report.

Rik <rik5>
Group administrator
Fri 06 Jan 2023 06:39:34 PM UTC, comment #2: 

As a workaround, or alternative, there are toJSON and fromJSOM in the io package which seem to do the right thing:

>> x.y = struct('z', {})
x =
  scalar structure containing the fields:
    y =
      0x0 struct array containing the fields:
        z

>> jsonencode (x)
ans = {"y"}
>> jsondecode (jsonencode (x))
error: jsondecode: parse error at offset 5: Missing a colon after a name of object member.

>> toJSON (x)
ans = {"y":{}}
>> fromJSON (toJSON (x))
ans =
  scalar structure containing the fields:
    y =
      scalar structure containing the fields:

>> jsondecode (toJSON (x))
ans =
  scalar structure containing the fields:
    y =
      scalar structure containing the fields:



Philip Nienhuis <philipnienhuis>
Group Member
Thu 05 Jan 2023 10:36:31 PM UTC, comment #1: 

Confirmed with Octave 8.0.1 on Windows.

Markus Mützel <mmuetzel>
Group administrator
Thu 05 Jan 2023 10:13:33 PM UTC, original submission:  


Example of invalid JSON (in this case using `--gui`):


>> x.y = struct('z', {});
>> jsonencode(x)
ans = {"y"}
>> jsondecode(jsonencode(x))
error: jsondecode: parse error at offset 5: Missing a colon after a name of object member.



Example of crash:


GNU Octave, version 7.1.0
...
Octave was configured for "x86_64-pc-linux-gnu".
...

octave:1> x.y = struct('z', {});
octave:2> x.a = 1;
octave:3> jsonencode(x)
octave-gui: /usr/include/rapidjson/writer.h:448: void rapidjson::Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags>::Prefix(rapidjson::Type) [with OutputStream = rapidjson::GenericStringBuffer<rapidjson::UTF8<> >; SourceEncoding = rapidjson::UTF8<>; TargetEncoding = rapidjson::UTF8<>; StackAllocator = rapidjson::CrtAllocator; unsigned int writeFlags = 2]: Assertion `type == kStringType' failed.
fatal: caught signal Aborted -- stopping myself...
Aborted (core dumped)


# My system
- OS: Ubuntu 22.04
- Octave version: Version 7.1.0
- Installation method: latest available snap

Anonymous

 

(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 mmuetzel (Posted a comment)
  •  

    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 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-10 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
        Fixed ReleaseNone 8.1.0
        Planned ReleaseNone 8.1.0
    2023-01-05 mmuetzel Item GroupNone Segfault, Bus Error, etc.
        StatusNone Confirmed
        Operating SystemGNU/Linux Any

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code