PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 111 lines | 73 code | 20 blank | 18 comment | 20 complexity | 54f83ec353fe9fa5958aa8c50b21225d 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. /**
  20. * @author wenshao<szujobs@hotmail.com>
  21. */
  22. public class ObjectArraySerializer implements ObjectSerializer {
  23. public static final ObjectArraySerializer instance = new ObjectArraySerializer();
  24. public ObjectArraySerializer(){
  25. }
  26. public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  27. SerializeWriter out = serializer.getWriter();
  28. Object[] array = (Object[]) object;
  29. if (object == null) {
  30. if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
  31. out.write("[]");
  32. } else {
  33. out.writeNull();
  34. }
  35. return;
  36. }
  37. int size = array.length;
  38. int end = size - 1;
  39. if (end == -1) {
  40. out.append("[]");
  41. return;
  42. }
  43. SerialContext context = serializer.getContext();
  44. serializer.setContext(context, object, fieldName);
  45. try {
  46. Class<?> preClazz = null;
  47. ObjectSerializer preWriter = null;
  48. out.append('[');
  49. if (out.isEnabled(SerializerFeature.PrettyFormat)) {
  50. serializer.incrementIndent();
  51. serializer.println();
  52. for (int i = 0; i < size; ++i) {
  53. if (i != 0) {
  54. out.write(',');
  55. serializer.println();
  56. }
  57. serializer.write(array[i]);
  58. }
  59. serializer.decrementIdent();
  60. serializer.println();
  61. out.write(']');
  62. return;
  63. }
  64. for (int i = 0; i < end; ++i) {
  65. Object item = array[i];
  66. if (item == null) {
  67. out.append("null,");
  68. } else {
  69. Class<?> clazz = item.getClass();
  70. if (clazz == preClazz) {
  71. preWriter.write(serializer, item, null, null);
  72. } else {
  73. preClazz = clazz;
  74. preWriter = serializer.getObjectWriter(clazz);
  75. preWriter.write(serializer, item, null, null);
  76. }
  77. out.append(',');
  78. }
  79. }
  80. Object item = array[end];
  81. if (item == null) {
  82. out.append("null]");
  83. } else {
  84. serializer.write(item);
  85. out.append(']');
  86. }
  87. } finally {
  88. serializer.setContext(context);
  89. }
  90. }
  91. }