PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 104 lines | 68 code | 18 blank | 18 comment | 22 complexity | ff5eff556a49f8a25c029da01f0017e0 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.Collection;
  21. import java.util.HashSet;
  22. import java.util.TreeSet;
  23. /**
  24. * @author wenshao<szujobs@hotmail.com>
  25. */
  26. public class CollectionSerializer implements ObjectSerializer {
  27. public final static CollectionSerializer instance = new CollectionSerializer();
  28. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  29. SerializeWriter out = serializer.getWriter();
  30. if (object == null) {
  31. if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
  32. out.write("[]");
  33. } else {
  34. out.writeNull();
  35. }
  36. return;
  37. }
  38. Type elementType = null;
  39. if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
  40. if (fieldType instanceof ParameterizedType) {
  41. ParameterizedType param = (ParameterizedType) fieldType;
  42. elementType = param.getActualTypeArguments()[0];
  43. }
  44. }
  45. Collection<?> collection = (Collection<?>) object;
  46. SerialContext context = serializer.getContext();
  47. serializer.setContext(context, object, fieldName);
  48. if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
  49. if (HashSet.class == collection.getClass()) {
  50. out.append("Set");
  51. } else if (TreeSet.class == collection.getClass()) {
  52. out.append("TreeSet");
  53. }
  54. }
  55. try {
  56. int i = 0;
  57. out.append('[');
  58. for (Object item : collection) {
  59. if (i++ != 0) {
  60. out.append(',');
  61. }
  62. if (item == null) {
  63. out.writeNull();
  64. continue;
  65. }
  66. Class<?> clazz = item.getClass();
  67. if (clazz == Integer.class) {
  68. out.writeInt(((Integer) item).intValue());
  69. continue;
  70. }
  71. if (clazz == Long.class) {
  72. out.writeLong(((Long) item).longValue());
  73. if (out.isEnabled(SerializerFeature.WriteClassName)) {
  74. out.write('L');
  75. }
  76. continue;
  77. }
  78. ObjectSerializer itemSerializer = serializer.getObjectWriter(clazz);
  79. itemSerializer.write(serializer, item, i - 1, elementType);
  80. }
  81. out.append(']');
  82. } finally {
  83. serializer.setContext(context);
  84. }
  85. }
  86. }