PRIM_JSON.Writer reuse

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
sotis
Posts: 16
Joined: Thu Sep 16, 2021 11:37 pm

PRIM_JSON.Writer reuse

Post by sotis »

Hello,

Is it possible to clear and reuse a PRIM_JSON.Writer object? e.g. within a list

Code: Select all

Define_Com Class(#XPRIM_JsonWriter) Name(#writer)

Selectlist Named(#Products)
 
#Writer.SetOutputToString
#Writer.BeginObject
#Writer.BeginObject Name('outputData')
.
.
#Writer.EndObject

Insert Fields...
Endselect
The way it is written above does not work (it crashes).

best,
Sotiris
adale
Posts: 210
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: PRIM_JSON.Writer reuse

Post by adale »

Sotiris,
Yes, the XPRIM_JSON.Writer object can be reused within a list.
Are you wanting the output Json to be just simple json string, or are you wanting the output to be an array?
Not sure if this is what you are looking for, but here is a simple sample I tested to work.

Writer defined in the S/M:
DEFINE_COM Class(#XPRIM_JsonWriter) Name(#Writer)


I defined a work list and loaded test data:

Code: Select all

* define work list
DEF_LIST Name(#wrkList) Fields(#std_code #STD_DESC) Counter(#STD_INT) Type(*Working) Entrys(*Max)
* Build list
#STD_CODE := 'A'
#STD_DESC := 'Apples'
ADD_ENTRY To_List(#wrkList)
#STD_CODE := *blank
#STD_DESC := *blank

#STD_CODE := 'B'
#STD_DESC := 'Bananas'
ADD_ENTRY To_List(#wrkList)
#STD_CODE := *blank
#STD_DESC := *blank

#STD_CODE := 'C'
#STD_DESC := 'Cherries'
ADD_ENTRY To_List(#wrkList)
#STD_CODE := *blank
#STD_DESC := *blank

Then build the writer object with the data from the work list:

Code: Select all

* Begin root
#Writer.BeginObject

* begin list objectname
#Writer.BeginObject Name('outputListAsJson')

SELECTLIST Named(#wrkList)
#Writer.WriteString Name(#STD_CODE) Value(#STD_DESC)
ENDSELECT

* End list data
#Writer.EndObject

* end root
#Writer.EndObject

This produced the Json output as such:

{
"outputListAsJson": {
"A": "Apples",
"B": "Bananas",
"C": "Cherries"
}
}
Arlyn Dale
Servias LLC
Post Reply