Groovy JsonBuilder strange behavior when toString ()

I need to create json to use as body in http.request

. I can grow json dynamically, but I noticed strange behavior when called builder.toString()

twice. The resulting json was completely different. I probably think it has something to do with some kind of buffer or so. I've read the documentation, but I can't seem to find a good answer. Here's the code for testing.

import groovy.json.JsonBuilder

def builder = new JsonBuilder()

def map = [
    catA: ["catA-element1", "catA-element2"],
    catB:[],
    catC:["catC-element1"]
]

def a = map.inject([:]) { res, k, v ->
    def b = v.inject([:]) {resp, i ->
        resp[i] = k
        resp
    }
    res += b
}
println a

def root = builder.query {
    bool {
        must a.collect{ k, v ->
            builder.match {
                "$v" k
            }
        }
    }
    should([
        builder.simple_query_string {
            query "text"
        }
    ])
}
println builder.toString()
println builder.toString()

      

This will print the following lines. Pay attention to the last two lines

[catA-element1:catA, catA-element2:catA, catC-element1:catC]
{"query":{"bool":{"must":[{"match":{"catA":"catA-element1"}},{"match":{"catA":"catA-element2"}},{"match":{"catC":"catC-element1"}}]},"should":[{"simple_query_string":{"query":"text"}}]}}
{"match":{"catC":"catC-element1"}}

      

In my code, I can easily send the first result toString()

to a variable and use it when needed. But why does this happen when called more than once?

+3


source to share


1 answer


I think this is happening because you are using builder

inside a closure bool

. If we do print builder.content

before printing the result ( buider.toString()

calls JsonOutput.toJson(builder.content)

), we get:

[query:[bool:ConsoleScript54$_run_closure3$_closure6@294b5a70, should:[[simple_query_string:[query:text]]]]]

      

By adding println builder.content

to the closure bool

, we can see what is being builder.content

modified when the closure is evaluated:

def root = builder.query {
    bool {
        must a.collect{ k, v ->
            builder.match {
                "$v" k
                println builder.content
            }
        }
    }
    should([
        builder.simple_query_string {
            query "text"
        }
    ])
}

println JsonOutput.toJson(builder.content)
println builder.content

      

The above gives:



[query:[bool:ConsoleScript55$_run_closure3$_closure6@39b6156d, should:[[simple_query_string:[query:text]]]]]
[match:[catA:catA-element1]]
[match:[catA:catA-element2]]
{"query":{"bool":{"must":[{"match":{"catA":"catA-element1"}},{"match":{"catA":"catA-element2"}},{"match":{"catC":"catC-element1"}}]},"should":[{"simple_query_string":{"query":"text"}}]}}
[match:[catC:catC-element1]]

      

You can easily avoid this with another closure builder inside:

def builder2 = new JsonBuilder()

def root = builder.query {
    bool {
        must a.collect{ k, v ->
            builder2.match {
                "$v" k
            }
        }
    }
    should([
        builder.simple_query_string {
            query "text"
        }
    ])
}

      

Or even better:

def root = builder.query {
    bool {
        must a.collect({ k, v -> ["$v": k] }).collect({[match: it]})
    }
    should([
        simple_query_string {
            query "text"
        }
    ])
}

      

+2


source







All Articles