PDA

View Full Version : JsonToDataType not working as in DF19



Jimmy Hansen
17-Nov-2021, 05:59 AM
This code works in DF19, but not in 20. Why?




Struct tHusnrResultat
String id
Number status
String vejkode
String vejnavn
String adresseringsvejnavn
String husnr
Variant etage
Variant doer
Variant supplerendebynavn
String postnr
String postnrnavn
String kommunekode
String adgangsadresseid
Number x
Number y
End_Struct


Use cJsonHttpTransfer.pkg

Function GetHouseNumbersPage String sPostnr String sVejnavn Integer iPage Returns tHusnrResultat[]
Handle hoObj
Handle oJsonHttp
Handle hoReqJson
Handle hoRcvdJson
String sRequest
String sResponse
Boolean bOk
Integer eStatus
String sBaseURL
String sFilePath
tHusnrResultat[] HusnrResultat

Move 'dawa.aws.dk' to sBaseURL
Get Create (RefClass(cJsonHttpTransfer)) to oJsonHttp


Set pbShowErrorDialog of oJsonHttp to True
Set piRemotePort of oJsonHttp to rpHttpSSL
Set peTransferFlags of oJsonHttp to ifSecure // kald som https
Set psContentTypeExpected of oJsonHttp to ""
Move ("/adresser?postnr="+sPostnr+"&vejnavn="+sVejnavn+"&struktur=mini&side="+String(iPage)+"&per_side=100") to sFilePath

Get HttpGetJson of oJsonHttp sBaseURL sFilePath (&bOk) to hoRcvdJson
Get peJsonTransferStatus of oJsonHttp to eStatus
Send Destroy of oJsonHttp

If (bOk and hoRcvdJson>0) Begin
// To show response
Get Stringify of hoRcvdJson to sResponse
Move (Replaces('"dør"',sResponse,'"doer"')) to sResponse
Get ParseString of hoRcvdJson sResponse to bOk
If bOk Begin
Get JsonToDataType of hoRcvdJson to HusnrResultat
End
Send Destroy of hoRcvdJson
End

Function_Return HusnrResultat
End_Function


tHusnrResultat[] HusnrResultat
Get GetHouseNumbersPage "8362" "Banev%C3%A6nget" 1 to HusnrResultat

starzen
17-Nov-2021, 06:43 AM
may want to be more specific at what fails

do you get the proper json data for ex in sResponse at the stringify line?

DF20 is unicode so you may not need the replaces thing any longer but you would need to test that with field names in a struct or use the meta field name replacement if it works

Jimmy Hansen
17-Nov-2021, 06:58 AM
Hi there

Yeah, I get the correct json in sResponse. It's the JsonToDatatype function that gives me an error.
15021
I made the small demo program to easy reproduce the issue.
I understand DF20 is unicode and I could get away without the replace (dør/doer), but it doesn't make a difference.

starzen
17-Nov-2021, 07:12 AM
take the json string and go to jsonlint.com to validate that there isnt an issue with the json

Jimmy Hansen
17-Nov-2021, 07:45 AM
15022

And it works fine in DF19...

starzen
17-Nov-2021, 07:53 AM
looks like you have an array of objects not an object

Jimmy Hansen
17-Nov-2021, 08:02 AM
looks like you have an array of objects not an object
Yes? Not sure what you mean?

starzen
17-Nov-2021, 08:06 AM
never mind. you have it declared as an array

Harm Wibier
17-Nov-2021, 09:34 AM
I assume you mean that you get a GPF (Access violation) in 20.0?

It turns out that with the conversion to Unicode a bug sneaked in when a null has to be converted to a Variant member type in JsonToDataType. The shortest sample to reproduce it is this:



Struct tVariantCrash
String sMember
Variant vMember
End_Struct


Procedure Test
tVariantCrash crash
Handle hoJson
Boolean bSucces

Get Create (RefClass(cJsonObject)) to hoJson
Get ParseString of hoJson '{ "sMember": "value", "vMember": null }' to bSucces

Get JsonToDataType of hoJson to crash
Send Destroy of hoJson
End_Procedure

Send Test


Of course we will make sure this gets fixed for next rev. Note that you can work around this by changing the member into a String and setting pbRequireAllMembers to false like this:





Struct tVariantCrash
String sMember
String vMember
End_Struct

Procedure Test
tVariantCrash crash
Handle hoJson
Boolean bSucces

Get Create (RefClass(cJsonObject)) to hoJson
Set pbRequireAllMembers of hoJson to False
Get ParseString of hoJson '{ "sMember": "value", "vMember": null }' to bSucces

Get JsonToDataType of hoJson to crash
Send Destroy of hoJson
End_Procedure

Send Test

Jimmy Hansen
18-Nov-2021, 03:09 AM
Thank you Harm

Stephen W. Meeley
1-Dec-2021, 04:03 PM
Jimmy,

This is fixed in Beta 2.