Uniface User Forum

 View Only
  • 1.  structtojson issue

    Posted 01-02-2019 18:05

    json_input = "{"insp_result":"C","insp_comment":null,"user_id":null}"

    jsontostruct vst_stuct, json_input

    vst_struct→insp_comment = "Cancelled"

    vst_struct→user_id = "SOMEONE"


    Structs now looks like this;

    []

      [insp_result] = "C"

      [insp_comment] = "Cancelled"

      [user_id] = "SOMEONE"


    structtojson/whitespace json_out, vst_struct

    Json now looks like this;

    "

     {

      "insp_result" : "C",

      "insp_comment" : null,

      "user_id" : null

    }"


    What on earth am I missing here?

    Knut




  • 2.  RE: structtojson issue
    Best Answer

    PARTNER
    Posted 01-03-2019 08:47

    Hi Knut,

    null as none type for UNIFACE.

    replace null by "" in json string.

    or add  :

    vst_struct->insp_comment->$tags->jsonDataType= "string"

    vst_struct->user_id->$tags->jsonDataType= "string"

    Cordialy

    Gilles.



  • 3.  RE: structtojson issue

    Posted 01-03-2019 14:34

    Hi Gilles,

    I don't follow you... 

    The jsontostruct moves the tags into the struct - and I can assign values to those tags (and those values are visible in the stuct).  Thus, converting the struct to json should retain those said values and push the values to the json string.  No?

    Regards,

    Knut



  • 4.  RE: structtojson issue

    PARTNER
    Posted 01-03-2019 17:41

    Hi Knut,

    The answer is in the documentation (Structs for JSON Data):

    Json value

    Struct Value

    Struct annotation

    nullMember with no value (an empty string). The jsonDataTypeannotation indicates that it represents the nullJSON keyword.jsonDataType=null

    jsonDataType=null can have only one value : null

    Regards,

    Gilles.




  • 5.  RE: structtojson issue

    Posted 01-03-2019 18:09

    I guess good ol' Uniface have spoilt me with data type conversions over the years.. 

    so, rather than Uniface throwing an error when I assign a value to a null datatype in the struct - and despite the fact that the struct, when inspecting the struct in the debugger, has a value as assigned, because the struct datatype is null, it's going to export the value as null....?

    Good grief Charlie Brown.....

    I'll stick to $replace - heck of a lot easier to deal with...

    Regards,

    Knut




  • 6.  RE: structtojson issue

    PARTNER
    Posted 01-04-2019 12:49

    OK or change jsonDataType to string

    Variables

    string json_input,json_out
    struct vst_struct
    EndVariables

    json_input = "{%%"insp_result%%":%%"C%%",%%"insp_comment%%":null,%%"user_id%%":null}"

    jsontostruct vst_struct, json_input
    message/info vst_struct->$dbgstring
    vst_struct->insp_comment->$tags->jsonDataType= "string"
    vst_struct->insp_comment = "Cancelled"
    vst_struct->user_id->$tags->jsonDataType= "string"
    vst_struct->user_id = "SOMEONE"
    message/info vst_struct->$dbgstring

    structtojson/whitespace json_out, vst_struct
    message/info json_out

    Regards,

    Gilles.



  • 7.  RE: structtojson issue

    ROCKETEER
    Posted 01-11-2019 10:25

    Hi Knut,

    I had a look at this and I agree that the error handling is insufficient here.

    When a scalar value is defined as number jsonDataType but the value cannot be converted to a JSON number then structToJson falls back to the Uniface data type (e.g. string) and a warning is added to $procReturnContext.

    I think that a value tagged as a null jsonDataType should behave in a similar way or at least throw a warning that is visible in $procReturnContext.

    Will investigate.

    Daniel