Results 1 to 10 of 26

Thread: JsonToDataType parse bug

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default JsonToDataType parse bug

    Hi,

    Reproduced in DataFlex 19.1, but unfortunately we are still on DataFlex 19.0, so we cannot use the altname workaround.

    The problem is easy to reproduce with the following code snippet:

    Code:
    Use Windows.pkg
    Use Dferror.pkg
    Use cJsonObject.pkg
    
    Struct tTransaction
        BigInt   id
        String   code
        Integer  companyId
        Date     date                
        Date     paymentDate
    End_Struct
    
    Object oJSON is a cJsonObject
    End_Object
    
    Procedure Test
      Boolean bOK
      String  sJSON
      tTransaction TR
      
      Move '{ "id": 0, "code": "2ae222a9-a44c-4c5c-8881-fd6c55829432", "companyId": 893349, "date": "2019-06-29", "paymentDate": "2019-06-29"}' To sJSON
      Get ParseString of oJSON sJSON to bOK
      If (bOK) Begin
        Get JsonToDataType of oJSON to TR
      End
    End_Procedure
    
    Send Test
    
    Winput "Click OK to Quit" WindowIndex
    Running the above triggers error
    Code:
    Invalid Json Object. Struct member not found in JSON. (.Date)
    
    Error: 4529
    
    MSG_TEST (4892) - DESKTOP (2) - at address 7947
    [start] - at address 7949
    But we did not define ".Date", we defined ".date"....

    However if I change the above code like this:
    Code:
    Use Windows.pkg
    Use Dferror.pkg
    Use cJsonObject.pkg
    
    Struct tTransaction
        BigInt   id
        String   code
        Integer  companyId
        date     date                
        Date     paymentDate
    End_Struct
    
    Object oJSON is a cJsonObject
    End_Object
    
    Procedure Test
      Boolean bOK
      String  sJSON
      tTransaction TR
      
      Move '{ "id": 0, "code": "2ae222a9-a44c-4c5c-8881-fd6c55829432", "companyId": 893349, "date": "2019-06-29", "paymentDate": "2019-06-29"}' To sJSON
      Get ParseString of oJSON sJSON to bOK
      If (bOK) Begin
        Get JsonToDataType of oJSON to TR
      End
    End_Procedure
    
    Send Test
    
    Winput "Click OK to Quit" WindowIndex
    THEN it works. No runtime errors.
    Unfortunately this is VERY fragile.

    For DataFlex 19.1 there's a solution, use the undocumented altname alternative.

    Code:
    Struct tTransaction
        BigInt   id
        String   code
        Integer  companyId
        { name="date"}
        Date     date
    Too bad we're still on 19.0 and no chance of upgrading any day soon.

    Edit: forgot to mention why this is a problem. This is a real problem because of things like the Studio automatically correcting variable type casing. Something that DfRefactor also can do on your behalf. It will break our code and makes it currently feel VERY FRAGILE.

    Our current workaround is to comment and apply altname already for the future.
    eg:
    Code:
    Struct tTransaction
        BigInt   id
        String   code
        Integer  companyId
        { name="date"}
        date     date                // On DataFlex 19.0 both "date" words on this line MUST BE all lowercase for the JsonToDataType function to work
    Not ideal, but the best we have for now.
    --
    Wil
    Last edited by wila; 30-Jul-2019 at 06:41 PM. Reason: Clarification and our workaround

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •