3

I need to create a json to use as body in an http.request. I'm able to build dynamically up the json, but I noticed a strange behavior when calling builder.toString() twice. The resulting json was totally different. I'm likely to think this is something related to a kind of buffer or so. I've been reading the documentation but I can't find a good answer. Here is a code to test.

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 toString() result to a variable and use it when needed. But, why does it change when invoking more than one time?

Mesi Rendón
  • 851
  • 1
  • 9
  • 21
  • I know this doesn't answer the _why_ of your question, but I think you meant to do `root.toString()`, not `builder.toString()`. – Keegan Jul 14 '15 at 04:57
  • 1
    Very interesting. If you add `println builder.content` you'll see that internal structure was modified under the hood. – Opal Jul 14 '15 at 07:25

1 Answers1

2

I think this is happening because you are using builder inside the closure bool. If we make print builder.content before printing the result (buider.toString() is calling JsonOutput.toJson(builder.content)) we get:

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

Adding println builder.content to the bool closure we can see that the builder.content is 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 yields:

[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 that with a different builder for the closure 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"
        }
    ])
}
jalopaba
  • 8,039
  • 2
  • 44
  • 57
  • I thought the same. Since all the time I was aware I was using the same builder object inside the bool closure. Having a different `JsonBuilder` instance helps to avoid the problem. And it must be true as well for the should closure. Thanks for the answer. – Mesi Rendón Jul 14 '15 at 13:17