ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Using YAJL to generate a JSON object across multiple external programs

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

  • Using YAJL to generate a JSON object across multiple external programs

    I've been successfully using YAJL to create and parse JSON objects. I've now got a requirement to generate a single JSON object using multiple external programs i.e. PGM A generates the first part of the JSON, then calls PGM B to generate the remainder of the JSON. How do I pass the JSON object between programs? I assume this can be done using pointers? Thanks in advance.

  • #2
    Hi Tim,

    Personally, I wouldn't design it that way. Instead, I would store the data in normal RPG variables (data structures, arrays, etc.) and I would pass those between the programs in order for each program to do their part and populate everything.

    Once all of the fields are set, have one routine that converts it to JSON.

    If each part is fully responsible for both the business logic to retrieve the data, and the export logic to export it, then it's not very modular. If you ever want to change an aspect of it, you essentially have to start over, rewriting everything. Instead, each piece of the application should have a specific role that they are responsible for. For example, one part might be responsible for calculating a certain part of the data, another part might be responsible for a different sort of calculations, and the last part might be responsible for taking the whole thing and converting it to JSON. When you use a design like this, you can swap out the individual parts to extend the application. For example, if you ever wanted to output XML instead of JSON, or YAJL instead of JSON, etc you would only need to rewrite the export portion, and the rest could stay the same. Likewise, if you decided under certain circumstances to calcuate some of the business logic differently, only that program would need to be replaced.

    That said, if you must do it by generating the JSON across multiple programs, even after hearing my recommendations not to, it's certainly possible.

    1) When generating the JSON, the data is stored in the memory of the YAJL service program. So if all of the calling applications are able to call the same activation of that service program, you can simply have each one generate their part. Since they're all sharing the same activation of the same service program, there isn't really anything to pass between them. They just need to know what the previous program did. (Which is why I don't recommend this type of thing... programs that need to know precisely what other programs do aren't really separated -- they are tightly coupled, that's what I was sayign above.)

    or

    2) JSON data is merely a character string. So no need to mess around with pointers or raw memory... You can just generate the JSON from the data you've added so far into a string, then pass that to another program. It can use yajl_string_load_tree to parse that string, then yajl_genFromNode() to copy that data into the YAJL generator. Then you can continue to generate more data after that.

    or

    3) If the previously generated string is meant to be internal to an object or array in a later program and is already formatted the way it needs to be, it can be inserted into the generator with yajl_addPreformattedPtr()

    or

    4) You can take my recommendation and NOT generate the JSON this way, but instead just create an RPG structure to hold it all and pass that between the programs. When all of the data is populated, have one last program that converts it to JSON. (This method would work with DATA-GEN, which would simplify things for you, vs calling the subprocedures to do the generating -- but even if you need to use the subprocedures, I think this is always a better design.)

    Comment


    • #3
      Thank you Scott for your very thorough reply! Lots of food for thought. I'll do some experimentation with each of your suggestions. Thanks again!

      Comment


      • #4
        Hi Scott, in experimenting with the 'yajl_addPreformattedPtr()' function, I'm encountering a binding error. The program compiles, but "Errors were found during the binding step". The binding error is 'CPD5D02' : 'Definition not found for symbol 'YAJL_ADDPREFORMATTEDPTR'.
        I'm running the latest version of YAJL. Any ideas?
        This is how I'm using the 'yajl_addPreformattedPtr()' function in my code:

        yajl_addPreformattedPtr('data': %addr(myJsonStr): %len(%trimr(myJsonStr)));

        Comment


        • #5
          Is it possible that you're picking up a different copy of YAJL from another library that might be older?

          Or, is it possible that when you installed the latest version, you didn't update the YAJLR4 service program somehow by mistake?

          Have you examined which copy of YAJLR4 you are binding to, and checked it's export list?

          Comment


          • #6
            Thank you Scott, you were right on the money. I had an older version of YAJLR4 higher in my library list!

            Comment

            Working...
            X