ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Writing and reading JSON in RPGLE

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

  • Writing and reading JSON in RPGLE

    Hi, I have written a little article on how to write and read JSON in RPGLE

    It can be found here:

    Join thousands of people who own a premium domain. Affordable financing available.

  • #2
    Re: Writing and reading JSON in RPGLE

    Nice work.....
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: Writing and reading JSON in RPGLE

      Great Work Henrik!

      Never tried to pass the JSON in RPGLE and just finished the project with java,
      One more thing, does powerEXT support Base64 - encode and decode ?

      Thanks
      Dhanuxp
      Last edited by dhanuxp; May 2, 2012, 09:13 PM.

      Comment


      • #4
        Re: Writing and reading JSON in RPGLE

        Hi dhanuxp:

        One more thing, does powerEXT support Base64 - encode and decode ?
        If it doesn't it's built into the powerx box:



        or Scott Klement wrote his own:


        Best of Luck
        GLS
        The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

        Comment


        • #5
          Re: Writing and reading JSON in RPGLE

          Hi,

          no I haven't build in Base64 support but either use the system API or Scott's Base64 subprocedures.

          Regards
          Henrik

          Comment


          • #6
            Re: Writing and reading JSON in RPGLE

            This is a little tricky because powerEXT runs EBCDIC internally so if you have to include base64 in a XML document that typical are translated from EBCDIC to ASCII/UTF-8 when saved or send to a client you will have to convert back and forth.

            However it can be done and here is a proposal:

            Create a new store (similar to a user space) and import a file into the store like this:

            setContent(?text/xml?); // tells Apache to do EBCDIC conversion
            myBinStore = storeNew();
            storeInz(myBinStore);
            storeFromStmf64(myBinStore:?/mytifffile.tif?); // This new procedure I have to build

            The store now holds mytifffile.tif in Base64 ASCII

            convCCS(storeAddr(myBinStore):storeSize(myBinStore ):819:0);

            The store now holds mytifffile.tif in base64 EBCDIC

            xmlNode(?MYTIFF?);
            storeAppend(0: storeAddr(myBinStore):storeSize(myBinStore));
            storeFree(myBinStore);
            xmlEndNode();
            echoToClient();

            The buffer now has an XML element MYTIFF but it is all in EBCDIC

            But when send/saved it will be converted back into ASCII or UTF-8 that in a Base64 perspective are exactly the same since UTF-8 shares x?00? > x?7F? with ASCII and Base64 always will be in that range.

            Sending a file RAW in Base64 could be like

            setContent(?application/xml?); // tells Apache NOT to do EBCDIC conversion
            myBinStore = storeNew();
            storeInz(myBinStore);
            storeFromStmf64(myBinStore:?/mytifffile.tif?); // This new procedure I have to build

            The store now holds mytifffile.tif in Base64 ASCII

            storeAppend(0: storeAddr(myBinStore):storeSize(myBinStore));
            storeFree(myBinStore);
            echoToClient();


            Getting the base64 code back from an XML element is also a little tricky because it is processed in EBCDIC

            xmlFromStmf(?/mytiffxml.xml?);
            xmlReaderInz(xmlAddr():xmlSize());
            dow xmlReader = 0;
            select;
            when xmlGetNode = ?/MYTIFF?;
            myBinStore = storeNew();
            storeInz(myBinStore);
            storeAppend(myBinStore:xmlInnerAddr():xmlInnerSize ());
            convCCS(storeAddr(myBinStore):storeSize(myBinStore ):0:819);
            storeToStmf64(myBinStore:?/mynewtiff.tif?:819); // This new procedure I have to build
            endsl;
            enddo;

            Comment


            • #7
              Re: Writing and reading JSON in RPGLE

              Thanks Henrik/ GLS

              Sorry for my poor knowledge. Since Im not a Java programmer, this powerEXT impress me so much
              What I need to do is send a Image using RPG-JSON
              This is how I did it using java
              (ofcasue this is client side program, donot think about it)

              Code:
              			JSONObject jImage = new JSONObject();
              			Bitmap bm = setImageToImageView("/mnt/sdcard/IMG001.JPG");			
              			ByteArrayOutputStream baos = new ByteArrayOutputStream();
              			bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
              																// bitmap object
              			byte[] byteArrayImage = baos.toByteArray();
              
              			String encodedImage = Base64.encodeToString(byteArrayImage,Base64.DEFAULT);
              					
              			jImage.put("ImageID", "IMG0001");			
              			jImage.put("Image", encodedImage);
              I'm going to check the System Api and Scott Base64 in details soon. Say anyhow I encoded the image to String but
              What make me confuse is, If encoded string is bigger than variable length 65535, how can i handle it in RPG-JSON?

              Thanks
              Dhanuxp
              Last edited by dhanuxp; May 2, 2012, 10:06 PM.

              Comment

              Working...
              X