/src/main/java/com/alibaba/fastjson/support/retrofit/Retrofit2ConverterFactory.java

https://github.com/alibaba/fastjson · Java · 257 lines · 149 code · 29 blank · 79 comment · 2 complexity · 2cb98789793873c2b1ccaeba62ac568f MD5 · raw file

  1. package com.alibaba.fastjson.support.retrofit;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.parser.Feature;
  4. import com.alibaba.fastjson.parser.ParserConfig;
  5. import com.alibaba.fastjson.serializer.SerializeConfig;
  6. import com.alibaba.fastjson.serializer.SerializerFeature;
  7. import com.alibaba.fastjson.support.config.FastJsonConfig;
  8. import okhttp3.MediaType;
  9. import okhttp3.RequestBody;
  10. import okhttp3.ResponseBody;
  11. import retrofit2.Converter;
  12. import retrofit2.Retrofit;
  13. import java.io.IOException;
  14. import java.lang.annotation.Annotation;
  15. import java.lang.reflect.Type;
  16. /**
  17. * @author ligboy, wenshao
  18. * @author Victor.Zxy
  19. */
  20. public class Retrofit2ConverterFactory extends Converter.Factory {
  21. private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
  22. private FastJsonConfig fastJsonConfig;
  23. @Deprecated
  24. private static final Feature[] EMPTY_SERIALIZER_FEATURES = new Feature[0];
  25. @Deprecated
  26. private ParserConfig parserConfig = ParserConfig.getGlobalInstance();
  27. @Deprecated
  28. private int featureValues = JSON.DEFAULT_PARSER_FEATURE;
  29. @Deprecated
  30. private Feature[] features;
  31. @Deprecated
  32. private SerializeConfig serializeConfig;
  33. @Deprecated
  34. private SerializerFeature[] serializerFeatures;
  35. public Retrofit2ConverterFactory() {
  36. this.fastJsonConfig = new FastJsonConfig();
  37. }
  38. public Retrofit2ConverterFactory(FastJsonConfig fastJsonConfig) {
  39. this.fastJsonConfig = fastJsonConfig;
  40. }
  41. public static Retrofit2ConverterFactory create() {
  42. return create(new FastJsonConfig());
  43. }
  44. public static Retrofit2ConverterFactory create(FastJsonConfig fastJsonConfig) {
  45. if (fastJsonConfig == null) throw new NullPointerException("fastJsonConfig == null");
  46. return new Retrofit2ConverterFactory(fastJsonConfig);
  47. }
  48. @Override
  49. public Converter<ResponseBody, Object> responseBodyConverter(Type type, //
  50. Annotation[] annotations, //
  51. Retrofit retrofit) {
  52. return new ResponseBodyConverter<Object>(type);
  53. }
  54. @Override
  55. public Converter<Object, RequestBody> requestBodyConverter(Type type, //
  56. Annotation[] parameterAnnotations, //
  57. Annotation[] methodAnnotations, //
  58. Retrofit retrofit) {
  59. return new RequestBodyConverter<Object>();
  60. }
  61. public FastJsonConfig getFastJsonConfig() {
  62. return fastJsonConfig;
  63. }
  64. public Retrofit2ConverterFactory setFastJsonConfig(FastJsonConfig fastJsonConfig) {
  65. this.fastJsonConfig = fastJsonConfig;
  66. return this;
  67. }
  68. /**
  69. * Gets parser config.
  70. *
  71. * @return the parser config
  72. * @see FastJsonConfig#getParserConfig()
  73. * @deprecated
  74. */
  75. @Deprecated
  76. public ParserConfig getParserConfig() {
  77. return fastJsonConfig.getParserConfig();
  78. }
  79. /**
  80. * Sets parser config.
  81. *
  82. * @param config the config
  83. * @return the parser config
  84. * @see FastJsonConfig#setParserConfig(ParserConfig)
  85. * @deprecated
  86. */
  87. @Deprecated
  88. public Retrofit2ConverterFactory setParserConfig(ParserConfig config) {
  89. fastJsonConfig.setParserConfig(config);
  90. return this;
  91. }
  92. /**
  93. * Gets parser feature values.
  94. *
  95. * @return the parser feature values
  96. * @see JSON#DEFAULT_PARSER_FEATURE
  97. * @deprecated
  98. */
  99. @Deprecated
  100. public int getParserFeatureValues() {
  101. return JSON.DEFAULT_PARSER_FEATURE;
  102. }
  103. /**
  104. * Sets parser feature values.
  105. *
  106. * @param featureValues the feature values
  107. * @return the parser feature values
  108. * @see JSON#DEFAULT_PARSER_FEATURE
  109. * @deprecated
  110. */
  111. @Deprecated
  112. public Retrofit2ConverterFactory setParserFeatureValues(int featureValues) {
  113. return this;
  114. }
  115. /**
  116. * Get parser features feature [].
  117. *
  118. * @return the feature []
  119. * @see FastJsonConfig#getFeatures()
  120. * @deprecated
  121. */
  122. @Deprecated
  123. public Feature[] getParserFeatures() {
  124. return fastJsonConfig.getFeatures();
  125. }
  126. /**
  127. * Sets parser features.
  128. *
  129. * @param features the features
  130. * @return the parser features
  131. * @see FastJsonConfig#setFeatures(Feature...)
  132. * @deprecated
  133. */
  134. @Deprecated
  135. public Retrofit2ConverterFactory setParserFeatures(Feature[] features) {
  136. fastJsonConfig.setFeatures(features);
  137. return this;
  138. }
  139. /**
  140. * Gets serialize config.
  141. *
  142. * @return the serialize config
  143. * @see FastJsonConfig#getSerializeConfig()
  144. * @deprecated
  145. */
  146. @Deprecated
  147. public SerializeConfig getSerializeConfig() {
  148. return fastJsonConfig.getSerializeConfig();
  149. }
  150. /**
  151. * Sets serialize config.
  152. *
  153. * @param serializeConfig the serialize config
  154. * @return the serialize config
  155. * @see FastJsonConfig#setSerializeConfig(SerializeConfig)
  156. * @deprecated
  157. */
  158. @Deprecated
  159. public Retrofit2ConverterFactory setSerializeConfig(SerializeConfig serializeConfig) {
  160. fastJsonConfig.setSerializeConfig(serializeConfig);
  161. return this;
  162. }
  163. /**
  164. * Get serializer features serializer feature [].
  165. *
  166. * @return the serializer feature []
  167. * @see FastJsonConfig#getSerializerFeatures()
  168. * @deprecated
  169. */
  170. @Deprecated
  171. public SerializerFeature[] getSerializerFeatures() {
  172. return fastJsonConfig.getSerializerFeatures();
  173. }
  174. /**
  175. * Sets serializer features.
  176. *
  177. * @param features the features
  178. * @return the serializer features
  179. * @see FastJsonConfig#setSerializerFeatures(SerializerFeature...)
  180. * @deprecated
  181. */
  182. @Deprecated
  183. public Retrofit2ConverterFactory setSerializerFeatures(SerializerFeature[] features) {
  184. fastJsonConfig.setSerializerFeatures(features);
  185. return this;
  186. }
  187. final class ResponseBodyConverter<T> implements Converter<ResponseBody, T> {
  188. private Type type;
  189. ResponseBodyConverter(Type type) {
  190. this.type = type;
  191. }
  192. public T convert(ResponseBody value) throws IOException {
  193. try {
  194. return JSON.parseObject(value.bytes()
  195. , fastJsonConfig.getCharset()
  196. , type
  197. , fastJsonConfig.getParserConfig()
  198. , fastJsonConfig.getParseProcess()
  199. , JSON.DEFAULT_PARSER_FEATURE
  200. , fastJsonConfig.getFeatures()
  201. );
  202. } catch (Exception e) {
  203. throw new IOException("JSON parse error: " + e.getMessage(), e);
  204. } finally {
  205. value.close();
  206. }
  207. }
  208. }
  209. final class RequestBodyConverter<T> implements Converter<T, RequestBody> {
  210. RequestBodyConverter() {
  211. }
  212. public RequestBody convert(T value) throws IOException {
  213. try {
  214. byte[] content = JSON.toJSONBytesWithFastJsonConfig(fastJsonConfig.getCharset()
  215. , value
  216. , fastJsonConfig.getSerializeConfig()
  217. , fastJsonConfig.getSerializeFilters()
  218. , fastJsonConfig.getDateFormat()
  219. , JSON.DEFAULT_GENERATE_FEATURE
  220. , fastJsonConfig.getSerializerFeatures()
  221. );
  222. return RequestBody.create(MEDIA_TYPE, content);
  223. } catch (Exception e) {
  224. throw new IOException("Could not write JSON: " + e.getMessage(), e);
  225. }
  226. }
  227. }
  228. }