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(javaType = "java.lang.Boolean", defaultValue = "true") 041 private String useDefaultObjectMapper; 042 @XmlAttribute 043 @Metadata(javaType = "java.lang.Boolean") 044 private String prettyPrint; 045 @XmlAttribute 046 @Metadata(defaultValue = "Jackson") 047 private JsonLibrary library = JsonLibrary.Jackson; 048 @XmlAttribute 049 private String unmarshalTypeName; 050 @XmlTransient 051 private Class<?> unmarshalType; 052 @XmlAttribute 053 private Class<?> jsonView; 054 @XmlAttribute 055 private String include; 056 @XmlAttribute 057 @Metadata(javaType = "java.lang.Boolean") 058 private String allowJmsType; 059 @XmlAttribute 060 private String collectionTypeName; 061 @XmlTransient 062 private Class<?> collectionType; 063 @XmlAttribute 064 @Metadata(javaType = "java.lang.Boolean") 065 private String useList; 066 @XmlAttribute 067 @Metadata(javaType = "java.lang.Boolean") 068 private String enableJaxbAnnotationModule; 069 @XmlAttribute 070 private String moduleClassNames; 071 @XmlAttribute 072 private String moduleRefs; 073 @XmlAttribute 074 private String enableFeatures; 075 @XmlAttribute 076 private String disableFeatures; 077 @XmlAttribute 078 private String permissions; 079 @XmlAttribute 080 @Metadata(javaType = "java.lang.Boolean") 081 private String allowUnmarshallType; 082 @XmlAttribute 083 private String timezone; 084 @XmlAttribute 085 @Metadata(javaType = "java.lang.Boolean", defaultValue = "false") 086 private String autoDiscoverObjectMapper; 087 088 public JsonDataFormat() { 089 super("json"); 090 } 091 092 public JsonDataFormat(JsonLibrary library) { 093 this.library = library; 094 } 095 096 public String getObjectMapper() { 097 return objectMapper; 098 } 099 100 /** 101 * Lookup and use the existing ObjectMapper with the given id when using 102 * Jackson. 103 */ 104 public void setObjectMapper(String objectMapper) { 105 this.objectMapper = objectMapper; 106 } 107 108 public String getUseDefaultObjectMapper() { 109 return useDefaultObjectMapper; 110 } 111 112 /** 113 * Whether to lookup and use default Jackson ObjectMapper from the registry. 114 */ 115 public void setUseDefaultObjectMapper(String useDefaultObjectMapper) { 116 this.useDefaultObjectMapper = useDefaultObjectMapper; 117 } 118 119 public String getPrettyPrint() { 120 return prettyPrint; 121 } 122 123 /** 124 * To enable pretty printing output nicely formatted. 125 * <p/> 126 * Is by default false. 127 */ 128 public void setPrettyPrint(String prettyPrint) { 129 this.prettyPrint = prettyPrint; 130 } 131 132 public String getUnmarshalTypeName() { 133 return unmarshalTypeName; 134 } 135 136 /** 137 * Class name of the java type to use when unarmshalling 138 */ 139 public void setUnmarshalTypeName(String unmarshalTypeName) { 140 this.unmarshalTypeName = unmarshalTypeName; 141 } 142 143 public Class<?> getUnmarshalType() { 144 return unmarshalType; 145 } 146 147 /** 148 * Class of the java type to use when unarmshalling 149 */ 150 public void setUnmarshalType(Class<?> unmarshalType) { 151 this.unmarshalType = unmarshalType; 152 } 153 154 public JsonLibrary getLibrary() { 155 return library; 156 } 157 158 /** 159 * Which json library to use. 160 */ 161 public void setLibrary(JsonLibrary library) { 162 this.library = library; 163 } 164 165 public Class<?> getJsonView() { 166 return jsonView; 167 } 168 169 /** 170 * When marshalling a POJO to JSON you might want to exclude certain fields 171 * from the JSON output. With Jackson you can use JSON views to accomplish 172 * this. This option is to refer to the class which has @JsonView 173 * annotations 174 */ 175 public void setJsonView(Class<?> jsonView) { 176 this.jsonView = jsonView; 177 } 178 179 public String getInclude() { 180 return include; 181 } 182 183 /** 184 * If you want to marshal a pojo to JSON, and the pojo has some fields with 185 * null values. And you want to skip these null values, you can set this 186 * option to <tt>NON_NULL</tt> 187 */ 188 public void setInclude(String include) { 189 this.include = include; 190 } 191 192 public String getAllowJmsType() { 193 return allowJmsType; 194 } 195 196 /** 197 * Used for JMS users to allow the JMSType header from the JMS spec to 198 * specify a FQN classname to use to unmarshal to. 199 */ 200 public void setAllowJmsType(String allowJmsType) { 201 this.allowJmsType = allowJmsType; 202 } 203 204 public String getCollectionTypeName() { 205 return collectionTypeName; 206 } 207 208 /** 209 * Refers to a custom collection type to lookup in the registry to use. This 210 * option should rarely be used, but allows to use different collection 211 * types than java.util.Collection based as default. 212 */ 213 public void setCollectionTypeName(String collectionTypeName) { 214 this.collectionTypeName = collectionTypeName; 215 } 216 217 public Class<?> getCollectionType() { 218 return collectionType; 219 } 220 221 public void setCollectionType(Class<?> collectionType) { 222 this.collectionType = collectionType; 223 } 224 225 public String getUseList() { 226 return useList; 227 } 228 229 /** 230 * To unarmshal to a List of Map or a List of Pojo. 231 */ 232 public void setUseList(String useList) { 233 this.useList = useList; 234 } 235 236 public String getEnableJaxbAnnotationModule() { 237 return enableJaxbAnnotationModule; 238 } 239 240 /** 241 * Whether to enable the JAXB annotations module when using jackson. When 242 * enabled then JAXB annotations can be used by Jackson. 243 */ 244 public void setEnableJaxbAnnotationModule(String enableJaxbAnnotationModule) { 245 this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; 246 } 247 248 public String getModuleClassNames() { 249 return moduleClassNames; 250 } 251 252 /** 253 * To use custom Jackson modules com.fasterxml.jackson.databind.Module 254 * specified as a String with FQN class names. Multiple classes can be 255 * separated by comma. 256 */ 257 public void setModuleClassNames(String moduleClassNames) { 258 this.moduleClassNames = moduleClassNames; 259 } 260 261 public String getModuleRefs() { 262 return moduleRefs; 263 } 264 265 /** 266 * To use custom Jackson modules referred from the Camel registry. Multiple 267 * modules can be separated by comma. 268 */ 269 public void setModuleRefs(String moduleRefs) { 270 this.moduleRefs = moduleRefs; 271 } 272 273 public String getEnableFeatures() { 274 return enableFeatures; 275 } 276 277 /** 278 * Set of features to enable on the Jackson 279 * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. 280 * <p/> 281 * The features should be a name that matches a enum from 282 * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, 283 * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or 284 * <tt>com.fasterxml.jackson.databind.MapperFeature</tt> 285 * <p/> 286 * Multiple features can be separated by comma 287 */ 288 public void setEnableFeatures(String enableFeatures) { 289 this.enableFeatures = enableFeatures; 290 } 291 292 public String getDisableFeatures() { 293 return disableFeatures; 294 } 295 296 /** 297 * Set of features to disable on the Jackson 298 * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. 299 * <p/> 300 * The features should be a name that matches a enum from 301 * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, 302 * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or 303 * <tt>com.fasterxml.jackson.databind.MapperFeature</tt> 304 * <p/> 305 * Multiple features can be separated by comma 306 */ 307 public void setDisableFeatures(String disableFeatures) { 308 this.disableFeatures = disableFeatures; 309 } 310 311 public String getPermissions() { 312 return permissions; 313 } 314 315 /** 316 * Adds permissions that controls which Java packages and classes XStream is 317 * allowed to use during unmarshal from xml/json to Java beans. 318 * <p/> 319 * A permission must be configured either here or globally using a JVM 320 * system property. The permission can be specified in a syntax where a plus 321 * sign is allow, and minus sign is deny. <br/> 322 * Wildcards is supported by using <tt>.*</tt> as prefix. For example to 323 * allow <tt>com.foo</tt> and all subpackages then specfy 324 * <tt>+com.foo.*</tt>. Multiple permissions can be configured separated by 325 * comma, such as <tt>+com.foo.*,-com.foo.bar.MySecretBean</tt>. <br/> 326 * The following default permission is always included: 327 * <tt>"-*,java.lang.*,java.util.*"</tt> unless its overridden by specifying 328 * a JVM system property with they key 329 * <tt>org.apache.camel.xstream.permissions</tt>. 330 */ 331 public void setPermissions(String permissions) { 332 this.permissions = permissions; 333 } 334 335 /** 336 * To add permission for the given pojo classes. 337 * 338 * @param type the pojo class(es) xstream should use as allowed permission 339 * @see #setPermissions(String) 340 */ 341 public void setPermissions(Class<?>... type) { 342 CollectionStringBuffer csb = new CollectionStringBuffer(","); 343 for (Class<?> clazz : type) { 344 csb.append("+"); 345 csb.append(clazz.getName()); 346 } 347 setPermissions(csb.toString()); 348 } 349 350 public String getAllowUnmarshallType() { 351 return allowUnmarshallType; 352 } 353 354 /** 355 * If enabled then Jackson is allowed to attempt to use the 356 * CamelJacksonUnmarshalType header during the unmarshalling. 357 * <p/> 358 * This should only be enabled when desired to be used. 359 */ 360 public void setAllowUnmarshallType(String allowUnmarshallType) { 361 this.allowUnmarshallType = allowUnmarshallType; 362 } 363 364 public String getTimezone() { 365 return timezone; 366 } 367 368 /** 369 * If set then Jackson will use the Timezone when marshalling/unmarshalling. 370 * This option will have no effect on the others Json DataFormat, like gson, 371 * fastjson and xstream. 372 */ 373 public void setTimezone(String timezone) { 374 this.timezone = timezone; 375 } 376 377 public String getAutoDiscoverObjectMapper() { 378 return autoDiscoverObjectMapper; 379 } 380 381 /** 382 * If set to true then Jackson will lookup for an objectMapper into the 383 * registry 384 */ 385 public void setAutoDiscoverObjectMapper(String autoDiscoverObjectMapper) { 386 this.autoDiscoverObjectMapper = autoDiscoverObjectMapper; 387 } 388 389 @Override 390 public String getDataFormatName() { 391 // json data format is special as the name can be from different bundles 392 return "json-" + library.name().toLowerCase(); 393 } 394 395 // 396 // Fluent builders 397 // 398 399 public JsonDataFormat objectMapper(String objectMapper) { 400 this.objectMapper = objectMapper; 401 return this; 402 } 403 404 public JsonDataFormat useDefaultObjectMapper(boolean useDefaultObjectMapper) { 405 return useDefaultObjectMapper(Boolean.toString(useDefaultObjectMapper)); 406 } 407 408 public JsonDataFormat useDefaultObjectMapper(String useDefaultObjectMapper) { 409 this.useDefaultObjectMapper = useDefaultObjectMapper; 410 return this; 411 } 412 413 public JsonDataFormat prettyPrint(boolean prettyPrint) { 414 return prettyPrint(Boolean.toString(prettyPrint)); 415 } 416 417 public JsonDataFormat prettyPrint(String prettyPrint) { 418 this.prettyPrint = prettyPrint; 419 return this; 420 } 421 422 public JsonDataFormat library(JsonLibrary library) { 423 this.library = library; 424 return this; 425 } 426 427 public JsonDataFormat unmarshalType(String unmarshalType) { 428 this.unmarshalTypeName = unmarshalType; 429 return this; 430 } 431 432 public JsonDataFormat unmarshalType(Class<?> unmarshalType) { 433 this.unmarshalType = unmarshalType; 434 return this; 435 } 436 437 public JsonDataFormat jsonView(Class<?> jsonView) { 438 this.jsonView = jsonView; 439 return this; 440 } 441 442 public JsonDataFormat include(String include) { 443 this.include = include; 444 return this; 445 } 446 447 public JsonDataFormat allowJmsType(boolean allowJmsType) { 448 return allowJmsType(Boolean.toString(allowJmsType)); 449 } 450 451 public JsonDataFormat allowJmsType(String allowJmsType) { 452 this.allowJmsType = allowJmsType; 453 return this; 454 } 455 456 public JsonDataFormat collectionType(String collectionType) { 457 this.collectionTypeName = collectionType; 458 return this; 459 } 460 461 public JsonDataFormat collectionType(Class<?> collectionType) { 462 this.collectionType = collectionType; 463 return this; 464 } 465 466 public JsonDataFormat useList(boolean useList) { 467 return useList(Boolean.toString(useList)); 468 } 469 470 public JsonDataFormat useList(String useList) { 471 this.useList = useList; 472 return this; 473 } 474 475 public JsonDataFormat enableJaxbAnnotationModule(boolean enableJaxbAnnotationModule) { 476 return enableJaxbAnnotationModule(Boolean.toString(enableJaxbAnnotationModule)); 477 } 478 479 public JsonDataFormat enableJaxbAnnotationModule(String enableJaxbAnnotationModule) { 480 this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; 481 return this; 482 } 483 484 public JsonDataFormat moduleClassNames(String moduleClassNames) { 485 this.moduleClassNames = moduleClassNames; 486 return this; 487 } 488 489 public JsonDataFormat moduleRefs(String moduleRefs) { 490 this.moduleRefs = moduleRefs; 491 return this; 492 } 493 494 public JsonDataFormat enableFeatures(String enableFeatures) { 495 this.enableFeatures = enableFeatures; 496 return this; 497 } 498 499 public JsonDataFormat disableFeatures(String disableFeatures) { 500 this.disableFeatures = disableFeatures; 501 return this; 502 } 503 504 public JsonDataFormat permissions(String permissions) { 505 this.permissions = permissions; 506 return this; 507 } 508 509 public JsonDataFormat allowUnmarshallType(boolean allowUnmarshallType) { 510 return allowUnmarshallType(Boolean.toString(allowUnmarshallType)); 511 } 512 513 public JsonDataFormat allowUnmarshallType(String allowUnmarshallType) { 514 this.allowUnmarshallType = allowUnmarshallType; 515 return this; 516 } 517 518 public JsonDataFormat timezone(String timezone) { 519 this.timezone = timezone; 520 return this; 521 } 522 523 public JsonDataFormat autoDiscoverObjectMapper(boolean autoDiscoverObjectMapper) { 524 return autoDiscoverObjectMapper(Boolean.toString(autoDiscoverObjectMapper)); 525 } 526 527 public JsonDataFormat autoDiscoverObjectMapper(String autoDiscoverObjectMapper) { 528 this.autoDiscoverObjectMapper = autoDiscoverObjectMapper; 529 return this; 530 } 531 532}