PageRenderTime 87ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 120 lines | 81 code | 21 blank | 18 comment | 46 complexity | a1927e1a7f7df7ea79e13534cbc9371e 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.util.Collection;
  18. import com.alibaba.fastjson.annotation.JSONField;
  19. import com.alibaba.fastjson.util.FieldInfo;
  20. /**
  21. * @author wenshao<szujobs@hotmail.com>
  22. */
  23. public class ObjectFieldSerializer extends FieldSerializer {
  24. private ObjectSerializer fieldSerializer;
  25. private Class<?> runtimeFieldClass;
  26. private String format;
  27. private boolean writeNumberAsZero = false;
  28. boolean writeNullStringAsEmpty = false;
  29. boolean writeNullBooleanAsFalse = false;
  30. boolean writeNullListAsEmpty = false;
  31. boolean writeEnumUsingToString = false;
  32. public ObjectFieldSerializer(FieldInfo fieldInfo){
  33. super(fieldInfo);
  34. JSONField annotation = fieldInfo.getAnnotation(JSONField.class);
  35. if (annotation != null) {
  36. format = annotation.format();
  37. if (format.trim().length() == 0) {
  38. format = null;
  39. }
  40. for (SerializerFeature feature : annotation.serialzeFeatures()) {
  41. if (feature == SerializerFeature.WriteNullNumberAsZero) {
  42. writeNumberAsZero = true;
  43. } else if (feature == SerializerFeature.WriteNullStringAsEmpty) {
  44. writeNullStringAsEmpty = true;
  45. } else if (feature == SerializerFeature.WriteNullBooleanAsFalse) {
  46. writeNullBooleanAsFalse = true;
  47. } else if (feature == SerializerFeature.WriteNullListAsEmpty) {
  48. writeNullListAsEmpty = true;
  49. } else if (feature == SerializerFeature.WriteEnumUsingToString) {
  50. writeEnumUsingToString = true;
  51. }
  52. }
  53. }
  54. }
  55. @Override
  56. public void writeProperty(JSONSerializer serializer, Object propertyValue) throws Exception {
  57. writePrefix(serializer);
  58. if (format != null) {
  59. serializer.writeWithFormat(propertyValue, format);
  60. return;
  61. }
  62. if (fieldSerializer == null) {
  63. if (propertyValue == null) {
  64. runtimeFieldClass = this.getMethod().getReturnType();
  65. } else {
  66. runtimeFieldClass = propertyValue.getClass();
  67. }
  68. fieldSerializer = serializer.getObjectWriter(runtimeFieldClass);
  69. }
  70. if (propertyValue == null) {
  71. if (writeNumberAsZero && Number.class.isAssignableFrom(runtimeFieldClass)) {
  72. serializer.getWriter().write('0');
  73. return;
  74. } else if (writeNullStringAsEmpty && String.class == runtimeFieldClass) {
  75. serializer.getWriter().write("\"\"");
  76. return;
  77. } else if (writeNullBooleanAsFalse && Boolean.class == runtimeFieldClass) {
  78. serializer.getWriter().write("false");
  79. return;
  80. } else if (writeNullListAsEmpty && Collection.class.isAssignableFrom(runtimeFieldClass)) {
  81. serializer.getWriter().write("[]");
  82. return;
  83. }
  84. fieldSerializer.write(serializer, null, fieldInfo.getName(), null);
  85. return;
  86. }
  87. if (writeEnumUsingToString == true && runtimeFieldClass.isEnum()) {
  88. serializer.getWriter().writeString(((Enum<?>) propertyValue).name());
  89. return;
  90. }
  91. Class<?> valueClass = propertyValue.getClass();
  92. if (valueClass == runtimeFieldClass) {
  93. fieldSerializer.write(serializer, propertyValue, fieldInfo.getName(), fieldInfo.getFieldType());
  94. return;
  95. }
  96. ObjectSerializer valueSerializer = serializer.getObjectWriter(valueClass);
  97. valueSerializer.write(serializer, propertyValue, fieldInfo.getName(), null);
  98. }
  99. }