Using other than Char/VarChar in JSON Nested DS

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
bbunney
Posts: 45
Joined: Wed Jul 06, 2022 7:52 pm

Using other than Char/VarChar in JSON Nested DS

Post by bbunney »

The way we have been creating the nested JSON data structures with YAJL is that all of the keys are defined as Char or VarChar. The JSON strings come into the web service from a .Net app. There are date, time, timestamp, numeric values (integers) and amounts that get consumed and published using JSON with YAJLINTO and YAJLDTAGEN. As you know, JSON has different data types like string, number, Boolean, object, array and null. In the RPG nested data structures can these data types be defined to match any of the JSON data types? Would a Boolean be defined as IND, number defined as a Packed, Zoned or INT data type? When using YAJLINTO and YAJLDTAGEN the values would have to be translated back and forth between the JSON string and the RPG nested DS. Thanks.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Using other than Char/VarChar in JSON Nested DS

Post by Scott Klement »

Hi,

When you write a %PARSER for DATA-INTO, you don't have any control over the data type. That is entirely handled by RPG. The %parser must pass everything to data-into as a character string, and it is up to RPG to assign it to whichever field matches the field name. The biggest limitation with this approach is with null or boolean values, or with arrays that have no values. We have no way of telling RPG what should be assigned to various data types.

For example, if YAJLINTO receives { "moneyAmount": false } it is a boolean value. So what do I pass back to RPG? Ideally, I'd pass *ON if it's an indicator, the word "true" if it is character, maybe a number 1 for a numeric -- but, I can't... because RPG doesn't tell the %PARSER what the field type is. YAJL defaults to passing '1' which is the same as *ON, so works for indicators, numbers... works for character, albeit you don't know the difference between an actual '1' vs 'true'. And I provide an option to override the value for true, false, or null to different things for each call to DATA-INTO. Not sure what else can be done.

For DATA-GEN RPG does tell the %GEN program (YAJLDTAGEN) exactly what the data type of each field is. So when a field is numeric (packed, zoned, integer, float) I will make the JSON field also be numeric. If the field is indicator (ind), I will make the JSON field be boolean. If the field is character, date, time or timestamp, I make the JSON field be character. Sadly, this makes it impossible to handle values that are null, becuase RPG does not support null indicators with either DATA-INTO or DATA-GEN.

If you need more sophisticated handling, YAJL does provide subprocedures you can call as an alternative to DATA-INTO or DATA-GEN. With these, you can completely control data types to your heart's content.
bbunney
Posts: 45
Joined: Wed Jul 06, 2022 7:52 pm

Re: Using other than Char/VarChar in JSON Nested DS

Post by bbunney »

Thank you so much for the information Scott. It is much appreciated!!
Post Reply