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

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 168 lines | 125 code | 25 blank | 18 comment | 45 complexity | 64310eef377fc445c6eaa1a8114ec383 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.ParameterizedType;
  19. import java.lang.reflect.Type;
  20. import java.util.List;
  21. /**
  22. * @author wenshao<szujobs@hotmail.com>
  23. */
  24. public final class ListSerializer implements ObjectSerializer {
  25. public static final ListSerializer instance = new ListSerializer();
  26. public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
  27. throws IOException {
  28. boolean writeClassName = serializer.isEnabled(SerializerFeature.WriteClassName);
  29. SerializeWriter out = serializer.getWriter();
  30. Type elementType = null;
  31. if (writeClassName) {
  32. if (fieldType instanceof ParameterizedType) {
  33. ParameterizedType param = (ParameterizedType) fieldType;
  34. elementType = param.getActualTypeArguments()[0];
  35. }
  36. }
  37. if (object == null) {
  38. if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
  39. out.write("[]");
  40. } else {
  41. out.writeNull();
  42. }
  43. return;
  44. }
  45. List<?> list = (List<?>) object;
  46. final int size = list.size();
  47. int end = size - 1;
  48. if (end == -1) {
  49. out.append("[]");
  50. return;
  51. }
  52. SerialContext context = serializer.getContext();
  53. serializer.setContext(context, object, fieldName);
  54. ObjectSerializer itemSerializer = null;
  55. try {
  56. if (size > 1 && out.isEnabled(SerializerFeature.PrettyFormat)) {
  57. out.append('[');
  58. serializer.incrementIndent();
  59. for (int i = 0; i < size; ++i) {
  60. if (i != 0) {
  61. out.append(',');
  62. }
  63. serializer.println();
  64. Object item = list.get(i);
  65. if (item != null) {
  66. if (serializer.containsReference(item)) {
  67. serializer.writeReference(item);
  68. } else {
  69. itemSerializer = serializer.getObjectWriter(item.getClass());
  70. SerialContext itemContext = new SerialContext(context, object, fieldName);
  71. serializer.setContext(itemContext);
  72. itemSerializer.write(serializer, item, i, elementType);
  73. }
  74. } else {
  75. serializer.getWriter().writeNull();
  76. }
  77. }
  78. serializer.decrementIdent();
  79. serializer.println();
  80. out.append(']');
  81. return;
  82. }
  83. out.append('[');
  84. for (int i = 0; i < end; ++i) {
  85. Object item = list.get(i);
  86. if (item == null) {
  87. out.append("null,");
  88. } else {
  89. Class<?> clazz = item.getClass();
  90. if (clazz == Integer.class) {
  91. out.writeIntAndChar(((Integer) item).intValue(), ',');
  92. } else if (clazz == Long.class) {
  93. long val = ((Long) item).longValue();
  94. if (writeClassName) {
  95. out.writeLongAndChar(val, 'L');
  96. out.write(',');
  97. } else {
  98. out.writeLongAndChar(val, ',');
  99. }
  100. } else {
  101. SerialContext itemContext = new SerialContext(context, object, fieldName);
  102. serializer.setContext(itemContext);
  103. if (serializer.containsReference(item)) {
  104. serializer.writeReference(item);
  105. } else {
  106. itemSerializer = serializer.getObjectWriter(item.getClass());
  107. itemSerializer.write(serializer, item, i, elementType);
  108. }
  109. out.append(',');
  110. }
  111. }
  112. }
  113. Object item = list.get(end);
  114. if (item == null) {
  115. out.append("null]");
  116. } else {
  117. Class<?> clazz = item.getClass();
  118. if (clazz == Integer.class) {
  119. out.writeIntAndChar(((Integer) item).intValue(), ']');
  120. } else if (clazz == Long.class) {
  121. if (writeClassName) {
  122. out.writeLongAndChar(((Long) item).longValue(), 'L');
  123. out.write(']');
  124. } else {
  125. out.writeLongAndChar(((Long) item).longValue(), ']');
  126. }
  127. } else {
  128. SerialContext itemContext = new SerialContext(context, object, fieldName);
  129. serializer.setContext(itemContext);
  130. if (serializer.containsReference(item)) {
  131. serializer.writeReference(item);
  132. } else {
  133. itemSerializer = serializer.getObjectWriter(item.getClass());
  134. itemSerializer.write(serializer, item, end, elementType);
  135. }
  136. out.append(']');
  137. }
  138. }
  139. } finally {
  140. serializer.setContext(context);
  141. }
  142. }
  143. }