From e0b4772b178aa8c85757f06216ba036f2fee10b1 Mon Sep 17 00:00:00 2001 From: graemerocher Date: Wed, 9 Dec 2015 14:06:20 +0100 Subject: [PATCH] StreamingJsonBuilder should handle nested closures --- .../java/groovy/json/StreamingJsonBuilder.java | 10 +++++++++- .../groovy/json/StreamingJsonBuilderTest.groovy | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java b/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java index 058ae4a2f30..0a3630eaedd 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java @@ -487,6 +487,9 @@ public StreamingJsonDelegate(Writer w, boolean first) { this.first = first; } + /** + * @return Obtains the current writer + */ public Writer getWriter() { return writer; } @@ -500,7 +503,12 @@ public Object invokeMethod(String name, Object args) { switch (len) { case 1: final Object value = arr[0]; - call(name, value); + if(value instanceof Closure) { + call(name, (Closure)value); + } + else { + call(name, value); + } return null; case 2: if(arr[len -1] instanceof Closure) { diff --git a/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy b/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy index 9bae6555be8..f37e8ee6e53 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy @@ -65,6 +65,22 @@ class StreamingJsonBuilderTest extends GroovyTestCase { } } + void testJsonBuilderWithNestedClosures() { + new StringWriter().with { w -> + def builder = new StreamingJsonBuilder(w) + + builder.response { + status "ok" + results { + sectionId "world" + assert delegate instanceof StreamingJsonBuilder.StreamingJsonDelegate + } + } + + assert w.toString() == '{"response":{"status":"ok","results":{"sectionId":"world"}}}' + } + } + void testJsonBuilderConstructor() { new StringWriter().with { w -> new StreamingJsonBuilder(w, [a: 1, b: true])