groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [5/7] groovy git commit: GROOVY-7706 - StreamingJsonBuilder should handle nested closures (closes #208)
Date Fri, 11 Dec 2015 23:30:20 GMT
GROOVY-7706 - StreamingJsonBuilder should handle nested closures (closes #208)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/7bf43908
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/7bf43908
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/7bf43908

Branch: refs/heads/GROOVY_2_4_X
Commit: 7bf43908af7f99cfb4e9496b706160ebaee1f34f
Parents: f92af36
Author: graemerocher <graeme.rocher@gmail.com>
Authored: Wed Dec 9 14:06:20 2015 +0100
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Fri Dec 11 14:41:42 2015 -0800

----------------------------------------------------------------------
 .../main/java/groovy/json/StreamingJsonBuilder.java | 10 +++++++++-
 .../groovy/json/StreamingJsonBuilderTest.groovy     | 16 ++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7bf43908/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java
----------------------------------------------------------------------
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 5c8e10e..7290d52 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 class StreamingJsonBuilder extends GroovyObjectSupport {
             this.first = first;
         }
 
+        /**
+         * @return Obtains the current writer
+         */
         public Writer getWriter() {
             return writer;
         }
@@ -500,7 +503,12 @@ public class StreamingJsonBuilder extends GroovyObjectSupport {
                     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) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/7bf43908/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy
----------------------------------------------------------------------
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 9bae655..f37e8ee 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])


Mime
View raw message