PageRenderTime 74ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 64 lines | 50 code | 14 blank | 0 comment | 11 complexity | 8487cbde5bf0e7e7aee7a7b489bd4438 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import java.net.InetAddress;
  4. import java.net.InetSocketAddress;
  5. import com.alibaba.fastjson.JSONException;
  6. import com.alibaba.fastjson.parser.DefaultJSONParser;
  7. import com.alibaba.fastjson.parser.JSONLexer;
  8. import com.alibaba.fastjson.parser.JSONToken;
  9. public class InetSocketAddressDeserializer implements ObjectDeserializer {
  10. public final static InetSocketAddressDeserializer instance = new InetSocketAddressDeserializer();
  11. @SuppressWarnings("unchecked")
  12. public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
  13. JSONLexer lexer = parser.getLexer();
  14. if (lexer.token() == JSONToken.NULL) {
  15. lexer.nextToken();
  16. return null;
  17. }
  18. parser.accept(JSONToken.LBRACE);
  19. InetAddress address = null;
  20. int port = 0;
  21. for (;;) {
  22. String key = lexer.symbol(parser.getSymbolTable());
  23. lexer.nextToken();
  24. if (key.equals("address")) {
  25. parser.accept(JSONToken.COLON);
  26. address = parser.parseObject(InetAddress.class);
  27. } else if (key.equals("port")) {
  28. parser.accept(JSONToken.COLON);
  29. if (lexer.token() != JSONToken.LITERAL_INT) {
  30. throw new JSONException("port is not int");
  31. }
  32. port = lexer.intValue();
  33. lexer.nextToken();
  34. } else {
  35. parser.accept(JSONToken.COLON);
  36. parser.parse();
  37. }
  38. if (lexer.token() == JSONToken.COMMA) {
  39. lexer.nextToken();
  40. continue;
  41. }
  42. break;
  43. }
  44. parser.accept(JSONToken.RBRACE);
  45. return (T) new InetSocketAddress(address, port);
  46. }
  47. public int getFastMatchToken() {
  48. return JSONToken.LBRACE;
  49. }
  50. }