sitemule / noxDB

Not only XML. SQL,JSON and XML made easy for IBM i
MIT License
41 stars 20 forks source link

`_asJsonText` not printing an array #17

Closed worksofliam closed 5 years ago

worksofliam commented 5 years ago

(This is happening on the master branch)

Below is an example script which creates an array, which is part of a object. I can explicitly reference the array in JSON_asJsonText which prints just fine, but if I reference the object it is part of, it does not print correctly.

        ctl-opt main(main) dftactgrp(*no);
        ctl-opt bndDir('NOXDB');

        /include ./headers/jsonparser.rpgle

        Dcl-Proc main;
          Dcl-S lArray  Pointer;
          Dcl-S lObject Pointer;
          Dcl-S Result  Char(52);

          lObject = JSON_NewObject();
          lArray = JSON_NewArray(json_LocateOrCreate(lObject:'args'));

          JSON_ArrayPush(lArray:'AAA');
          JSON_ArrayPush(lArray:'BBB');
          JSON_ArrayPush(lArray:'CCC');

          Result = json_AsJsonText(lArray); 
          Dsply Result;

          Result = json_AsJsonText(lObject); 
          Dsply Result;

          Result = json_AsJsonText(json_Locate(lObject:'args')); 
          Dsply Result;
        End-Proc;
> call problem              
  DSPLY  ["AAA","BBB","CCC"]
  DSPLY  {"args":""}
  DSPLY  ""

When jsonStreamPrintNode is called with the array node from the object, it thinks it is a POINTER_VALUE, which explains why it is coming back as a blank string - but does not explain why it works when we reference the array pointer defined in the program.

NielsLiisberg commented 5 years ago

Both for newArray() and newObject() the "parent" parameter is depreciated - and will disappear in the new version of noxDB. You are using a notation used primary for backward compatibility but with the new feature locateOrCreate() so the behaviour is a little unpredictable ...

The code should look like the following which also is more flexible. You can move the the array into the object before or after the array contents. You can create the object before or after creating the array:

ctl-opt main(main) dftactgrp(*no);
        ctl-opt bndDir('NOXDB');

        /include ./headers/jsonparser.rpgle

        Dcl-Proc main;
          Dcl-S lArray  Pointer;
          Dcl-S lObject Pointer;
          Dcl-S Result  Char(52);

          lObject = json_NewObject();
          lArray = json_NewArray();

          json_ArrayPush(lArray:'AAA');
          json_ArrayPush(lArray:'BBB');
          json_ArrayPush(lArray:'CCC');

          json_moveObjectInto (lObject : 'args' : lArray);

          Result = json_AsJsonText(lArray); 
          Dsply Result;

          Result = json_AsJsonText(lObject); 
          Dsply Result;

          Result = json_AsJsonText(json_Locate(lObject:'args')); 
          Dsply Result;
        End-Proc;
worksofliam commented 5 years ago

Wow, thank you @NielsLiisberg - in fact, the parameters are documented as deprecated in the docs.. I just didn't read them. 🙄

Thanks!!