PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/serializer/JavaBeanSerializer.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 207 lines | 147 code | 41 blank | 19 comment | 35 complexity | c0f9022e2ae9cfb57d9b83952732ffa3 MD5 | raw file
  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.fastjson.serializer;
  17. import java.io.IOException;
  18. import java.lang.reflect.Field;
  19. import java.lang.reflect.Modifier;
  20. import java.lang.reflect.Type;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import com.alibaba.fastjson.JSONException;
  26. import com.alibaba.fastjson.util.FieldInfo;
  27. import com.alibaba.fastjson.util.TypeUtils;
  28. /**
  29. * @author wenshao<szujobs@hotmail.com>
  30. */
  31. public class JavaBeanSerializer implements ObjectSerializer {
  32. // serializers
  33. private final FieldSerializer[] getters;
  34. private final FieldSerializer[] sortedGetters;
  35. public FieldSerializer[] getGetters() {
  36. return getters;
  37. }
  38. public JavaBeanSerializer(Class<?> clazz){
  39. this(clazz, (Map<String, String>) null);
  40. }
  41. public JavaBeanSerializer(Class<?> clazz, String... aliasList){
  42. this(clazz, createAliasMap(aliasList));
  43. }
  44. static Map<String, String> createAliasMap(String... aliasList) {
  45. Map<String, String> aliasMap = new HashMap<String, String>();
  46. for (String alias : aliasList) {
  47. aliasMap.put(alias, alias);
  48. }
  49. return aliasMap;
  50. }
  51. public JavaBeanSerializer(Class<?> clazz, Map<String, String> aliasMap){
  52. {
  53. List<FieldSerializer> getterList = new ArrayList<FieldSerializer>();
  54. List<FieldInfo> fieldInfoList = TypeUtils.computeGetters(clazz, aliasMap, false);
  55. for (FieldInfo fieldInfo : fieldInfoList) {
  56. getterList.add(createFieldSerializer(fieldInfo));
  57. }
  58. getters = getterList.toArray(new FieldSerializer[getterList.size()]);
  59. }
  60. {
  61. List<FieldSerializer> getterList = new ArrayList<FieldSerializer>();
  62. List<FieldInfo> fieldInfoList = TypeUtils.computeGetters(clazz, aliasMap, true);
  63. for (FieldInfo fieldInfo : fieldInfoList) {
  64. getterList.add(createFieldSerializer(fieldInfo));
  65. }
  66. sortedGetters = getterList.toArray(new FieldSerializer[getterList.size()]);
  67. }
  68. }
  69. protected boolean isWriteClassName(JSONSerializer serializer, Object obj, Type fieldType, Object fieldName) {
  70. return serializer.isWriteClassName(fieldType, obj);
  71. }
  72. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  73. SerializeWriter out = serializer.getWriter();
  74. if (object == null) {
  75. out.writeNull();
  76. return;
  77. }
  78. if (serializer.containsReference(object)) {
  79. writeReference(serializer, object);
  80. return;
  81. }
  82. final FieldSerializer[] getters;
  83. if (out.isEnabled(SerializerFeature.SortField)) {
  84. getters = this.sortedGetters;
  85. } else {
  86. getters = this.getters;
  87. }
  88. SerialContext parent = serializer.getContext();
  89. serializer.setContext(parent, object, fieldName);
  90. try {
  91. out.append('{');
  92. if (getters.length > 0 && out.isEnabled(SerializerFeature.PrettyFormat)) {
  93. serializer.incrementIndent();
  94. serializer.println();
  95. }
  96. boolean commaFlag = false;
  97. if (isWriteClassName(serializer, object, fieldType, fieldName)) {
  98. Class<?> objClass = object.getClass();
  99. if (objClass != fieldType) {
  100. out.writeFieldName("@type");
  101. serializer.write(object.getClass());
  102. commaFlag = true;
  103. }
  104. }
  105. for (int i = 0; i < getters.length; ++i) {
  106. FieldSerializer fieldSerializer = getters[i];
  107. if (serializer.isEnabled(SerializerFeature.SkipTransientField)) {
  108. Field field = fieldSerializer.getField();
  109. if (field != null) {
  110. if (Modifier.isTransient(field.getModifiers())) {
  111. continue;
  112. }
  113. }
  114. }
  115. Object propertyValue = fieldSerializer.getPropertyValue(object);
  116. if (!FilterUtils.apply(serializer, object, fieldSerializer.getName(), propertyValue)) {
  117. continue;
  118. }
  119. String key = FilterUtils.processKey(serializer, object, fieldSerializer.getName(), propertyValue);
  120. Object originalValue = propertyValue;
  121. propertyValue = FilterUtils.processValue(serializer, object, fieldSerializer.getName(), propertyValue);
  122. if (propertyValue == null) {
  123. if ((!fieldSerializer.isWriteNull())
  124. && (!serializer.isEnabled(SerializerFeature.WriteMapNullValue))) {
  125. continue;
  126. }
  127. }
  128. if (commaFlag) {
  129. out.append(',');
  130. if (out.isEnabled(SerializerFeature.PrettyFormat)) {
  131. serializer.println();
  132. }
  133. }
  134. if (key != fieldSerializer.getName()) {
  135. out.writeFieldName(key);
  136. serializer.write(propertyValue);
  137. } else if (originalValue != propertyValue) {
  138. fieldSerializer.writePrefix(serializer);
  139. serializer.write(propertyValue);
  140. } else {
  141. fieldSerializer.writeProperty(serializer, propertyValue);
  142. }
  143. commaFlag = true;
  144. }
  145. if (getters.length > 0 && out.isEnabled(SerializerFeature.PrettyFormat)) {
  146. serializer.decrementIdent();
  147. serializer.println();
  148. }
  149. out.append('}');
  150. } catch (Exception e) {
  151. throw new JSONException("write javaBean error", e);
  152. } finally {
  153. serializer.setContext(parent);
  154. }
  155. }
  156. public void writeReference(JSONSerializer serializer, Object object) {
  157. serializer.writeReference(object);
  158. }
  159. public FieldSerializer createFieldSerializer(FieldInfo fieldInfo) {
  160. Class<?> clazz = fieldInfo.getFieldClass();
  161. if (clazz == Number.class) {
  162. return new NumberFieldSerializer(fieldInfo);
  163. }
  164. return new ObjectFieldSerializer(fieldInfo);
  165. }
  166. }