Skip to content

Commit

Permalink
Merge pull request #1761, add Locale serialize & deserialize support.
Browse files Browse the repository at this point in the history
Fixed #906
  • Loading branch information
nzomkxia authored and chickenlj committed May 14, 2018
1 parent 038b600 commit 9c4017e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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 @@ -358,6 +359,24 @@ public Object decode(Object jv) throws IOException {
}
};
GlobalDecoderMap.put(Date.class, d);

d = new Decoder() {
@Override
public Object decode(Object jv) throws IOException {
if (jv instanceof String) {
String[] items = ((String)jv).split("_");
if(items.length == 1){
return new Locale(items[0]);
}
if(items.length == 2){
return new Locale(items[0], items[1]);
}
return new Locale(items[0], items[1], items[2]);
}
return (Locale)null;
}
};
GlobalDecoderMap.put(Locale.class, d);
}

@Override
Expand Down Expand Up @@ -407,6 +426,8 @@ public void writeValue(Object obj, JSONWriter jb, boolean writeClass) throws IOE
writeValue(item, jb, writeClass);
}
jb.arrayEnd();
} else if(obj instanceof Locale) {
jb.valueString(obj.toString());
} else {
jb.objectBegin();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -169,6 +170,14 @@ public void testParse2Arguments() throws Exception {
assertEquals(test[0], 1);
}

@Test
public void testLocale() throws Exception {
Locale obj = Locale.US;
String str = JSON.json(obj);
assertEquals("\"en_US\"", str);
assertEquals(obj, JSON.parse(str, Locale.class));
}

public static class Bean1 {
public int[] array;
private String name, displayName;
Expand Down

0 comments on commit 9c4017e

Please sign in to comment.