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

/components/camel-fastjson/src/main/java/org/apache/camel/component/fastjson/FastjsonDataFormat.java

https://github.com/gnodet/camel
Java | 221 lines | 161 code | 35 blank | 25 comment | 16 complexity | 95f8ff8154bbedba0886364b1beea91b MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.fastjson;
  18. import java.io.*;
  19. import java.lang.reflect.Type;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.alibaba.fastjson.JSON;
  23. import com.alibaba.fastjson.serializer.SerializerFeature;
  24. import com.alibaba.fastjson.support.config.FastJsonConfig;
  25. import org.apache.camel.CamelContext;
  26. import org.apache.camel.CamelContextAware;
  27. import org.apache.camel.Exchange;
  28. import org.apache.camel.Message;
  29. import org.apache.camel.spi.DataFormat;
  30. import org.apache.camel.spi.DataFormatContentTypeHeader;
  31. import org.apache.camel.spi.DataFormatName;
  32. import org.apache.camel.spi.Metadata;
  33. import org.apache.camel.spi.annotations.Dataformat;
  34. import org.apache.camel.support.service.ServiceSupport;
  35. /**
  36. * Marshal POJOs to JSON and back using <a href="https://github.com/alibaba/fastjson">Fastjson</a>
  37. */
  38. @Dataformat("json-fastjson")
  39. @Metadata(includeProperties = "unmarshalTypeName,prettyprint,contentTypeHeader")
  40. public class FastjsonDataFormat extends ServiceSupport
  41. implements DataFormat, DataFormatName, DataFormatContentTypeHeader, CamelContextAware {
  42. private CamelContext camelContext;
  43. private FastJsonConfig config;
  44. private Class<?> unmarshalType;
  45. private String unmarshalTypeName;
  46. private Type unmarshalGenericType;
  47. private boolean serializeNulls;
  48. private boolean prettyPrint;
  49. private String dateFormatPattern;
  50. private boolean contentTypeHeader = true;
  51. public FastjsonDataFormat() {
  52. this(Object.class);
  53. }
  54. public FastjsonDataFormat(Class<?> unmarshalType) {
  55. this(null, unmarshalType);
  56. }
  57. public FastjsonDataFormat(FastJsonConfig config, Class<?> unmarshalType) {
  58. this.config = config;
  59. this.unmarshalType = unmarshalType;
  60. }
  61. public FastjsonDataFormat(Type unmarshalGenericType) {
  62. this(null, unmarshalGenericType);
  63. }
  64. public FastjsonDataFormat(FastJsonConfig config, Type unmarshalGenericType) {
  65. this.config = config;
  66. this.unmarshalGenericType = unmarshalGenericType;
  67. }
  68. @Override
  69. public CamelContext getCamelContext() {
  70. return camelContext;
  71. }
  72. @Override
  73. public void setCamelContext(CamelContext camelContext) {
  74. this.camelContext = camelContext;
  75. }
  76. @Override
  77. public String getDataFormatName() {
  78. return "json-fastjson";
  79. }
  80. @Override
  81. public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
  82. int len = JSON.writeJSONString(stream,
  83. config.getCharset(),
  84. graph,
  85. config.getSerializeConfig(),
  86. config.getSerializeFilters(),
  87. config.getDateFormat(),
  88. JSON.DEFAULT_GENERATE_FEATURE,
  89. config.getSerializerFeatures());
  90. if (contentTypeHeader) {
  91. Message message = exchange.getMessage();
  92. message.setHeader(Exchange.CONTENT_TYPE, "application/json");
  93. message.setHeader(Exchange.CONTENT_LENGTH, len);
  94. }
  95. }
  96. @Override
  97. public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
  98. if (unmarshalGenericType == null) {
  99. return JSON.parseObject(stream, config.getCharset(), unmarshalType, config.getFeatures());
  100. } else {
  101. return JSON.parseObject(stream, config.getCharset(), unmarshalGenericType, config.getFeatures());
  102. }
  103. }
  104. @Override
  105. protected void doInit() throws Exception {
  106. if (unmarshalTypeName != null && (unmarshalType == null || unmarshalType == Object.class)) {
  107. unmarshalType = camelContext.getClassResolver().resolveClass(unmarshalTypeName);
  108. }
  109. }
  110. @Override
  111. protected void doStart() throws Exception {
  112. if (config == null) {
  113. List<SerializerFeature> serializerFeatureList = new ArrayList<>();
  114. config = new FastJsonConfig();
  115. if (prettyPrint) {
  116. serializerFeatureList.add(SerializerFeature.PrettyFormat);
  117. }
  118. if (serializeNulls) {
  119. serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
  120. serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
  121. serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
  122. serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
  123. serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
  124. }
  125. if (this.dateFormatPattern != null) {
  126. serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
  127. config.setDateFormat(this.dateFormatPattern);
  128. }
  129. config.setSerializerFeatures(serializerFeatureList.toArray(new SerializerFeature[0]));
  130. }
  131. }
  132. @Override
  133. protected void doStop() throws Exception {
  134. // noop
  135. }
  136. // Properties
  137. // -------------------------------------------------------------------------
  138. public Class<?> getUnmarshalType() {
  139. return this.unmarshalType;
  140. }
  141. public void setUnmarshalType(Class<?> unmarshalType) {
  142. this.unmarshalType = unmarshalType;
  143. }
  144. public String getUnmarshalTypeName() {
  145. return unmarshalTypeName;
  146. }
  147. public void setUnmarshalTypeName(String unmarshalTypeName) {
  148. this.unmarshalTypeName = unmarshalTypeName;
  149. }
  150. public Type getUnmarshalGenericType() {
  151. return this.unmarshalType;
  152. }
  153. public void setUnmarshalGenericType(Type unmarshalGenericType) {
  154. this.unmarshalGenericType = unmarshalGenericType;
  155. }
  156. public boolean isSerializeNulls() {
  157. return serializeNulls;
  158. }
  159. public void setSerializeNulls(boolean serializeNulls) {
  160. this.serializeNulls = serializeNulls;
  161. }
  162. public boolean isPrettyPrint() {
  163. return prettyPrint;
  164. }
  165. public void setPrettyPrint(boolean prettyPrint) {
  166. this.prettyPrint = prettyPrint;
  167. }
  168. public String getDateFormatPattern() {
  169. return dateFormatPattern;
  170. }
  171. public void setDateFormatPattern(String dateFormatPattern) {
  172. this.dateFormatPattern = dateFormatPattern;
  173. }
  174. public boolean isContentTypeHeader() {
  175. return contentTypeHeader;
  176. }
  177. /**
  178. * If enabled then JSON will set the Content-Type header to <tt>application/json</tt> when marshalling.
  179. */
  180. public void setContentTypeHeader(boolean contentTypeHeader) {
  181. this.contentTypeHeader = contentTypeHeader;
  182. }
  183. public FastJsonConfig getConfig() {
  184. return this.config;
  185. }
  186. }