PageRenderTime 113ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 97 lines | 73 code | 6 blank | 18 comment | 0 complexity | b3c57978e89c27f5a82c486c0e4a987f 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.Method;
  20. import com.alibaba.fastjson.annotation.JSONField;
  21. import com.alibaba.fastjson.util.FieldInfo;
  22. /**
  23. * @author wenshao<szujobs@hotmail.com>
  24. */
  25. public abstract class FieldSerializer implements Comparable<FieldSerializer> {
  26. protected final FieldInfo fieldInfo;
  27. private final String double_quoted_fieldPrefix;
  28. private final String single_quoted_fieldPrefix;
  29. private final String un_quoted_fieldPrefix;
  30. private boolean writeNull = false;
  31. public FieldSerializer(FieldInfo fieldInfo){
  32. super();
  33. this.fieldInfo = fieldInfo;
  34. fieldInfo.setAccessible(true);
  35. this.double_quoted_fieldPrefix = '"' + fieldInfo.getName() + "\":";
  36. this.single_quoted_fieldPrefix = '\'' + fieldInfo.getName() + "\':";
  37. this.un_quoted_fieldPrefix = fieldInfo.getName() + ":";
  38. JSONField annotation = fieldInfo.getAnnotation(JSONField.class);
  39. if (annotation != null) {
  40. for (SerializerFeature feature : annotation.serialzeFeatures()) {
  41. if (feature == SerializerFeature.WriteMapNullValue) {
  42. writeNull = true;
  43. }
  44. }
  45. }
  46. }
  47. public boolean isWriteNull() {
  48. return writeNull;
  49. }
  50. public Field getField() {
  51. return fieldInfo.getField();
  52. }
  53. public String getName() {
  54. return fieldInfo.getName();
  55. }
  56. public Method getMethod() {
  57. return fieldInfo.getMethod();
  58. }
  59. public void writePrefix(JSONSerializer serializer) throws IOException {
  60. SerializeWriter out = serializer.getWriter();
  61. if (serializer.isEnabled(SerializerFeature.QuoteFieldNames)) {
  62. if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
  63. out.write(single_quoted_fieldPrefix);
  64. } else {
  65. out.write(double_quoted_fieldPrefix);
  66. }
  67. } else {
  68. out.write(un_quoted_fieldPrefix);
  69. }
  70. }
  71. public int compareTo(FieldSerializer o) {
  72. return this.getName().compareTo(o.getName());
  73. }
  74. public Object getPropertyValue(Object object) throws Exception {
  75. return fieldInfo.get(object);
  76. }
  77. public abstract void writeProperty(JSONSerializer serializer, Object propertyValue) throws Exception;
  78. }