From 6b3b0b8a03c4cbbbf2bd590138c8d860a052b868 Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Fri, 23 Sep 2016 11:12:30 +0200 Subject: [PATCH 1/4] GROOVY-7946 - allow writable values for StreamingJsonBuilder --- .../groovy/json/StreamingJsonBuilder.java | 16 ++++++++++++++ .../json/StreamingJsonBuilderTest.groovy | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) 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 05dbcdcb269..9e4a0fdd3fb 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java @@ -21,6 +21,7 @@ import groovy.lang.Closure; import groovy.lang.DelegatesTo; import groovy.lang.GroovyObjectSupport; +import groovy.lang.Writable; import java.io.IOException; import java.io.Writer; @@ -506,6 +507,9 @@ public Object invokeMethod(String name, Object args) { if(value instanceof Closure) { call(name, (Closure)value); } + else if(value instanceof Writable) { + call(name, (Writable)value); + } else { call(name, value); } @@ -651,6 +655,18 @@ public void call(String name, JsonOutput.JsonUnescaped json) throws IOException writer.write(json.toString()); } + /** + * Writes the given Writable as the value of the given attribute name + * + * @param name The attribute name The attribute name + * @param json The value The writable + * @throws IOException + */ + public void call(String name, Writable json) throws IOException { + writeName(name); + verifyValue(); + json.writeTo(writer); + } private void writeObjects(Iterable coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { verifyValue(); 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 41b884156e1..ae189ef59a5 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,28 @@ class StreamingJsonBuilderTest extends GroovyTestCase { } } + void testJsonBuilderWithWritableValue() { + new StringWriter().with { w -> + def builder = new StreamingJsonBuilder(w) + def writable = new Writable() { + @Override + Writer writeTo(Writer writer) throws IOException { + new StreamingJsonBuilder(writer).call { + sectionId "world" + assert delegate instanceof StreamingJsonBuilder.StreamingJsonDelegate + } + return writer + } + } + builder.response { + status "ok" + results writable + } + + assert w.toString() == '{"response":{"status":"ok","results":{"sectionId":"world"}}}' + } + } + void testJsonBuilderWithNestedClosures() { new StringWriter().with { w -> def builder = new StreamingJsonBuilder(w) From 1f3611407c5ed0101ce2ced42e396b960f9e580a Mon Sep 17 00:00:00 2001 From: graemerocher Date: Mon, 26 Sep 2016 08:26:02 +0200 Subject: [PATCH 2/4] Fix groovydoc errors --- .../src/main/java/groovy/json/StreamingJsonBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9e4a0fdd3fb..26ac3b817fc 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java @@ -658,8 +658,8 @@ public void call(String name, JsonOutput.JsonUnescaped json) throws IOException /** * Writes the given Writable as the value of the given attribute name * - * @param name The attribute name The attribute name - * @param json The value The writable + * @param name The attribute name + * @param json The writable value * @throws IOException */ public void call(String name, Writable json) throws IOException { From 8b15a6da2cb3c29886eee3db56fb32a4011856e6 Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Tue, 27 Sep 2016 10:57:02 +0200 Subject: [PATCH 3/4] GROOVY-7946 - handle GString values --- .../src/main/java/groovy/json/StreamingJsonBuilder.java | 7 ++++++- .../groovy/groovy/json/StreamingJsonBuilderTest.groovy | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) 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 9e4a0fdd3fb..38998a9b9c2 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java @@ -665,7 +665,12 @@ public void call(String name, JsonOutput.JsonUnescaped json) throws IOException public void call(String name, Writable json) throws IOException { writeName(name); verifyValue(); - json.writeTo(writer); + if(json instanceof CharSequence) { + writer.write(JsonOutput.toJson(json.toString())); + } + else { + json.writeTo(writer); + } } private void writeObjects(Iterable coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException { 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 ae189ef59a5..25ac0ae6494 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy @@ -71,8 +71,10 @@ class StreamingJsonBuilderTest extends GroovyTestCase { def writable = new Writable() { @Override Writer writeTo(Writer writer) throws IOException { + def value = "world" new StreamingJsonBuilder(writer).call { - sectionId "world" + sectionId "$value" + itemId "foo" assert delegate instanceof StreamingJsonBuilder.StreamingJsonDelegate } return writer @@ -83,7 +85,7 @@ class StreamingJsonBuilderTest extends GroovyTestCase { results writable } - assert w.toString() == '{"response":{"status":"ok","results":{"sectionId":"world"}}}' + assert w.toString() == '{"response":{"status":"ok","results":{"sectionId":"world","itemId":"foo"}}}' } } From 0a40e8759234b7a7a27e43a0edc7cbf8b44d991a Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Tue, 27 Sep 2016 11:03:04 +0200 Subject: [PATCH 4/4] GROOVY-7946 - narrow handling to just GString --- .../src/main/java/groovy/json/StreamingJsonBuilder.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 3ec813103e7..cce6e8d2be2 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/StreamingJsonBuilder.java @@ -18,10 +18,7 @@ */ package groovy.json; -import groovy.lang.Closure; -import groovy.lang.DelegatesTo; -import groovy.lang.GroovyObjectSupport; -import groovy.lang.Writable; +import groovy.lang.*; import java.io.IOException; import java.io.Writer; @@ -665,7 +662,7 @@ public void call(String name, JsonOutput.JsonUnescaped json) throws IOException public void call(String name, Writable json) throws IOException { writeName(name); verifyValue(); - if(json instanceof CharSequence) { + if(json instanceof GString) { writer.write(JsonOutput.toJson(json.toString())); } else {