returning packed values via json

Use this board to ask questions or have discussions with other Profound.js users.
trjlove
New User
Posts: 4
Joined: Tue Apr 23, 2019 2:00 pm
First Name: Tim
Last Name: Love
Company Name: Wright Flood
Phone: 7275685518
State / Province: Florida
Zip / Postal Code: 33702
Country: United States
Contact:

returning packed values via json

Post by trjlove »

I am calling a program and want to return the results as a json object. However, those darned packed values are not playing nice. everything seems to be working, except translating the data structure to json with packed values.

if i evaluate the field value individually using the following:

Code: Select all

 console.log(logmessage + myDS[1].field1);
then the value is correct. but it blows chucks on the packed values. all other values are correct (no errors, just not readable data). I was not able to find documentation to convert the packed values.

Code: Select all

 pjs.define( 
        "myDS",   {type: "data structure", dim: 15, qualified: true, elements: {
        "field1": {type: "packed", length:  3, decimals:0, packeven: true},
        "field2": {type: "packed", length:  5, decimals:0, packeven: false},
        "field3": {type: "char",    length:  4},
        "field4": {type: "decimal", length:  3, decimals:0 }

    }});

    pjs.call("mylib/myprogram", pjs.refParm("myDS"));
    response.json({records: pjs.getDSValue(myDS)});
     
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: returning packed values via json

Post by Scott Klement »

I don't think you meant to use pjs.getDSValue, here. This creates a single buffer containing the DS. Basically, it's the equivalent of RPG's ability to view the entire data structure as a single string, so you get the raw buffer behind it.
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: returning packed values via json

Post by Scott Klement »

It'd be nice if we had a routine to convert the strict-data-type DS into a JSON primitive DS. We may add something like that in the future.

In the meantime, assuming you want the json to be the same format as your "myDS", you'd need to do something like this (after the pjs.call)

Code: Select all

var arr = [];
for (var i = 1; i <= myDs.length; i++)
  arr.push({ field1: myDs[i].field1, field2: myDs[i].field2 });
res.json(arr);
Basically, you're just constructing regular JavaScript primitive array with primitive objects inside of it, and returning that.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests