You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2015/09/08 20:54:52 UTC
incubator-groovy git commit: GROOVY-7519: Fixes JsonOuput.toJson()
leads to StackOverflowException (closes #105)
Repository: incubator-groovy
Updated Branches:
refs/heads/master a16ab5d86 -> 23fda89f6
GROOVY-7519: Fixes JsonOuput.toJson() leads to StackOverflowException (closes #105)
Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/23fda89f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/23fda89f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/23fda89f
Branch: refs/heads/master
Commit: 23fda89f69f248e050257748ff7cc553f38b046e
Parents: a16ab5d
Author: Esteban <eg...@gmail.com>
Authored: Sat Aug 29 23:20:07 2015 -0700
Committer: pascalschumacher <pa...@gmx.net>
Committed: Tue Sep 8 20:53:54 2015 +0200
----------------------------------------------------------------------
.../src/main/java/groovy/json/JsonOutput.java | 16 ++++++++++++++++
.../groovy/groovy/json/JsonOutputTest.groovy | 19 +++++++++++++++++++
2 files changed, 35 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/23fda89f/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java b/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java
index 9fd3209..d186c75 100644
--- a/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java
+++ b/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java
@@ -24,6 +24,7 @@ import groovy.lang.Closure;
import groovy.util.Expando;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
+import java.io.File;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -286,6 +287,21 @@ public class JsonOutput {
writeArray(objectClass, object, buffer);
} else if (Enum.class.isAssignableFrom(objectClass)) {
buffer.addQuoted(((Enum<?>) object).name());
+ }else if (File.class.isAssignableFrom(objectClass)){
+ Map<?, ?> properties = DefaultGroovyMethods.getProperties(object);
+ properties.remove("class");
+ properties.remove("declaringClass");
+ properties.remove("metaClass");
+ //Clean up all recursive references to File objects
+ Iterator<? extends Map.Entry<?, ?>> iterator = properties.entrySet().iterator();
+ while(iterator.hasNext()){
+ Map.Entry<?,?> entry = iterator.next();
+ if(entry.getValue() instanceof File){
+ iterator.remove();
+ }
+ }
+
+ writeMap(properties, buffer);
} else {
Map<?, ?> properties = DefaultGroovyMethods.getProperties(object);
properties.remove("class");
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/23fda89f/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy b/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
index 195fdbc..4c4efb6 100644
--- a/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
+++ b/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
@@ -415,6 +415,20 @@ class JsonOutputTest extends GroovyTestCase {
assert toJson({'\1' 0}) == '{"\\u0001":0}'
assert toJson({'\u0002' 0}) == '{"\\u0002":0}'
}
+
+ void testFile() {
+ def file = File.createTempFile('test', 'file-json')
+ def unusedProp = ['class', 'metaclass', 'declaringClass', 'canonicalFile', 'absoluteFile', 'parentFile']
+ def removeUnused = { map -> unusedProp.each { map.remove(it)}; map }
+
+ assert toJson(file) == toJson(removeUnused(file.properties))
+ def dir = File.createTempDir()
+ assert toJson(dir) == toJson(removeUnused(dir.properties))
+
+ def objectWithFile = new JsonEmbeddedFile(name: 'testFile', file: file)
+ assert toJson(objectWithFile) == "{\"file\":${toJson(file)},\"name\":\"testFile\"}".toString()
+ }
+
}
@Canonical
@@ -448,3 +462,8 @@ class JsonFoo {
enum JsonStreetKind {
street, boulevard, avenue
}
+
+class JsonEmbeddedFile {
+ String name
+ File file
+}