Forum Discussion

AlbertSoapUI's avatar
AlbertSoapUI
Contributor
7 years ago

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 co...
  • AlbertSoapUI's avatar
    AlbertSoapUI
    7 years ago

    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())