[
https://issues.apache.org/jira/browse/GROOVY-7682?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15275358#comment-15275358
]
Andre Steingress commented on GROOVY-7682:
------------------------------------------
When having a look at Jackson, I saw they solve the issue of self referencing
objects with either annotations (in Jackson 2) or a specific "module
configuration" in case of Joda Time (= specific support for it).
So I came about this approach for {{JsonOutput}}:
We introduce new {{toJson(..., Map)}} methods that will allow to specify a map
as 2nd parameter. The map is treated as configuration map (similar to the map
used in the ToString AST xform) and allows to exclude properties of specified
classes.
An example can be found here:
https://github.com/andresteingress/groovy/blob/e1141a2f6d0e324b6a416b0a38df586da17c1406/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy#L430
The implementation has support for specifying a list of properties or only a
single property {{String}}, thus giving some flexibility in configuration style
and footprint.
I like this approach as it could give more flexibility for additional
configuration entries for the future. However, the disadvantage of this
approach is that it does not directly solve the {{StackOverflowException}},
instead the caller needs to know which properties cause the exception to
exlcude them in the configuration (which is tricky and maybe can not be
demanded).
For introducing the configuration map, I am not totally sure right now which
toJson methods should actually get that new parameter (besides
{{toJson(Object)}}), but maybe that's already details.
I also should note here: from my pov it does not make sense to demand the same
or equal functionality from {{JsonOutput}} as dedicated JSON libraries like
Jackson provide, but a little bit more flexibility in terms of configuring
JsonOutput would make it maybe more applicable in certain cases.
What do you guys think about that - or any other approaches how to solve this
issue?
> JsonBuilder or JsonOutput incompatible with JodaTime
> ----------------------------------------------------
>
> Key: GROOVY-7682
> URL: https://issues.apache.org/jira/browse/GROOVY-7682
> Project: Groovy
> Issue Type: Bug
> Components: JSON
> Affects Versions: 2.4.5
> Environment: JVM: 1.8.0_65 (Oracle)
> Mac OS X 10.11.1 (El Capitan)
> Reporter: Jean-Philippe Pellerin
> Labels: DateTime,, Json
>
> Using groovy's built-in methods to serialize Joda DateTime to JSON always
> conclude in a {{StackOverflowError}}.
> {code:title=ExampleMethod|type=java}
> package com.mgd
> import groovy.json.JsonOutput
> import org.joda.time.DateTime
> import org.joda.time.format.DateTimeFormat
> import org.joda.time.format.DateTimeFormatter
> import com.sun.beans.util.Cache.Kind.Strong
> class JodaBugJson {
> public static void main(String[] args) {
> DateTimeFormatter formatter =
> DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS z")
> DateTime dt = formatter.parseDateTime("2015-11-20 13:37:21.123
> EST")
> // String json = new JsonBuilder(dt).toString()
> //same outcome
> Strong json = JsonOutput.toJson(dt)
> println "\n${json}\n"
> }
> }
> {code}
> Running the above will result in :
> {code: title=Error}
> Exception in thread "main" java.lang.StackOverflowError
> at
> org.codehaus.groovy.reflection.ClassInfo.getClassInfo(ClassInfo.java:119)
> at
> org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:261)
> at
> org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:871)
> at
> org.codehaus.groovy.runtime.DefaultGroovyMethods.getMetaPropertyValues(DefaultGroovyMethods.java:364)
> at
> org.codehaus.groovy.runtime.DefaultGroovyMethods.getProperties(DefaultGroovyMethods.java:383)
> at groovy.json.JsonOutput.writeObject(JsonOutput.java:290)
> at groovy.json.JsonOutput.writeMap(JsonOutput.java:424)
> at groovy.json.JsonOutput.writeObject(JsonOutput.java:294)
> at groovy.json.JsonOutput.writeMap(JsonOutput.java:424)
> at groovy.json.JsonOutput.writeObject(JsonOutput.java:294)
> at groovy.json.JsonOutput.writeMap(JsonOutput.java:424)
> ...
> {code}
> Whether this be only the date, or an object including a Joda DateTime the
> outcome is the same.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)