PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 100 lines | 74 code | 26 blank | 0 comment | 7 complexity | f472d80a6b30a810e0549bf8e6f92c22 MD5 | raw file
  1. package com.alibaba.fastjson.parser.deserializer;
  2. import java.lang.reflect.Type;
  3. import java.util.Map;
  4. import com.alibaba.fastjson.JSONException;
  5. import com.alibaba.fastjson.parser.DefaultJSONParser;
  6. import com.alibaba.fastjson.parser.Feature;
  7. import com.alibaba.fastjson.parser.JSONScanner;
  8. import com.alibaba.fastjson.parser.ParserConfig;
  9. import com.alibaba.fastjson.util.FieldInfo;
  10. public abstract class ASMJavaBeanDeserializer implements ObjectDeserializer {
  11. protected InnerJavaBeanDeserializer serializer;
  12. public ASMJavaBeanDeserializer(ParserConfig mapping, Class<?> clazz){
  13. serializer = new InnerJavaBeanDeserializer(mapping, clazz);
  14. serializer.getFieldDeserializerMap();
  15. }
  16. public abstract Object createInstance(DefaultJSONParser parser, Type type);
  17. public InnerJavaBeanDeserializer getInnterSerializer() {
  18. return serializer;
  19. }
  20. @SuppressWarnings("unchecked")
  21. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  22. return (T) serializer.deserialze(parser, type, fieldName);
  23. }
  24. public int getFastMatchToken() {
  25. return serializer.getFastMatchToken();
  26. }
  27. public Object createInstance(DefaultJSONParser parser) {
  28. return serializer.createInstance(parser, serializer.getClazz());
  29. }
  30. public FieldDeserializer createFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) {
  31. return mapping.createFieldDeserializer(mapping, clazz, fieldInfo);
  32. }
  33. public FieldDeserializer getFieldDeserializer(String name) {
  34. return serializer.getFieldDeserializerMap().get(name);
  35. }
  36. public Type getFieldType(String name) {
  37. return serializer.getFieldDeserializerMap().get(name).getFieldType();
  38. }
  39. public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues) {
  40. JSONScanner lexer = (JSONScanner) parser.getLexer(); // xxx
  41. Map<String, FieldDeserializer> feildDeserializerMap = serializer.getFieldDeserializerMap();
  42. FieldDeserializer fieldDeserializer = feildDeserializerMap.get(key);
  43. if (fieldDeserializer == null) {
  44. for (Map.Entry<String, FieldDeserializer> entry : feildDeserializerMap.entrySet()) {
  45. if (entry.getKey().equalsIgnoreCase(key)) {
  46. fieldDeserializer = entry.getValue();
  47. break;
  48. }
  49. }
  50. }
  51. if (fieldDeserializer == null) {
  52. if (!parser.isEnabled(Feature.IgnoreNotMatch)) {
  53. throw new JSONException("setter not found, class " + serializer.getClass() + ", property " + key);
  54. }
  55. lexer.nextTokenWithColon();
  56. parser.parse(); // skip
  57. return false;
  58. }
  59. lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken());
  60. fieldDeserializer.parseField(parser, object, objectType, fieldValues);
  61. return true;
  62. }
  63. public final class InnerJavaBeanDeserializer extends JavaBeanDeserializer {
  64. private InnerJavaBeanDeserializer(ParserConfig mapping, Class<?> clazz){
  65. super(mapping, clazz);
  66. }
  67. public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues) {
  68. return ASMJavaBeanDeserializer.this.parseField(parser, key, object, objectType, fieldValues);
  69. }
  70. public FieldDeserializer createFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) {
  71. return ASMJavaBeanDeserializer.this.createFieldDeserializer(mapping, clazz, fieldInfo);
  72. }
  73. }
  74. }