ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

DATA-GEN and DIM(*AUTO) Serendipity

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • DATA-GEN and DIM(*AUTO) Serendipity

    Hello.

    I came across something nice today. Whether it's intentional or not I don't know, and maybe Scott Klement can chime in since I'm using his YAJLDTAGEN.

    Using a varying dimension array (DIM(*AUTO) I found that DATA-GEN will serialize based on the number of array entries actually used, thereby eliminating the need for using "countprefix".

    Here's code:
    Code:
    h dftactgrp(*no) option(*srcstmt: *nodebugio: *noshowcpy)
    h bnddir('YAJL/YAJL')
    h decedit('0.')
    
    dcl-s jsonDoc varchar(250);
    
    dcl-ds names dim(*auto: 10) qualified inz;
      name varchar(50);
    end-ds;
    
    names(*next).name = 'John';
    names(*next).name = 'Doe';
    
    data-gen names
                 %data(jsonDoc)
                 %gen('YAJL/YAJLDTAGEN');
    
    *inlr = *on;
    return;​
    The result is variable "jsonDoc" contains [{"name":"John"},{"name":"Doe"}]. No need for an additonal field (like num_names).

    If I take out *AUTO so that it's just a DIM(10) then I get an additional eight empty names.

    Pretty cool, if it's by design.

    I couldn't find any documentation about this in the ILE RPG Reference but found this in Open Access guide for RPG (subfield totalElems looks like the key):



    Any thoughts?

    Mike
    Last edited by mlopez01; 4 weeks ago.

  • #2
    It's the way varying-dimension arrays work in general. If the current dimension of "names" is 2, then "DATA-GEN names" generates 2 elements.

    If you didn't have *VAR or *AUTO for your array, you would use %SUBARR to control the number of elements generated.
    DATA-GEN %SUBARR(names : 1 : 2) ...

    Comment


    • #3
      Although varying-dimension arrays are a nice "feature" as it pertains to DATA-GEN it's very limited since a varying-dimension array can only be at the top level, and typical JSON docs (what I'm using DATA-GEN for) usually contain many nested levels.

      Is there any talk around the water cooler about allowing nested varying-dimension arrays?

      Comment

      Working...
      X