Forum Discussion

AlbertSoapUI's avatar
AlbertSoapUI
Contributor
6 years ago
Solved

JsonGenerator not recognized in external code, does not compile

Hi,

 

At this moment I am rewriting code that generates json body content, after reading values from an Excel file. Originally everything is written by using String operations, which makes the code horribly complicated. I want to remove this anti pattern with code generated by JsonBuilder. I did this using an external code compiler and it works (beautifully) then.

 

It looks something like this:

 

import groovy.json.JsonBuilder;
import groovy.json.JsonBuilder.*;
import groovy.json.*

public static String generateSomeContent(def travelClassValue, def productFamilyList)
def
generator = new JsonGenerator.Options().excludeNulls().build()
JsonBuilder builder = new JsonBuilder(generator)

def root = builder.productOptions {
travelClass travelClassValue
productFamilies productFamilyList
}
return JsonOutput.prettyPrint(generator.toJson(root).toString())

 If I want to call this (alike) method from ReadyApi. Unfortunatly this generates a compiler error, which I do not get when using an external compiler: 'unable to resolve class JsonGenerator.Options'

I can use JsonBuilder class and that works well in ReadyApi, but I really need to use JsonGenerator class for the option excludeNulls, otherwise I have to resort to all kind of null pointer checks - which will complicate the code again.

 

I suppose it has to do something with library imports, but I don't know which library I should import and how to do that.

I can't find similar threads on this subject, so here is my call for help...

  • Hi Msiadak,

    I give up on my old solution. Putting jar files in SoapUI is a bit too circumspect, as the code has to be easily shared with others. It's good to know for sure I cannot use all groovy logic of version 2.5. Thx.

    My new solution is somewhat like an older solution I have posted myself earlier.

     

    I put the structure in hasmaps (conditionally), and add this to a JsonBuilder object. Then I prettyprint this structure. Not as accessible as the intended solution, but better than adding jsonbuilding logic in a String.

     

    dummy code:

    import groovy.json.JsonBuilder;
    import groovy.json.JsonBuilder.*;
    import groovy.json.*

    public static String generateSomeContent(def travelClassValue, def productFamilyList)
    def rootMap = [:]
    if (
    travelClassValue) rootMap.travelClass = travelClassValue
    if (
    productFamilyList) rootMap.productFamily = productFamilyList.split(',')
    JsonBuilder builder = new JsonBuilder(rootMap)

    return
    JsonOutput.prettyPrint(builder.toString())

     

     

5 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    In ReadyAPI 2.4.0, the latest versin I have, it appears that the groovy library included is 2.4.4, and does not contain the importable JsonGenerator that you are trying to use. That might come in a later version of the groovy library and does not appear to be available in at least ReadyAPI 2.4.0.

     

    As far as I can see, the JsonGenerator code is available in Groovy 2.5.0. You can try submitting a support case, maybe, to see if ReadyAPI can adopt the newer version(s) of groovy that supports this?

    • AlbertSoapUI's avatar
      AlbertSoapUI
      Contributor

      Thank you for your answer.

      I have upgraded to ReadyApi 2.5.0, but I have the same problems as with 2.4.0. I also have to reprogram code using log4j with that (setLevel no longer supported).

      :smileysad:

      My (non-ReadyApi) compiler is Groovy Version: 2.5.2 JVM: 1.8.0_144, with which I developed the code to be implemented. I used this, so that I do not have to rerun SoapUI everytime for minor code changes. However, as you can see, this lead to false assumptions about the code to be used.

       

      Guess I just cannot use class JsonGenerator.Options in my project.

      I do want to keep using JsonBuilder, as I do not want to implement logic about generating json throughout the code and reinvent the weel over and over.

      Now, suppose I want to implement the logic that when there is no value for a key, then no key-value pair is generated in the json content (this is what JsonGenerator.Options().excludeNulls().build() is about). How should I do that, without complicating the code with null checks, and nested if-else logic?

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        Unfortunately to get what you desire, you are going to have to recreate the wheel and do all of that logic that that class was already offering you.  My guess is that code has those null checks in place and you will have to do that to error proof your groovy script.

         

        One thing you could try is to place the latest groovy.jar file that contains the functionality that you want into the Smartbear\ReadyAPI2.5.0\lib folder and restart ReadyAPI. It might let you use a different version of groovy, but I'd make a backup of everything first just in case!