LANSA Integrator - Json Binding Wizard – Cannot define Array of Objects

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
nick.diama
Posts: 19
Joined: Wed May 25, 2016 5:36 pm

LANSA Integrator - Json Binding Wizard – Cannot define Array of Objects

Post by nick.diama »

Hi all,

Based on the json specification we need to build a fragment array in which multiple objects will reside.
See below the example:

"channels": [
{
"viber": {
"message": {
"text": "Viber Message"
},
"validity_period": 120
}
},
{
"sms": {
"from": "TEST",
"text": "SMS Message",
"encoding": 0,
"type": 1,
"validity_period": 240
}
}
]

Upon trying to build the solution, the json server types are built like so, where inside the MobileMessage fragment there is an array of MobileChannels. Inside MobileChannels fragment there are 3 fragments, one for push one for viber and one for sms.

json_types.png
json_types.png (14.24 KiB) Viewed 8781 times

Building the solution based on the json server type described above we have the following.

json_wizard.png
json_wizard.png (17.38 KiB) Viewed 8781 times

However, the solution as it is built produces the following (as expected) json and not the one requested by the json specification.

"channels": [
{
"viber": {
"validity_period": 60,
"message": {
"image": "",
"caption": "",
"action": "",
"text": ""
},
"smartphone_only": false,
"action_tracking": true
},
"sms": {
"validity_period": 2880,
"encoding": 0,
"type": 0,
"from": "",
"text": ""
}
}
],

So, inside fragment array “channels” 3 fragments are produced (2 in this case) inside a single entity of the array “channels”.
Is it possible to define somehow an element of type object that will be the repeating entity of the array instead of having fragments?
Currently, I cannot see such possibility in the definitions of json server types.

Am I missing something?
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: LANSA Integrator - Json Binding Wizard – Cannot define Array of Objects

Post by Dino »

Maybe somebody will come with a better answer and using the LANSA Integrator JSON Binding Wizard,

but... why not using the new way available in Visual LANSA since V14 to generate JSON? JSON files can be very complicated and not really that structured, so this little code can handle that file generation in a server module or reusable part, without the need of LANSA Integrator.

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)

Define_Com Class(#XPRIM_HttpRequest) Name(#Request) Scope(*Application)
Define_Com Class(#XPRIM_JsonWriter) Name(#Writer)
Define Field(#RESPONSE) Type(*String) Length(512)

Srvroutine Name(returnsjson)

#Request.Clear

#Writer.SetOutputToString

#Writer.BeginObject
#Writer.BeginArray( 'channels' )
*
#Writer.BeginObject
#Writer.BeginObject Name('viber')
#Writer.BeginObject Name('message')
#Writer.WriteString Name('text') Value('Viber Message')
#Writer.EndObject
#Writer.WriteNumber Name('validity_period') Value(120)
#Writer.EndObject
#Writer.EndObject
*
#Writer.BeginObject
#Writer.BeginObject Name('sms')
#Writer.WriteString Name('from') Value('TEST')
#Writer.WriteString Name('text') Value('SMS Message')
#Writer.WriteNumber Name('encoding') Value(0)
#Writer.WriteNumber Name('type') Value(1)
#Writer.WriteNumber Name('validity_period') Value(240)
#Writer.EndObject
#Writer.EndObject

#Writer.EndArray

#Writer.EndObject

#Request.Content.AddString Value(#Writer.AsString)
#Request.Options.AddHeader Name('Content-Type') Value('application/json')
#Request.DoPost Url('https://companyxyz.com/xyzapi/')

If (#Request.Response.IsSuccessfulRequest)
If (#Request.Response.IsSuccessHttpStatusCode)
#RESPONSE := #Request.RESPONSE.AsString.AsNativeString
Else
#RESPONSE := #Request.RESPONSE.ErrorCode.AsNativeString + ' / ' + #Request.RESPONSE.ErrorMessage.AsNativeString
Endif
Else
#RESPONSE := #Request.RESPONSE.ErrorCode.AsNativeString + ' / ' + #Request.RESPONSE.ErrorMessage.AsNativeString
Endif
Endroutine
End_Com
json0302.jpg
json0302.jpg (189.08 KiB) Viewed 8754 times
SIDE NOTE: if you want to test this, an easy way is to create a folder which will act like the trace in integrator, a folder like c:\temp\lansaxlib_logs in your pc, put a file lansaxlib.config.json attached here in the c:\temp\ , and add this lines to your code:

Code: Select all

Define_Com Class(#XPRIM_OSUtil) Name(#OSUtil) Scope(*Application)

If (*OSAPI = "IBMI")
#OSUtil.SetEnvironmentVariable Name('LANSA_XLIB_CONFIG') Value('/tmp/lansaxlib.configas400.json')
Else
#OSUtil.SetEnvironmentVariable Name('LANSA_XLIB_CONFIG') Value('c:\temp\lansaxlib.config.json')
Endif
Attachments
lansaxlib.config.json.zip
(279 Bytes) Downloaded 1183 times
nick.diama
Posts: 19
Joined: Wed May 25, 2016 5:36 pm

Re: LANSA Integrator - Json Binding Wizard – Cannot define Array of Objects

Post by nick.diama »

Hi Dino,

Thanks for this! What you described is my alternative. I had already implemented this in a reusable part.
I just wanted to know if we can declare element objects inside a fragment array in LANSA Integrator, not just string/int/bool elements.
If it was on me, I would definitely go with the reusable part solution. Our customer however has a different opinion.
Post Reply