Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Locale serialize & deserialize support #1761

Merged
merged 1 commit into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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