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

https://github.com/alibaba/fastjson · Java · 136 lines · 102 code · 16 blank · 18 comment · 31 complexity · cec19d8d394614bd19b69193505f8a8b MD5 · raw file

  1. /*
  2. * Copyright 1999-2018 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 PrimitiveArraySerializer implements ObjectSerializer {
  23. public static PrimitiveArraySerializer instance = new PrimitiveArraySerializer();
  24. public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
  25. SerializeWriter out = serializer.out;
  26. if (object == null) {
  27. out.writeNull(SerializerFeature.WriteNullListAsEmpty);
  28. return;
  29. }
  30. if (object instanceof int[]) {
  31. int[] array = (int[]) object;
  32. out.write('[');
  33. for (int i = 0; i < array.length; ++i) {
  34. if (i != 0) {
  35. out.write(',');
  36. }
  37. out.writeInt(array[i]);
  38. }
  39. out.write(']');
  40. return;
  41. }
  42. if (object instanceof short[]) {
  43. short[] array = (short[]) object;
  44. out.write('[');
  45. for (int i = 0; i < array.length; ++i) {
  46. if (i != 0) {
  47. out.write(',');
  48. }
  49. out.writeInt(array[i]);
  50. }
  51. out.write(']');
  52. return;
  53. }
  54. if (object instanceof long[]) {
  55. long[] array = (long[]) object;
  56. out.write('[');
  57. for (int i = 0; i < array.length; ++i) {
  58. if (i != 0) {
  59. out.write(',');
  60. }
  61. out.writeLong(array[i]);
  62. }
  63. out.write(']');
  64. return;
  65. }
  66. if (object instanceof boolean[]) {
  67. boolean[] array = (boolean[]) object;
  68. out.write('[');
  69. for (int i = 0; i < array.length; ++i) {
  70. if (i != 0) {
  71. out.write(',');
  72. }
  73. out.write(array[i]);
  74. }
  75. out.write(']');
  76. return;
  77. }
  78. if (object instanceof float[]) {
  79. float[] array = (float[]) object;
  80. out.write('[');
  81. for (int i = 0; i < array.length; ++i) {
  82. if (i != 0) {
  83. out.write(',');
  84. }
  85. float item = array[i];
  86. if (Float.isNaN(item)) {
  87. out.writeNull();
  88. } else {
  89. out.append(Float.toString(item));
  90. }
  91. }
  92. out.write(']');
  93. return;
  94. }
  95. if (object instanceof double[]) {
  96. double[] array = (double[]) object;
  97. out.write('[');
  98. for (int i = 0; i < array.length; ++i) {
  99. if (i != 0) {
  100. out.write(',');
  101. }
  102. double item = array[i];
  103. if (Double.isNaN(item)) {
  104. out.writeNull();
  105. } else {
  106. out.append(Double.toString(item));
  107. }
  108. }
  109. out.write(']');
  110. return;
  111. }
  112. if (object instanceof byte[]) {
  113. byte[] array = (byte[]) object;
  114. out.writeByteArray(array);
  115. return;
  116. }
  117. char[] chars = (char[]) object;
  118. out.writeString(chars);
  119. }
  120. }