PageRenderTime 229ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 81 lines | 66 code | 15 blank | 0 comment | 25 complexity | 58bcc397c4ed38cfe01c7b9c63421427 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.awt.Rectangle;
  3. import java.lang.reflect.Type;
  4. import java.util.Collections;
  5. import java.util.Set;
  6. import com.alibaba.fastjson.JSONException;
  7. import com.alibaba.fastjson.parser.DefaultJSONParser;
  8. import com.alibaba.fastjson.parser.JSONScanner;
  9. import com.alibaba.fastjson.parser.JSONToken;
  10. public class RectangleDeserializer implements AutowiredObjectDeserializer {
  11. public final static RectangleDeserializer instance = new RectangleDeserializer();
  12. @SuppressWarnings("unchecked")
  13. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  14. JSONScanner lexer = (JSONScanner) parser.getLexer();
  15. if (lexer.token() == JSONToken.NULL) {
  16. lexer.nextToken();
  17. return null;
  18. }
  19. if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
  20. throw new JSONException("syntax error");
  21. }
  22. lexer.nextToken();
  23. int x = 0, y = 0, width = 0, height = 0;
  24. for (;;) {
  25. if (lexer.token() == JSONToken.RBRACE) {
  26. lexer.nextToken();
  27. break;
  28. }
  29. String key;
  30. if (lexer.token() == JSONToken.LITERAL_STRING) {
  31. key = lexer.stringVal();
  32. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  33. } else {
  34. throw new JSONException("syntax error");
  35. }
  36. int val;
  37. if (lexer.token() == JSONToken.LITERAL_INT) {
  38. val = lexer.intValue();
  39. lexer.nextToken();
  40. } else {
  41. throw new JSONException("syntax error");
  42. }
  43. if (key.equalsIgnoreCase("x")) {
  44. x = val;
  45. } else if (key.equalsIgnoreCase("y")) {
  46. y = val;
  47. } else if (key.equalsIgnoreCase("width")) {
  48. width = val;
  49. } else if (key.equalsIgnoreCase("height")) {
  50. height = val;
  51. } else {
  52. throw new JSONException("syntax error, " + key);
  53. }
  54. if (lexer.token() == JSONToken.COMMA) {
  55. lexer.nextToken(JSONToken.LITERAL_STRING);
  56. }
  57. }
  58. return (T) new Rectangle(x, y, width, height);
  59. }
  60. public int getFastMatchToken() {
  61. return JSONToken.LBRACE;
  62. }
  63. public Set<Type> getAutowiredFor() {
  64. return Collections.<Type>singleton(Rectangle.class);
  65. }
  66. }