JsonGenerator.Options allow registering a custom Converter
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/6f39e27d Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/6f39e27d Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/6f39e27d Branch: refs/heads/parrot Commit: 6f39e27d75f876a504638bbccd4dddfb44fb41b9 Parents: ca6beb8 Author: John Wagenleitner <jwagenleit...@apache.org> Authored: Tue Nov 8 17:12:15 2016 -0800 Committer: John Wagenleitner <jwagenleit...@apache.org> Committed: Tue Nov 8 17:12:15 2016 -0800 ---------------------------------------------------------------------- .../main/java/groovy/json/JsonGenerator.java | 16 +++++++++++-- .../groovy/json/DefaultJsonGeneratorTest.groovy | 24 +++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/6f39e27d/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 cea8b4b..c5b13a3 100644 --- a/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java +++ b/subprojects/groovy-json/src/main/java/groovy/json/JsonGenerator.java @@ -198,6 +198,19 @@ public interface JsonGenerator { } /** + * Registers a converter that will be called when a type it handles is encountered. + * + * @param converter to register + * @return a reference to this {@code Options} instance + */ + public Options addConverter(Converter converter) { + if (converter != null) { + converters.add(converter); + } + return this; + } + + /** * Registers a closure that will be called when the specified type or subtype * is serialized. * @@ -245,8 +258,7 @@ public interface JsonGenerator { if (converters.contains(converter)) { converters.remove(converter); } - converters.add(converter); - return this; + return addConverter(converter); } /** http://git-wip-us.apache.org/repos/asf/groovy/blob/6f39e27d/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 c1c17e2..027497e 100644 --- a/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy +++ b/subprojects/groovy-json/src/test/groovy/groovy/json/DefaultJsonGeneratorTest.groovy @@ -88,7 +88,7 @@ class DefaultJsonGeneratorTest extends GroovyTestCase { } } - void testConverters() { + void testClosureConverters() { def generator = new JsonGenerator.Options() .addConverter(JsonCyclicReference) { object, key -> return "JsonCyclicReference causes a stackoverflow" @@ -118,6 +118,28 @@ class DefaultJsonGeneratorTest extends GroovyTestCase { assert generator.toJson([timeline: Calendar.getInstance()]) == '{"timeline":"22 days ago"}' } + void testCustomConverters() { + def converter = new JsonGenerator.Converter() { + @Override + boolean handles(Class<?> type) { Date.class == type } + @Override + Object convert(Object value, String key) { '42' } + } + + def generator = new JsonGenerator.Options() + .addConverter(converter) + .build() + + assert generator.toJson([new Date()]) == '["42"]' + + def mapConverter = [handles: { Date.class == it }, convert: { obj, key -> 7 }] + generator = new JsonGenerator.Options() + .addConverter(mapConverter as JsonGenerator.Converter) + .build() + + assert generator.toJson([new Date()]) == '[7]' + } + void testConverterAddedLastTakesPrecedence() { def options = new JsonGenerator.Options() def c1 = { 'c1' }