ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

HTTPAPI / YAJL and UTF-8

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • HTTPAPI / YAJL and UTF-8

    Hi!

    So, what is my situation:
    I receive utf-8 json data from a webservice via HTTPAPI. So far, the utf-8 data is stored in a variable in my RPGLE program. One way is to convert the data to EBSDIC via iConv....the converted data in this variable can be parsed with YAJL via yajl_buf_load_tree.
    Evarything works fine so far.
    But now, i have the requirement, that i need to parse the raw utf-8 data to get i.e. the name of a city in utf-8 - this information i have to store in my utf-8 DB2 table.
    When i try to parse the variable with utf-8 data via yajl_buf_load_tree i get an error (invalid characters).
    So, is there a proper way to do this - have anyone an idea?

  • #2
    Why not use yajl_tree_parse()?!

    Did you look at the code for yajl_buf_load_tree? All it does is convert from EBCDIC to UTF-8 and then call yajl_tree_parse... do you really want to convert it to from UTF-8 to EBCDIC just so it can be immediately converted back to UTF-8? That doesn't make a lot of sense to me.

    Comment


    • #3
      Since I just did this for a project I am happy to share the code I used.

      // Before copying in the YAJL prototypes we need to use the
      // define directive as we are using one of the YAJL C APIs directly.
      // Without this that prototype would be excluded when copying yajl_h

      /define YAJL_C_PROTOTYPES

      // Add null terminator to end of response string for parsing by YAJL
      responseJSON += X'00';

      // Data is already in UTF-8 so call the underlying C function not the wrapper
      root = yajl_tree_parse( p_responseJSON
      : errText
      : %Len(errText) );


      Now I just wait for Scott to tell me I didn't need to do that all <grin>

      P.S. p_responseJSON is initialized with %Addr( responseJSON : *Data ) because responseJSON is a varying field.

      Comment


      • #4
        You didn't need to add x'00' to your JSON data, since the prototype has options(*string)...

        Comment


        • #5
          Originally posted by Scott Klement View Post
          You didn't need to add x'00' to your JSON data, since the prototype has options(*string)...
          I noticed that - but in this case I was looking for best performance and there is the potential for the response packet to be very large in my case. Taking advantage of that option would have resulted in a (potentially) huge amount of data being copied to add the null terminator.

          Sorry - should have made that clear in my comment.

          Am I correct in assuming that the error text comes back from yajl_tree_parse as a null terminated string?

          Comment


          • #6
            Sinee you pass the length as well as the string itself, I would assume it is not null-terminated. But, I don't remember for sure... you'd have to try it and see.

            Comment


            • #7
              Originally posted by Scott Klement View Post
              Why not use yajl_tree_parse()?!

              Did you look at the code for yajl_buf_load_tree? All it does is convert from EBCDIC to UTF-8 and then call yajl_tree_parse... do you really want to convert it to from UTF-8 to EBCDIC just so it can be immediately converted back to UTF-8? That doesn't make a lot of sense to me.
              Thank you very much vor your help....it will work now!

              Comment


              • #8
                Originally posted by JonBoy View Post
                Since I just did this for a project I am happy to share the code I used.

                // Before copying in the YAJL prototypes we need to use the
                // define directive as we are using one of the YAJL C APIs directly.
                // Without this that prototype would be excluded when copying yajl_h

                /define YAJL_C_PROTOTYPES

                // Add null terminator to end of response string for parsing by YAJL
                responseJSON += X'00';

                // Data is already in UTF-8 so call the underlying C function not the wrapper
                root = yajl_tree_parse( p_responseJSON
                : errText
                : %Len(errText) );


                Now I just wait for Scott to tell me I didn't need to do that all <grin>

                P.S. p_responseJSON is initialized with %Addr( responseJSON : *Data ) because responseJSON is a varying field.


                Where can I find information on using YAJL in this fashion? I'd like to be able to send JSON directly to my RPG program. I'm using CGIDEV2 to read the input. Right now I'm creating a JSON object but sending it to the RPG program as a string. I then use yajl_buf_load_tree to decode it all. I'd like to be able to send true JSON objects.

                Thanks.
                Your friends list is empty!

                Comment

                Working...
                X