If a converter is found, further process the result (closes #457)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/0da08714 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/0da08714 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/0da08714 Branch: refs/heads/parrot Commit: 0da08714341ae6f8678216acd299a43034f7542b Parents: a0875e3 Author: jameskleeh <james.kl...@gmail.com> Authored: Sat Nov 5 18:36:49 2016 -0400 Committer: John Wagenleitner <jwagenleit...@apache.org> Committed: Tue Nov 8 17:10:39 2016 -0800 ---------------------------------------------------------------------- .../main/java/groovy/json/DefaultJsonGenerator.java | 12 ++++++------ .../src/main/java/groovy/json/JsonGenerator.java | 8 ++++---- .../src/spec/test/json/JsonBuilderTest.groovy | 2 +- .../groovy-json/src/spec/test/json/JsonTest.groovy | 4 ++-- .../src/spec/test/json/StreamingJsonBuilderTest.groovy | 2 +- .../groovy/groovy/json/CustomJsonGeneratorTest.groovy | 13 ++++++++----- .../groovy/groovy/json/DefaultJsonGeneratorTest.groovy | 6 +++--- .../src/test/groovy/groovy/json/JsonBuilderTest.groovy | 2 +- .../groovy/groovy/json/StreamingJsonBuilderTest.groovy | 4 ++-- 9 files changed, 28 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java b/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java index 1dbd0ad..634d9d3 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java @@ -182,8 +182,8 @@ public class DefaultJsonGenerator implements JsonGenerator { Converter converter = findConverter(objectClass); if (converter != null) { - writeRaw(converter.convert(object, key), buffer); - return; + object = converter.convert(object, key); + objectClass = object.getClass(); } if (CharSequence.class.isAssignableFrom(objectClass)) { // Handle String, StringBuilder, GString and other CharSequence implementations @@ -450,10 +450,10 @@ public class DefaultJsonGenerator implements JsonGenerator { protected static class ClosureConverter implements Converter { protected final Class<?> type; - protected final Closure<? extends CharSequence> closure; + protected final Closure closure; protected final int paramCount; - protected ClosureConverter(Class<?> type, Closure<? extends CharSequence> closure) { + protected ClosureConverter(Class<?> type, Closure closure) { if (type == null) { throw new NullPointerException("Type parameter must not be null"); } @@ -498,7 +498,7 @@ public class DefaultJsonGenerator implements JsonGenerator { * @param value the object to convert * @return a JSON value representing the value */ - public CharSequence convert(Object value) { + public Object convert(Object value) { return convert(value, null); } @@ -509,7 +509,7 @@ public class DefaultJsonGenerator implements JsonGenerator { * @param key the key name for the value, may be {@code null} * @return a JSON value representing the value */ - public CharSequence convert(Object value, String key) { + public Object convert(Object value, String key) { return (paramCount == 1) ? closure.call(value) : closure.call(value, key); http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java b/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java index 91b0e06..e0a13d6 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java @@ -89,7 +89,7 @@ public interface JsonGenerator { * @param value the object to convert * @return a JSON value representing the object */ - CharSequence convert(Object value); + Object convert(Object value); /** * Converts a given object to a JSON value. @@ -98,7 +98,7 @@ public interface JsonGenerator { * @param key the key name for the value, may be {@code null} * @return a JSON value representing the object */ - CharSequence convert(Object value, String key); + Object convert(Object value, String key); } @@ -225,7 +225,7 @@ public interface JsonGenerator { * <pre><code class="groovyTestCase"> * def generator = new groovy.json.JsonGenerator.Options() * .addConverter(URL) { URL u -> - * "\"${u.getHost()}\"" + * u.getHost() * } * .build() * @@ -251,7 +251,7 @@ public interface JsonGenerator { */ public <T> Options addConverter(Class<T> type, @ClosureParams(value=FromString.class, options={"T","T,String"}) - Closure<? extends CharSequence> closure) + Closure closure) { Converter converter = new DefaultJsonGenerator.ClosureConverter(type, closure); if (converters.contains(converter)) { http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/spec/test/json/JsonBuilderTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/spec/test/json/JsonBuilderTest.groovy b/subprojects/groovy-json/src/spec/test/json/JsonBuilderTest.groovy index d4a1576..0328cc7 100644 --- a/subprojects/groovy-json/src/spec/test/json/JsonBuilderTest.groovy +++ b/subprojects/groovy-json/src/spec/test/json/JsonBuilderTest.groovy @@ -79,7 +79,7 @@ class JsonBuilderTest extends GroovyTestCase { .excludeNulls() .excludeFieldsByName('make', 'country', 'record') .excludeFieldsByType(Number) - .addConverter(URL) { url -> '"http://groovy-lang.org"' } + .addConverter(URL) { url -> "http://groovy-lang.org" } .build() JsonBuilder builder = new JsonBuilder(generator) http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/spec/test/json/JsonTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/spec/test/json/JsonTest.groovy b/subprojects/groovy-json/src/spec/test/json/JsonTest.groovy index c230a62..4f9bf83 100644 --- a/subprojects/groovy-json/src/spec/test/json/JsonTest.groovy +++ b/subprojects/groovy-json/src/spec/test/json/JsonTest.groovy @@ -140,9 +140,9 @@ class JsonTest extends GroovyTestCase { def generator = new JsonGenerator.Options() .addConverter(URL) { URL u, String key -> if (key == 'favoriteUrl') { - '"' + u.getHost() + '"' + u.getHost() } else { - JsonOutput.toJson(u) + u } } .build() http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/spec/test/json/StreamingJsonBuilderTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/spec/test/json/StreamingJsonBuilderTest.groovy b/subprojects/groovy-json/src/spec/test/json/StreamingJsonBuilderTest.groovy index 7deb2ae..28ce1f9 100644 --- a/subprojects/groovy-json/src/spec/test/json/StreamingJsonBuilderTest.groovy +++ b/subprojects/groovy-json/src/spec/test/json/StreamingJsonBuilderTest.groovy @@ -79,7 +79,7 @@ class StreamingJsonBuilderTest extends GroovyTestCase { .excludeNulls() .excludeFieldsByName('make', 'country', 'record') .excludeFieldsByType(Number) - .addConverter(URL) { url -> '"http://groovy-lang.org"' } + .addConverter(URL) { url -> "http://groovy-lang.org" } .build() StringWriter writer = new StringWriter() http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/test/groovy/groovy/json/CustomJsonGeneratorTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/test/groovy/groovy/json/CustomJsonGeneratorTest.groovy b/subprojects/groovy-json/src/test/groovy/groovy/json/CustomJsonGeneratorTest.groovy index 38a73fa..08b5178 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/CustomJsonGeneratorTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/CustomJsonGeneratorTest.groovy @@ -36,7 +36,8 @@ class CustomJsonGeneratorTest extends GroovyTestCase { assert generator.toJson(['one', null, 'two', null]) == '["one","two"]' assert generator.toJson(['Foo':'test1', 'BAR':'test2']) == '{"foo":"test1","bar":"test2"}' - assert generator.toJson(['foo': new CustomFoo()]) == '{"foo":"CustomFoo from CustomJsonConverter"}' + assert generator.toJson(['foo': new CustomFoo(c: { -> "CustomFoo from CustomJsonConverter" })]) == '{"foo":"CustomFoo from CustomJsonConverter"}' + assert generator.toJson(['foo': new CustomFoo(c: { -> JsonOutput.unescaped("{}") })]) == '{"foo":{}}' } static class CustomJsonOptions extends Options { @@ -75,15 +76,17 @@ class CustomJsonGeneratorTest extends GroovyTestCase { } @Override - CharSequence convert(Object value) { + Object convert(Object value) { return convert(value, null) } @Override - CharSequence convert(Object value, String key) { - return '"CustomFoo from CustomJsonConverter"' + Object convert(Object value, String key) { + return ((CustomFoo)value).c.call() } } - static class CustomFoo {} + static class CustomFoo { + Closure c + } } http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy b/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy index 167ce31..4c4f7c9 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy @@ -91,13 +91,13 @@ class DefaultJsonGeneratorTest extends GroovyTestCase { void testConverters() { def generator = new JsonGenerator.Options() .addConverter(JsonCyclicReference) { object, key -> - return '"JsonCyclicReference causes a stackoverflow"' + return "JsonCyclicReference causes a stackoverflow" } .addConverter(Date) { object -> - return '"4 score and 7 years ago"' + return "4 score and 7 years ago" } .addConverter(Calendar) { object -> - return '"22 days ago"' + return "22 days ago" } .build() http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/subprojects/groovy-json/src/test/groovy/groovy/json/JsonBuilderTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-json/src/test/groovy/groovy/json/JsonBuilderTest.groovy b/subprojects/groovy-json/src/test/groovy/groovy/json/JsonBuilderTest.groovy index 79c88e3..e057e18 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/JsonBuilderTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/JsonBuilderTest.groovy @@ -409,7 +409,7 @@ class JsonBuilderTest extends GroovyTestCase { .dateFormat('yyyyMM') .excludeFieldsByName('secretKey', 'creditCardNumber') .excludeFieldsByType(URL) - .addConverter(java.util.concurrent.atomic.AtomicBoolean) { ab -> ab.toString() } + .addConverter(java.util.concurrent.atomic.AtomicBoolean) { ab -> ab.get() } .build() def json = new JsonBuilder(generator) http://git-wip-us.apache.org/repos/asf/groovy/blob/0da08714/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 a3cf4d8..ec61dcd 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/StreamingJsonBuilderTest.groovy @@ -503,7 +503,7 @@ class StreamingJsonBuilderTest extends GroovyTestCase { .dateFormat('yyyyMM') .excludeFieldsByName('secretKey', 'creditCardNumber') .excludeFieldsByType(URL) - .addConverter(java.util.concurrent.atomic.AtomicBoolean) { ab -> ab.toString() } + .addConverter(java.util.concurrent.atomic.AtomicBoolean) { ab -> ab.get() } .build() new StringWriter().with { w -> @@ -529,7 +529,7 @@ class StreamingJsonBuilderTest extends GroovyTestCase { .dateFormat('yyyyMM') .excludeFieldsByName('secretKey', 'creditCardNumber') .excludeFieldsByType(URL) - .addConverter(java.util.concurrent.atomic.AtomicBoolean) { ab -> ab.toString() } + .addConverter(java.util.concurrent.atomic.AtomicBoolean) { ab -> ab.get() } .build() new StringWriter().with { w ->