PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 88 lines | 73 code | 15 blank | 0 comment | 29 complexity | 8b9491cb84d933d522ebe79ddedd885b MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.awt.Font;
  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 FontDeserializer implements AutowiredObjectDeserializer {
  11. public final static FontDeserializer instance = new FontDeserializer();
  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(JSONToken.COMMA);
  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 size = 0, style = 0;
  24. String name = null;
  25. for (;;) {
  26. if (lexer.token() == JSONToken.RBRACE) {
  27. lexer.nextToken();
  28. break;
  29. }
  30. String key;
  31. if (lexer.token() == JSONToken.LITERAL_STRING) {
  32. key = lexer.stringVal();
  33. lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
  34. } else {
  35. throw new JSONException("syntax error");
  36. }
  37. if (key.equalsIgnoreCase("name")) {
  38. if (lexer.token() == JSONToken.LITERAL_STRING) {
  39. name = lexer.stringVal();
  40. lexer.nextToken();
  41. } else {
  42. throw new JSONException("syntax error");
  43. }
  44. } else if (key.equalsIgnoreCase("style")) {
  45. if (lexer.token() == JSONToken.LITERAL_INT) {
  46. style = lexer.intValue();
  47. lexer.nextToken();
  48. } else {
  49. throw new JSONException("syntax error");
  50. }
  51. } else if (key.equalsIgnoreCase("size")) {
  52. if (lexer.token() == JSONToken.LITERAL_INT) {
  53. size = lexer.intValue();
  54. lexer.nextToken();
  55. } else {
  56. throw new JSONException("syntax error");
  57. }
  58. } else {
  59. throw new JSONException("syntax error, " + key);
  60. }
  61. if (lexer.token() == JSONToken.COMMA) {
  62. lexer.nextToken(JSONToken.LITERAL_STRING);
  63. }
  64. }
  65. return (T) new Font(name, style, size);
  66. }
  67. public int getFastMatchToken() {
  68. return JSONToken.LBRACE;
  69. }
  70. public Set<Type> getAutowiredFor() {
  71. return Collections.<Type>singleton(Font.class);
  72. }
  73. }