From f1dc4fae59823aeafedb7499d68005e25ce368f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B5=A9?= Date: Tue, 14 Mar 2017 11:13:24 +0800 Subject: [PATCH] fix #400: SON.json(Locale) throw StackOverflowError, into the dead loop --- .../common/json/GenericJSONConverter.java | 5 ++++ .../alibaba/dubbo/common/json/JSONTest.java | 26 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/json/GenericJSONConverter.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/json/GenericJSONConverter.java index d2af6ec0491..c8566fa247d 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/json/GenericJSONConverter.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/json/GenericJSONConverter.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -61,6 +62,10 @@ else if( obj instanceof JSONNode ) { ((JSONNode)obj).writeJSON(this, jb, writeClass); } + else if( obj instanceof Locale ) + { + jb.valueString(obj.toString()); //fix-JSON.json(Locale) throw StackOverflowError(into the dead loop) + } else if( c.isEnum() ) { jb.valueString(((Enum)obj).name()); diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/json/JSONTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/json/JSONTest.java index b9c89ab3e3d..3c2859d21ec 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/json/JSONTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/json/JSONTest.java @@ -20,15 +20,39 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import junit.framework.Assert; -import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.assertEquals; public class JSONTest { + + @Test + public void testLocale() throws Exception { + Locale locale = Locale.US; + String result = JSON.json(locale); + //check JSON.json(Locale) into the dead loop + /** + java.lang.StackOverflowError + at java.lang.StringBuilder.append(StringBuilder.java:136) + at sun.util.locale.provider.LocaleResources.getLocaleName(LocaleResources.java:230) + at java.util.Locale.getDisplayName(Locale.java:1879) + at java.util.Locale.getDisplayName(Locale.java:1845) + at com.alibaba.dubbo.common.bytecode.Wrapper1.getPropertyValue(Wrapper1.java) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:126) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:73) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:130) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:73) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:130) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:73) + at com.alibaba.dubbo.common.json.GenericJSONConverter.writeValue(GenericJSONConverter.java:130) + */ + Assert.assertEquals(result, JSON.json(locale.toString())); + } + @Test public void testException() throws Exception { MyException e = new MyException("001", "AAAAAAAA");