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

https://github.com/alibaba/fastjson · Java · 248 lines · 194 code · 36 blank · 18 comment · 59 complexity · 17f1f67f644c5b1fc9fbe51380c95c4d 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.Array;
  19. import java.lang.reflect.GenericArrayType;
  20. import java.lang.reflect.ParameterizedType;
  21. import java.lang.reflect.Type;
  22. import java.lang.reflect.TypeVariable;
  23. import com.alibaba.fastjson.JSONArray;
  24. import com.alibaba.fastjson.parser.DefaultJSONParser;
  25. import com.alibaba.fastjson.parser.JSONLexer;
  26. import com.alibaba.fastjson.parser.JSONToken;
  27. import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
  28. import com.alibaba.fastjson.util.TypeUtils;
  29. /**
  30. * @author wenshao[szujobs@hotmail.com]
  31. */
  32. public class ObjectArrayCodec implements ObjectSerializer, ObjectDeserializer {
  33. public static final ObjectArrayCodec instance = new ObjectArrayCodec();
  34. public ObjectArrayCodec(){
  35. }
  36. public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
  37. throws IOException {
  38. SerializeWriter out = serializer.out;
  39. Object[] array = (Object[]) object;
  40. if (object == null) {
  41. out.writeNull(SerializerFeature.WriteNullListAsEmpty);
  42. return;
  43. }
  44. int size = array.length;
  45. int end = size - 1;
  46. if (end == -1) {
  47. out.append("[]");
  48. return;
  49. }
  50. SerialContext context = serializer.context;
  51. serializer.setContext(context, object, fieldName, 0);
  52. try {
  53. Class<?> preClazz = null;
  54. ObjectSerializer preWriter = null;
  55. out.append('[');
  56. if (out.isEnabled(SerializerFeature.PrettyFormat)) {
  57. serializer.incrementIndent();
  58. serializer.println();
  59. for (int i = 0; i < size; ++i) {
  60. if (i != 0) {
  61. out.write(',');
  62. serializer.println();
  63. }
  64. serializer.writeWithFieldName(array[i], Integer.valueOf(i));
  65. }
  66. serializer.decrementIdent();
  67. serializer.println();
  68. out.write(']');
  69. return;
  70. }
  71. for (int i = 0; i < end; ++i) {
  72. Object item = array[i];
  73. if (item == null) {
  74. out.append("null,");
  75. } else {
  76. if (serializer.containsReference(item)) {
  77. serializer.writeReference(item);
  78. } else {
  79. Class<?> clazz = item.getClass();
  80. if (clazz == preClazz) {
  81. preWriter.write(serializer, item, i, null, 0);
  82. } else {
  83. preClazz = clazz;
  84. preWriter = serializer.getObjectWriter(clazz);
  85. preWriter.write(serializer, item, i, null, 0);
  86. }
  87. }
  88. out.append(',');
  89. }
  90. }
  91. Object item = array[end];
  92. if (item == null) {
  93. out.append("null]");
  94. } else {
  95. if (serializer.containsReference(item)) {
  96. serializer.writeReference(item);
  97. } else {
  98. serializer.writeWithFieldName(item, end);
  99. }
  100. out.append(']');
  101. }
  102. } finally {
  103. serializer.context = context;
  104. }
  105. }
  106. @SuppressWarnings({ "unchecked", "rawtypes" })
  107. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  108. final JSONLexer lexer = parser.lexer;
  109. int token = lexer.token();
  110. if (token == JSONToken.NULL) {
  111. lexer.nextToken(JSONToken.COMMA);
  112. return null;
  113. }
  114. if (token == JSONToken.LITERAL_STRING || token == JSONToken.HEX) {
  115. byte[] bytes = lexer.bytesValue();
  116. lexer.nextToken(JSONToken.COMMA);
  117. if (bytes.length == 0 && type != byte[].class) {
  118. return null;
  119. }
  120. return (T) bytes;
  121. }
  122. Class componentClass;
  123. Type componentType;
  124. if (type instanceof GenericArrayType) {
  125. GenericArrayType clazz = (GenericArrayType) type;
  126. componentType = clazz.getGenericComponentType();
  127. if (componentType instanceof TypeVariable) {
  128. TypeVariable typeVar = (TypeVariable) componentType;
  129. Type objType = parser.getContext().type;
  130. if (objType instanceof ParameterizedType) {
  131. ParameterizedType objParamType = (ParameterizedType) objType;
  132. Type objRawType = objParamType.getRawType();
  133. Type actualType = null;
  134. if (objRawType instanceof Class) {
  135. TypeVariable[] objTypeParams = ((Class) objRawType).getTypeParameters();
  136. for (int i = 0; i < objTypeParams.length; ++i) {
  137. if (objTypeParams[i].getName().equals(typeVar.getName())) {
  138. actualType = objParamType.getActualTypeArguments()[i];
  139. }
  140. }
  141. }
  142. if (actualType instanceof Class) {
  143. componentClass = (Class) actualType;
  144. } else {
  145. componentClass = Object.class;
  146. }
  147. } else {
  148. componentClass = TypeUtils.getClass(typeVar.getBounds()[0]);
  149. }
  150. } else {
  151. componentClass = TypeUtils.getClass(componentType);
  152. }
  153. } else {
  154. Class clazz = (Class) type;
  155. componentType = componentClass = clazz.getComponentType();
  156. }
  157. JSONArray array = new JSONArray();
  158. parser.parseArray(componentType, array, fieldName);
  159. return (T) toObjectArray(parser, componentClass, array);
  160. }
  161. @SuppressWarnings("unchecked")
  162. private <T> T toObjectArray(DefaultJSONParser parser, Class<?> componentType, JSONArray array) {
  163. if (array == null) {
  164. return null;
  165. }
  166. int size = array.size();
  167. Object objArray = Array.newInstance(componentType, size);
  168. for (int i = 0; i < size; ++i) {
  169. Object value = array.get(i);
  170. if (value == array) {
  171. Array.set(objArray, i, objArray);
  172. continue;
  173. }
  174. if (componentType.isArray()) {
  175. Object element;
  176. if (componentType.isInstance(value)) {
  177. element = value;
  178. } else {
  179. element = toObjectArray(parser, componentType, (JSONArray) value);
  180. }
  181. Array.set(objArray, i, element);
  182. } else {
  183. Object element = null;
  184. if (value instanceof JSONArray) {
  185. boolean contains = false;
  186. JSONArray valueArray = (JSONArray) value;
  187. int valueArraySize = valueArray.size();
  188. for (int y = 0; y < valueArraySize; ++y) {
  189. Object valueItem = valueArray.get(y);
  190. if (valueItem == array) {
  191. valueArray.set(i, objArray);
  192. contains = true;
  193. }
  194. }
  195. if (contains) {
  196. element = valueArray.toArray();
  197. }
  198. }
  199. if (element == null) {
  200. element = TypeUtils.cast(value, componentType, parser.getConfig());
  201. }
  202. Array.set(objArray, i, element);
  203. }
  204. }
  205. array.setRelatedArray(objArray);
  206. array.setComponentType(componentType);
  207. return (T) objArray; // TODO
  208. }
  209. public int getFastMatchToken() {
  210. return JSONToken.LBRACKET;
  211. }
  212. }