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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 180 lines | 129 code | 32 blank | 19 comment | 41 complexity | 39d91705b7ed2c0905c43681d68a0d81 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.Type;
  19. import java.util.LinkedHashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.SortedMap;
  23. import java.util.TreeMap;
  24. /**
  25. * @author wenshao<szujobs@hotmail.com>
  26. */
  27. public class MapSerializer implements ObjectSerializer {
  28. public static MapSerializer instance = new MapSerializer();
  29. @SuppressWarnings({ "rawtypes", "unchecked" })
  30. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  31. SerializeWriter out = serializer.getWriter();
  32. if (object == null) {
  33. out.writeNull();
  34. return;
  35. }
  36. Map<?, ?> map = (Map<?, ?>) object;
  37. if (out.isEnabled(SerializerFeature.SortField)) {
  38. if ((!(map instanceof SortedMap)) && !(map instanceof LinkedHashMap)) {
  39. try {
  40. map = new TreeMap(map);
  41. } catch (Exception ex) {
  42. // skip
  43. }
  44. }
  45. }
  46. if (serializer.containsReference(object)) {
  47. serializer.writeReference(object);
  48. return;
  49. }
  50. SerialContext parent = serializer.getContext();
  51. serializer.setContext(parent, object, fieldName);
  52. try {
  53. out.write('{');
  54. serializer.incrementIndent();
  55. Class<?> preClazz = null;
  56. ObjectSerializer preWriter = null;
  57. boolean first = true;
  58. if (out.isEnabled(SerializerFeature.WriteClassName)) {
  59. out.writeFieldName("@type");
  60. out.writeString(object.getClass().getName());
  61. first = false;
  62. }
  63. for (Map.Entry entry : map.entrySet()) {
  64. Object value = entry.getValue();
  65. Object entryKey = entry.getKey();
  66. if (entryKey == null || entryKey instanceof String) {
  67. String key = (String) entryKey;
  68. List<PropertyPreFilter> namePreFilters = serializer.getPropertyPreFiltersDirect();
  69. if (namePreFilters != null) {
  70. boolean apply = true;
  71. for (PropertyPreFilter nameFilter : namePreFilters) {
  72. if (!nameFilter.apply(serializer, object, key)) {
  73. apply = false;
  74. break;
  75. }
  76. }
  77. if (!apply) {
  78. continue;
  79. }
  80. }
  81. List<PropertyFilter> propertyFilters = serializer.getPropertyFiltersDirect();
  82. if (propertyFilters != null) {
  83. boolean apply = true;
  84. for (PropertyFilter propertyFilter : propertyFilters) {
  85. if (!propertyFilter.apply(object, key, value)) {
  86. apply = false;
  87. break;
  88. }
  89. }
  90. if (!apply) {
  91. continue;
  92. }
  93. }
  94. List<NameFilter> nameFilters = serializer.getNameFiltersDirect();
  95. if (nameFilters != null) {
  96. for (NameFilter nameFilter : nameFilters) {
  97. key = nameFilter.process(object, key, value);
  98. }
  99. }
  100. List<ValueFilter> valueFilters = serializer.getValueFiltersDirect();
  101. if (valueFilters != null) {
  102. for (ValueFilter valueFilter : valueFilters) {
  103. value = valueFilter.process(object, key, value);
  104. }
  105. }
  106. if (value == null) {
  107. if (!serializer.isEnabled(SerializerFeature.WriteMapNullValue)) {
  108. continue;
  109. }
  110. }
  111. if (!first) {
  112. out.write(',');
  113. }
  114. if (out.isEnabled(SerializerFeature.PrettyFormat)) {
  115. serializer.println();
  116. }
  117. out.writeFieldName(key, true);
  118. } else {
  119. if (!first) {
  120. out.write(',');
  121. }
  122. serializer.write(entryKey);
  123. out.write(':');
  124. }
  125. first = false;
  126. if (value == null) {
  127. out.writeNull();
  128. continue;
  129. }
  130. Class<?> clazz = value.getClass();
  131. if (clazz == preClazz) {
  132. preWriter.write(serializer, value, entryKey, null);
  133. } else {
  134. preClazz = clazz;
  135. preWriter = serializer.getObjectWriter(clazz);
  136. preWriter.write(serializer, value, entryKey, null);
  137. }
  138. }
  139. } finally {
  140. serializer.setContext(parent);
  141. }
  142. serializer.decrementIdent();
  143. if (out.isEnabled(SerializerFeature.PrettyFormat) && map.size() > 0) {
  144. serializer.println();
  145. }
  146. out.write('}');
  147. }
  148. }