Skip to content

Commit

Permalink
fix apache#400: SON.json(Locale) throw StackOverflowError, into the d…
Browse files Browse the repository at this point in the history
…ead loop
  • Loading branch information
takeseem committed Mar 14, 2017
1 parent 27917f2 commit f1dc4fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit f1dc4fa

Please sign in to comment.