Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: JsonToDataType parse bug

Hybrid 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

  2. #2
    Join Date
    Feb 2009
    Location
    Castlegar, BC Canada
    Posts
    3,679

    Default Re: JsonToDataType parse bug

    Thanks for taking the time to document this. Discovering the workaround must have been about 1000 hours of pleasure.

    Mike

  3. #3

    Default Re: JsonToDataType parse bug

    That's a lot!
    The bug was identified and a workaround found within the hour.

    But then comes discussing it with the client.
    Looking at impact, reproducing it in DF19.1, looking up altname details, testing altname, making sure altname doesn't do harm in DF19.0.
    Making a small reproducable sample for the forum, etc..

    So yes it did take the speed out of what I was working on for about half a day, which I think is bad enough for a casing issue.
    --
    Wil

  4. #4
    Join Date
    Feb 2009
    Location
    Castlegar, BC Canada
    Posts
    3,679

    Default Re: JsonToDataType parse bug

    I was just thinking about how you figured out/(stumbled onto) that changing "Date" to "date" ended up being the work-around. That's my big question of the day

  5. #5

    Default Re: JsonToDataType parse bug

    Just a matter of trying and being amazed at the result of that test.
    --
    Wil

  6. #6
    Join Date
    Feb 2009
    Location
    Colombia
    Posts
    1,276

    Default Re: JsonToDataType parse bug

    Hi all

    God, I thought he was the only one who messed with json Rest, I think DAC needs to make an extra effort and make a patch for df18.2 / 19.0 / 19.1.

    Edgar

  7. #7
    Join Date
    Feb 2009
    Posts
    4,736

    Default Re: JsonToDataType parse bug

    The thing I find odd is it is objecting to the Data Type as opposed to the variable name ?? Which I assume is a quirk of using a reserved word ?
    Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill

  8. #8

    Default Re: JsonToDataType parse bug

    Yeah, that's the bug in a few words and yep I do expect that it is because the RESTful service I'm using is using a bunch (not just one) of DataFlex reserved words.
    Not something I have any control over I'm afraid.
    --
    Wil

  9. #9
    Join Date
    Feb 2009
    Posts
    4,736

    Default Re: JsonToDataType parse bug

    JsonToDataType and DataTypeToJson are great and do what the say on the tin (this challenge aside)

    However when used in conjunction with REST services they almost do what you need but not quite

    There is another long thread on here about it but I'm not going to link to it because there is no new info to add at this stage and I don't want to create another thread explosion
    Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill

  10. #10
    Join Date
    Nov 2008
    Location
    Round Rock, TX
    Posts
    7,695

    Default Re: JsonToDataType parse bug

    Hi Wil,

    are you aware of the Name Meta-Data Tag we added in 19.1?

Posting Permissions

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