001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.dataformat;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.model.DataFormatDefinition;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.util.CollectionStringBuffer;
028
029/**
030 * JSon data format is used for unmarshal a JSon payload to POJO or to marshal
031 * POJO back to JSon payload.
032 */
033@Metadata(label = "dataformat,transformation,json", title = "JSon")
034@XmlRootElement(name = "json")
035@XmlAccessorType(XmlAccessType.FIELD)
036public class JsonDataFormat extends DataFormatDefinition {
037    @XmlAttribute
038    private String objectMapper;
039    @XmlAttribute
040    @Metadata(defaultValue = "true")
041    private Boolean useDefaultObjectMapper;
042    @XmlAttribute
043    private Boolean prettyPrint;
044    @XmlAttribute
045    @Metadata(defaultValue = "Jackson")
046    private JsonLibrary library = JsonLibrary.Jackson;
047    @XmlAttribute
048    private String unmarshalTypeName;
049    @XmlTransient
050    private Class<?> unmarshalType;
051    @XmlAttribute
052    private Class<?> jsonView;
053    @XmlAttribute
054    private String include;
055    @XmlAttribute
056    private Boolean allowJmsType;
057    @XmlAttribute
058    private String collectionTypeName;
059    @XmlTransient
060    private Class<?> collectionType;
061    @XmlAttribute
062    private Boolean useList;
063    @XmlAttribute
064    private Boolean enableJaxbAnnotationModule;
065    @XmlAttribute
066    private String moduleClassNames;
067    @XmlAttribute
068    private String moduleRefs;
069    @XmlAttribute
070    private String enableFeatures;
071    @XmlAttribute
072    private String disableFeatures;
073    @XmlAttribute
074    private String permissions;
075    @XmlAttribute
076    private Boolean allowUnmarshallType;
077    @XmlAttribute
078    private String timezone;
079
080    public JsonDataFormat() {
081        super("json");
082    }
083
084    public JsonDataFormat(JsonLibrary library) {
085        this.library = library;
086    }
087
088    public String getObjectMapper() {
089        return objectMapper;
090    }
091
092    /**
093     * Lookup and use the existing ObjectMapper with the given id when using
094     * Jackson.
095     */
096    public void setObjectMapper(String objectMapper) {
097        this.objectMapper = objectMapper;
098    }
099
100    public Boolean getUseDefaultObjectMapper() {
101        return useDefaultObjectMapper;
102    }
103
104    /**
105     * Whether to lookup and use default Jackson ObjectMapper from the registry.
106     */
107    public void setUseDefaultObjectMapper(Boolean useDefaultObjectMapper) {
108        this.useDefaultObjectMapper = useDefaultObjectMapper;
109    }
110
111    public Boolean getPrettyPrint() {
112        return prettyPrint;
113    }
114
115    /**
116     * To enable pretty printing output nicely formatted.
117     * <p/>
118     * Is by default false.
119     */
120    public void setPrettyPrint(Boolean prettyPrint) {
121        this.prettyPrint = prettyPrint;
122    }
123
124    public String getUnmarshalTypeName() {
125        return unmarshalTypeName;
126    }
127
128    /**
129     * Class name of the java type to use when unarmshalling
130     */
131    public void setUnmarshalTypeName(String unmarshalTypeName) {
132        this.unmarshalTypeName = unmarshalTypeName;
133    }
134
135    public Class<?> getUnmarshalType() {
136        return unmarshalType;
137    }
138
139    /**
140     * Class of the java type to use when unarmshalling
141     */
142    public void setUnmarshalType(Class<?> unmarshalType) {
143        this.unmarshalType = unmarshalType;
144    }
145
146    public JsonLibrary getLibrary() {
147        return library;
148    }
149
150    /**
151     * Which json library to use.
152     */
153    public void setLibrary(JsonLibrary library) {
154        this.library = library;
155    }
156
157    public Class<?> getJsonView() {
158        return jsonView;
159    }
160
161    /**
162     * When marshalling a POJO to JSON you might want to exclude certain fields
163     * from the JSON output. With Jackson you can use JSON views to accomplish
164     * this. This option is to refer to the class which has @JsonView
165     * annotations
166     */
167    public void setJsonView(Class<?> jsonView) {
168        this.jsonView = jsonView;
169    }
170
171    public String getInclude() {
172        return include;
173    }
174
175    /**
176     * If you want to marshal a pojo to JSON, and the pojo has some fields with
177     * null values. And you want to skip these null values, you can set this
178     * option to <tt>NON_NULL</tt>
179     */
180    public void setInclude(String include) {
181        this.include = include;
182    }
183
184    public Boolean getAllowJmsType() {
185        return allowJmsType;
186    }
187
188    /**
189     * Used for JMS users to allow the JMSType header from the JMS spec to
190     * specify a FQN classname to use to unmarshal to.
191     */
192    public void setAllowJmsType(Boolean allowJmsType) {
193        this.allowJmsType = allowJmsType;
194    }
195
196    public String getCollectionTypeName() {
197        return collectionTypeName;
198    }
199
200    /**
201     * Refers to a custom collection type to lookup in the registry to use. This
202     * option should rarely be used, but allows to use different collection
203     * types than java.util.Collection based as default.
204     */
205    public void setCollectionTypeName(String collectionTypeName) {
206        this.collectionTypeName = collectionTypeName;
207    }
208
209    public Class<?> getCollectionType() {
210        return collectionType;
211    }
212
213    public void setCollectionType(Class<?> collectionType) {
214        this.collectionType = collectionType;
215    }
216
217    public Boolean getUseList() {
218        return useList;
219    }
220
221    /**
222     * To unarmshal to a List of Map or a List of Pojo.
223     */
224    public void setUseList(Boolean useList) {
225        this.useList = useList;
226    }
227
228    public Boolean getEnableJaxbAnnotationModule() {
229        return enableJaxbAnnotationModule;
230    }
231
232    /**
233     * Whether to enable the JAXB annotations module when using jackson. When
234     * enabled then JAXB annotations can be used by Jackson.
235     */
236    public void setEnableJaxbAnnotationModule(Boolean enableJaxbAnnotationModule) {
237        this.enableJaxbAnnotationModule = enableJaxbAnnotationModule;
238    }
239
240    public String getModuleClassNames() {
241        return moduleClassNames;
242    }
243
244    /**
245     * To use custom Jackson modules com.fasterxml.jackson.databind.Module
246     * specified as a String with FQN class names. Multiple classes can be
247     * separated by comma.
248     */
249    public void setModuleClassNames(String moduleClassNames) {
250        this.moduleClassNames = moduleClassNames;
251    }
252
253    public String getModuleRefs() {
254        return moduleRefs;
255    }
256
257    /**
258     * To use custom Jackson modules referred from the Camel registry. Multiple
259     * modules can be separated by comma.
260     */
261    public void setModuleRefs(String moduleRefs) {
262        this.moduleRefs = moduleRefs;
263    }
264
265    public String getEnableFeatures() {
266        return enableFeatures;
267    }
268
269    /**
270     * Set of features to enable on the Jackson
271     * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>.
272     * <p/>
273     * The features should be a name that matches a enum from
274     * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
275     * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or
276     * <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
277     * <p/>
278     * Multiple features can be separated by comma
279     */
280    public void setEnableFeatures(String enableFeatures) {
281        this.enableFeatures = enableFeatures;
282    }
283
284    public String getDisableFeatures() {
285        return disableFeatures;
286    }
287
288    /**
289     * Set of features to disable on the Jackson
290     * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>.
291     * <p/>
292     * The features should be a name that matches a enum from
293     * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>,
294     * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or
295     * <tt>com.fasterxml.jackson.databind.MapperFeature</tt>
296     * <p/>
297     * Multiple features can be separated by comma
298     */
299    public void setDisableFeatures(String disableFeatures) {
300        this.disableFeatures = disableFeatures;
301    }
302
303    public String getPermissions() {
304        return permissions;
305    }
306
307    /**
308     * Adds permissions that controls which Java packages and classes XStream is
309     * allowed to use during unmarshal from xml/json to Java beans.
310     * <p/>
311     * A permission must be configured either here or globally using a JVM
312     * system property. The permission can be specified in a syntax where a plus
313     * sign is allow, and minus sign is deny. <br/>
314     * Wildcards is supported by using <tt>.*</tt> as prefix. For example to
315     * allow <tt>com.foo</tt> and all subpackages then specfy
316     * <tt>+com.foo.*</tt>. Multiple permissions can be configured separated by
317     * comma, such as <tt>+com.foo.*,-com.foo.bar.MySecretBean</tt>. <br/>
318     * The following default permission is always included:
319     * <tt>"-*,java.lang.*,java.util.*"</tt> unless its overridden by specifying
320     * a JVM system property with they key
321     * <tt>org.apache.camel.xstream.permissions</tt>.
322     */
323    public void setPermissions(String permissions) {
324        this.permissions = permissions;
325    }
326
327    /**
328     * To add permission for the given pojo classes.
329     * 
330     * @param type the pojo class(es) xstream should use as allowed permission
331     * @see #setPermissions(String)
332     */
333    public void setPermissions(Class<?>... type) {
334        CollectionStringBuffer csb = new CollectionStringBuffer(",");
335        for (Class<?> clazz : type) {
336            csb.append("+");
337            csb.append(clazz.getName());
338        }
339        setPermissions(csb.toString());
340    }
341
342    public Boolean getAllowUnmarshallType() {
343        return allowUnmarshallType;
344    }
345
346    /**
347     * If enabled then Jackson is allowed to attempt to use the
348     * CamelJacksonUnmarshalType header during the unmarshalling.
349     * <p/>
350     * This should only be enabled when desired to be used.
351     */
352    public void setAllowUnmarshallType(Boolean allowUnmarshallType) {
353        this.allowUnmarshallType = allowUnmarshallType;
354    }
355
356    public String getTimezone() {
357        return timezone;
358    }
359
360    /**
361     * If set then Jackson will use the Timezone when marshalling/unmarshalling.
362     * This option will have no effect on the others Json DataFormat, like gson,
363     * fastjson and xstream.
364     */
365    public void setTimezone(String timezone) {
366        this.timezone = timezone;
367    }
368
369    @Override
370    public String getDataFormatName() {
371        // json data format is special as the name can be from different bundles
372        return "json-" + library.name().toLowerCase();
373    }
374
375}