PageRenderTime 222ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/matticala/apache-camel
Java | 188 lines | 130 code | 32 blank | 26 comment | 10 complexity | 503cae0a5c0d52c969765071b60f3747 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.Exchange;
  26. import org.apache.camel.Message;
  27. import org.apache.camel.spi.DataFormat;
  28. import org.apache.camel.spi.DataFormatName;
  29. import org.apache.camel.support.ServiceSupport;
  30. /**
  31. * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat})
  32. * using <a href="https://github.com/alibaba/fastjson">Fastjson</a> to marshal to and from JSON.
  33. */
  34. public class FastjsonDataFormat extends ServiceSupport implements DataFormat, DataFormatName {
  35. private FastJsonConfig config;
  36. private Class<?> unmarshalType;
  37. private Type unmarshalGenericType;
  38. private boolean serializeNulls;
  39. private boolean prettyPrint;
  40. private String dateFormatPattern;
  41. private boolean contentTypeHeader = true;
  42. public FastjsonDataFormat() {
  43. this(Object.class);
  44. }
  45. public FastjsonDataFormat(Class<?> unmarshalType) {
  46. this(null, unmarshalType);
  47. }
  48. public FastjsonDataFormat(FastJsonConfig config, Class<?> unmarshalType) {
  49. this.config = config;
  50. this.unmarshalType = unmarshalType;
  51. }
  52. public FastjsonDataFormat(Type unmarshalGenericType) {
  53. this(null, unmarshalGenericType);
  54. }
  55. public FastjsonDataFormat(FastJsonConfig config, Type unmarshalGenericType) {
  56. this.config = config;
  57. this.unmarshalGenericType = unmarshalGenericType;
  58. }
  59. @Override
  60. public String getDataFormatName() {
  61. return "json-fastjson";
  62. }
  63. @Override
  64. public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
  65. int len = JSON.writeJSONString(stream,
  66. config.getCharset(),
  67. graph,
  68. config.getSerializeConfig(),
  69. config.getSerializeFilters(),
  70. config.getDateFormat(),
  71. JSON.DEFAULT_GENERATE_FEATURE,
  72. config.getSerializerFeatures());
  73. if (contentTypeHeader) {
  74. Message message = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
  75. message.setHeader(Exchange.CONTENT_TYPE, "application/json");
  76. message.setHeader(Exchange.CONTENT_LENGTH, len);
  77. }
  78. }
  79. @Override
  80. public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
  81. if (unmarshalGenericType == null) {
  82. return JSON.parseObject(stream, config.getCharset(), unmarshalType, config.getFeatures());
  83. } else {
  84. return JSON.parseObject(stream, config.getCharset(), unmarshalGenericType, config.getFeatures());
  85. }
  86. }
  87. @Override
  88. protected void doStart() throws Exception {
  89. if (config == null) {
  90. List<SerializerFeature> serializerFeatureList = new ArrayList<>();
  91. config = new FastJsonConfig();
  92. if (prettyPrint) {
  93. serializerFeatureList.add(SerializerFeature.PrettyFormat);
  94. }
  95. if (serializeNulls) {
  96. serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
  97. serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
  98. serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
  99. serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
  100. serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
  101. }
  102. if (this.dateFormatPattern != null) {
  103. serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
  104. config.setDateFormat(this.dateFormatPattern);
  105. }
  106. }
  107. }
  108. @Override
  109. protected void doStop() throws Exception {
  110. // noop
  111. }
  112. // Properties
  113. // -------------------------------------------------------------------------
  114. public Class<?> getUnmarshalType() {
  115. return this.unmarshalType;
  116. }
  117. public void setUnmarshalType(Class<?> unmarshalType) {
  118. this.unmarshalType = unmarshalType;
  119. }
  120. public Type getUnmarshalGenericType() {
  121. return this.unmarshalType;
  122. }
  123. public void setUnmarshalGenericType(Type unmarshalGenericType) {
  124. this.unmarshalGenericType = unmarshalGenericType;
  125. }
  126. public boolean isSerializeNulls() {
  127. return serializeNulls;
  128. }
  129. public void setSerializeNulls(boolean serializeNulls) {
  130. this.serializeNulls = serializeNulls;
  131. }
  132. public boolean isPrettyPrint() {
  133. return prettyPrint;
  134. }
  135. public void setPrettyPrint(boolean prettyPrint) {
  136. this.prettyPrint = prettyPrint;
  137. }
  138. public String getDateFormatPattern() {
  139. return dateFormatPattern;
  140. }
  141. public void setDateFormatPattern(String dateFormatPattern) {
  142. this.dateFormatPattern = dateFormatPattern;
  143. }
  144. public boolean isContentTypeHeader() {
  145. return contentTypeHeader;
  146. }
  147. /**
  148. * If enabled then JSON will set the Content-Type header to <tt>application/json</tt> when marshalling.
  149. */
  150. public void setContentTypeHeader(boolean contentTypeHeader) {
  151. this.contentTypeHeader = contentTypeHeader;
  152. }
  153. public FastJsonConfig getConfig() {
  154. return this.config;
  155. }
  156. }