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 */ 017 package org.apache.camel.model.dataformat; 018 019 import javax.xml.bind.annotation.XmlAccessType; 020 import javax.xml.bind.annotation.XmlAccessorType; 021 import javax.xml.bind.annotation.XmlAttribute; 022 import javax.xml.bind.annotation.XmlRootElement; 023 import javax.xml.bind.annotation.XmlTransient; 024 025 import org.apache.camel.CamelContext; 026 import org.apache.camel.model.DataFormatDefinition; 027 import org.apache.camel.spi.DataFormat; 028 import org.apache.camel.spi.RouteContext; 029 import 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) 038 public 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 050 public JsonDataFormat() { 051 } 052 053 public JsonDataFormat(JsonLibrary library) { 054 this.library = library; 055 } 056 057 public Boolean getPrettyPrint() { 058 return prettyPrint; 059 } 060 061 public void setPrettyPrint(Boolean prettyPrint) { 062 this.prettyPrint = prettyPrint; 063 } 064 065 public String getUnmarshalTypeName() { 066 return unmarshalTypeName; 067 } 068 069 public void setUnmarshalTypeName(String unmarshalTypeName) { 070 this.unmarshalTypeName = unmarshalTypeName; 071 } 072 073 public Class<?> getUnmarshalType() { 074 return unmarshalType; 075 } 076 077 public void setUnmarshalType(Class<?> unmarshalType) { 078 this.unmarshalType = unmarshalType; 079 } 080 081 public JsonLibrary getLibrary() { 082 return library; 083 } 084 085 public void setLibrary(JsonLibrary library) { 086 this.library = library; 087 } 088 089 public Class<?> getJsonView() { 090 return jsonView; 091 } 092 093 public void setJsonView(Class<?> jsonView) { 094 this.jsonView = jsonView; 095 } 096 097 @Override 098 protected DataFormat createDataFormat(RouteContext routeContext) { 099 if (library == JsonLibrary.XStream) { 100 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-xstream"); 101 } else if (library == JsonLibrary.Jackson) { 102 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-jackson"); 103 } else { 104 setProperty(routeContext.getCamelContext(), this, "dataFormatName", "json-gson"); 105 } 106 107 if (unmarshalType == null && unmarshalTypeName != null) { 108 try { 109 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 110 } catch (ClassNotFoundException e) { 111 throw ObjectHelper.wrapRuntimeCamelException(e); 112 } 113 } 114 115 return super.createDataFormat(routeContext); 116 } 117 118 @Override 119 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 120 if (unmarshalType != null) { 121 setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); 122 } 123 if (prettyPrint != null) { 124 setProperty(camelContext, dataFormat, "prettyPrint", unmarshalType); 125 } 126 127 if (jsonView != null) { 128 setProperty(camelContext, dataFormat, "jsonView", jsonView); 129 } 130 } 131 132 }