PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 122 lines | 106 code | 14 blank | 2 comment | 76 complexity | 9802acd8b87582dcadcc3fc8e1d8e998 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import com.alibaba.fastjson.JSONException;
  4. import com.alibaba.fastjson.parser.DefaultJSONParser;
  5. import com.alibaba.fastjson.parser.Feature;
  6. import com.alibaba.fastjson.parser.JSONLexer;
  7. import com.alibaba.fastjson.parser.JSONToken;
  8. public class StackTraceElementDeserializer implements ObjectDeserializer {
  9. public final static StackTraceElementDeserializer instance = new StackTraceElementDeserializer();
  10. @SuppressWarnings("unchecked")
  11. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  12. JSONLexer lexer = parser.getLexer();
  13. if (lexer.token() == JSONToken.NULL) {
  14. lexer.nextToken();
  15. return null;
  16. }
  17. if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
  18. throw new JSONException("syntax error: " + JSONToken.name(lexer.token()));
  19. }
  20. String declaringClass = null;
  21. String methodName = null;
  22. String fileName = null;
  23. int lineNumber = 0;
  24. for (;;) {
  25. // lexer.scanSymbol
  26. String key = lexer.scanSymbol(parser.getSymbolTable());
  27. if (key == null) {
  28. if (lexer.token() == JSONToken.RBRACE) {
  29. lexer.nextToken(JSONToken.COMMA);
  30. break;
  31. }
  32. if (lexer.token() == JSONToken.COMMA) {
  33. if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
  34. continue;
  35. }
  36. }
  37. }
  38. lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
  39. if (key == "className") {
  40. if (lexer.token() == JSONToken.NULL) {
  41. declaringClass = null;
  42. } else if (lexer.token() == JSONToken.LITERAL_STRING) {
  43. declaringClass = lexer.stringVal();
  44. } else {
  45. throw new JSONException("syntax error");
  46. }
  47. } else if (key == "methodName") {
  48. if (lexer.token() == JSONToken.NULL) {
  49. methodName = null;
  50. } else if (lexer.token() == JSONToken.LITERAL_STRING) {
  51. methodName = lexer.stringVal();
  52. } else {
  53. throw new JSONException("syntax error");
  54. }
  55. } else if (key == "fileName") {
  56. if (lexer.token() == JSONToken.NULL) {
  57. fileName = null;
  58. } else if (lexer.token() == JSONToken.LITERAL_STRING) {
  59. fileName = lexer.stringVal();
  60. } else {
  61. throw new JSONException("syntax error");
  62. }
  63. } else if (key == "lineNumber") {
  64. if (lexer.token() == JSONToken.NULL) {
  65. lineNumber = 0;
  66. } else if (lexer.token() == JSONToken.LITERAL_INT) {
  67. lineNumber = lexer.intValue();
  68. } else {
  69. throw new JSONException("syntax error");
  70. }
  71. } else if (key == "nativeMethod") {
  72. if (lexer.token() == JSONToken.NULL) {
  73. lexer.nextToken(JSONToken.COMMA);
  74. } else if (lexer.token() == JSONToken.TRUE) {
  75. lexer.nextToken(JSONToken.COMMA);
  76. } else if (lexer.token() == JSONToken.FALSE) {
  77. lexer.nextToken(JSONToken.COMMA);
  78. } else {
  79. throw new JSONException("syntax error");
  80. }
  81. } else if (key == "@type") {
  82. if (lexer.token() == JSONToken.NULL) {
  83. // skip
  84. } else if (lexer.token() == JSONToken.LITERAL_STRING) {
  85. String elementType = lexer.stringVal();
  86. if (!elementType.equals("java.lang.StackTraceElement")) {
  87. throw new JSONException("syntax error : " + elementType);
  88. }
  89. } else {
  90. throw new JSONException("syntax error");
  91. }
  92. } else {
  93. throw new JSONException("syntax error : " + key);
  94. }
  95. if (lexer.token() == JSONToken.COMMA) {
  96. continue;
  97. }
  98. if (lexer.token() == JSONToken.RBRACE) {
  99. lexer.nextToken(JSONToken.COMMA);
  100. break;
  101. }
  102. }
  103. return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
  104. }
  105. public int getFastMatchToken() {
  106. return JSONToken.LBRACE;
  107. }
  108. }