PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/parser/deserializer/LocaleDeserializer.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 36 lines | 26 code | 10 blank | 0 comment | 6 complexity | bf78be1173630096f96cace548c5204e MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import java.util.Locale;
  4. import com.alibaba.fastjson.parser.DefaultJSONParser;
  5. import com.alibaba.fastjson.parser.JSONToken;
  6. public class LocaleDeserializer implements ObjectDeserializer {
  7. public final static LocaleDeserializer instance = new LocaleDeserializer();
  8. @SuppressWarnings("unchecked")
  9. public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
  10. String text = (String) parser.parse();
  11. if (text == null) {
  12. return null;
  13. }
  14. String[] items = text.split("_");
  15. if (items.length == 1) {
  16. return (T) new Locale(items[0]);
  17. }
  18. if (items.length == 2) {
  19. return (T) new Locale(items[0], items[1]);
  20. }
  21. return (T) new Locale(items[0], items[1], items[2]);
  22. }
  23. public int getFastMatchToken() {
  24. return JSONToken.LITERAL_STRING;
  25. }
  26. }