Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C1D78200BDF for ; Thu, 10 Nov 2016 16:13:39 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id C011C160AF6; Thu, 10 Nov 2016 15:13:39 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E1E23160B2C for ; Thu, 10 Nov 2016 16:13:36 +0100 (CET) Received: (qmail 49238 invoked by uid 500); 10 Nov 2016 15:13:36 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 47633 invoked by uid 99); 10 Nov 2016 15:13:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Nov 2016 15:13:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id CBDB0E00E5; Thu, 10 Nov 2016 15:13:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sunlan@apache.org To: commits@groovy.apache.org Date: Thu, 10 Nov 2016 15:14:16 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [44/50] [abbrv] groovy git commit: If a converter is found, further process the result (closes #457) archived-at: Thu, 10 Nov 2016 15:13:40 -0000 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 Authored: Sat Nov 5 18:36:49 2016 -0400 Committer: John Wagenleitner 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 closure; + protected final Closure closure; protected final int paramCount; - protected ClosureConverter(Class type, Closure 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 { *

          *     def generator = new groovy.json.JsonGenerator.Options()
          *                         .addConverter(URL) { URL u ->
-         *                             "\"${u.getHost()}\""
+         *                             u.getHost()
          *                         }
          *                         .build()
          *
@@ -251,7 +251,7 @@ public interface JsonGenerator {
          */
         public  Options addConverter(Class type,
                                         @ClosureParams(value=FromString.class, options={"T","T,String"})
-                                        Closure 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 ->