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.CamelContext; 026import org.apache.camel.model.DataFormatDefinition; 027import org.apache.camel.spi.DataFormat; 028import org.apache.camel.spi.RouteContext; 029import org.apache.camel.util.ObjectHelper; 030 031/** 032 * Represents the Json {@link DataFormat} 033 * 034 * @version 035 */ 036@XmlRootElement(name = "json") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class JsonDataFormat extends DataFormatDefinition { 039 @XmlAttribute 040 private Boolean prettyPrint; 041 @XmlAttribute 042 private JsonLibrary library = JsonLibrary.XStream; 043 @XmlAttribute 044 private String unmarshalTypeName; 045 @XmlTransient 046 private Class<?> unmarshalType; 047 @XmlAttribute 048 private Class<?> jsonView; 049 @XmlAttribute 050 private String include; 051 @XmlAttribute 052 private Boolean allowJmsType; 053 @XmlAttribute 054 private String collectionTypeName; 055 @XmlTransient 056 private Class<?> collectionType; 057 @XmlAttribute 058 private Boolean useList; 059 060 public JsonDataFormat() { 061 } 062 063 public JsonDataFormat(JsonLibrary library) { 064 this.library = library; 065 } 066 067 public Boolean getPrettyPrint() { 068 return prettyPrint; 069 } 070 071 public void setPrettyPrint(Boolean prettyPrint) { 072 this.prettyPrint = prettyPrint; 073 } 074 075 public String getUnmarshalTypeName() { 076 return unmarshalTypeName; 077 } 078 079 public void setUnmarshalTypeName(String unmarshalTypeName) { 080 this.unmarshalTypeName = unmarshalTypeName; 081 } 082 083 public Class<?> getUnmarshalType() { 084 return unmarshalType; 085 } 086 087 public void setUnmarshalType(Class<?> unmarshalType) { 088 this.unmarshalType = unmarshalType; 089 } 090 091 public JsonLibrary getLibrary() { 092 return library; 093 } 094 095 public void setLibrary(JsonLibrary library) { 096 this.library = library; 097 } 098 099 public Class<?> getJsonView() { 100 return jsonView; 101 } 102 103 public void setJsonView(Class<?> jsonView) { 104 this.jsonView = jsonView; 105 } 106 107 public String getInclude() { 108 return include; 109 } 110 111 public void setInclude(String include) { 112 this.include = include; 113 } 114 115 public Boolean getAllowJmsType() { 116 return allowJmsType; 117 } 118 119 public void setAllowJmsType(Boolean allowJmsType) { 120 this.allowJmsType = allowJmsType; 121 } 122 123 public String getCollectionTypeName() { 124 return collectionTypeName; 125 } 126 127 public void setCollectionTypeName(String collectionTypeName) { 128 this.collectionTypeName = collectionTypeName; 129 } 130 131 public Boolean getUseList() { 132 return useList; 133 } 134 135 public void setUseList(Boolean useList) { 136 this.useList = useList; 137 } 138 139 @Override 140 protected DataFormat createDataFormat(RouteContext routeContext) { 141 if (library == JsonLibrary.XStream) { 142 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-xstream"); 143 } else if (library == JsonLibrary.Jackson) { 144 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-jackson"); 145 } else { 146 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-gson"); 147 } 148 149 if (unmarshalType == null && unmarshalTypeName != null) { 150 try { 151 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 152 } catch (ClassNotFoundException e) { 153 throw ObjectHelper.wrapRuntimeCamelException(e); 154 } 155 } 156 if (collectionType == null && collectionTypeName != null) { 157 try { 158 collectionType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(collectionTypeName); 159 } catch (ClassNotFoundException e) { 160 throw ObjectHelper.wrapRuntimeCamelException(e); 161 } 162 } 163 164 return super.createDataFormat(routeContext); 165 } 166 167 @Override 168 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 169 if (unmarshalType != null) { 170 setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); 171 } 172 if (prettyPrint != null) { 173 setProperty(camelContext, dataFormat, "prettyPrint", prettyPrint); 174 } 175 if (jsonView != null) { 176 setProperty(camelContext, dataFormat, "jsonView", jsonView); 177 } 178 if (include != null) { 179 setProperty(camelContext, dataFormat, "include", include); 180 } 181 if (allowJmsType != null) { 182 setProperty(camelContext, dataFormat, "allowJmsType", allowJmsType); 183 } 184 if (collectionType != null) { 185 setProperty(camelContext, dataFormat, "collectionType", collectionType); 186 } 187 if (useList != null) { 188 setProperty(camelContext, dataFormat, "useList", useList); 189 } 190 } 191 192}