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.model.DataFormatDefinition; 026 import org.apache.camel.spi.DataFormat; 027 import org.apache.camel.spi.RouteContext; 028 import org.apache.camel.util.ObjectHelper; 029 030 /** 031 * Represents the Json {@link DataFormat} 032 * 033 * @version 034 */ 035 @XmlRootElement(name = "json") 036 @XmlAccessorType(XmlAccessType.FIELD) 037 public class JsonDataFormat extends DataFormatDefinition { 038 @XmlAttribute 039 private Boolean prettyPrint; 040 @XmlAttribute 041 private JsonLibrary library = JsonLibrary.XStream; 042 @XmlAttribute 043 private String unmarshalTypeName; 044 @XmlTransient 045 private Class<?> unmarshalType; 046 047 public JsonDataFormat() { 048 } 049 050 public JsonDataFormat(JsonLibrary library) { 051 this.library = library; 052 } 053 054 public Boolean getPrettyPrint() { 055 return prettyPrint; 056 } 057 058 public void setPrettyPrint(Boolean prettyPrint) { 059 this.prettyPrint = prettyPrint; 060 } 061 062 public String getUnmarshalTypeName() { 063 return unmarshalTypeName; 064 } 065 066 public void setUnmarshalTypeName(String unmarshalTypeName) { 067 this.unmarshalTypeName = unmarshalTypeName; 068 } 069 070 public Class<?> getUnmarshalType() { 071 return unmarshalType; 072 } 073 074 public void setUnmarshalType(Class<?> unmarshalType) { 075 this.unmarshalType = unmarshalType; 076 } 077 078 public JsonLibrary getLibrary() { 079 return library; 080 } 081 082 public void setLibrary(JsonLibrary library) { 083 this.library = library; 084 } 085 086 @Override 087 protected DataFormat createDataFormat(RouteContext routeContext) { 088 if (library == JsonLibrary.XStream) { 089 setProperty(this, "dataFormatName", "json-xstream"); 090 } else if (library == JsonLibrary.Jackson) { 091 setProperty(this, "dataFormatName", "json-jackson"); 092 } else { 093 setProperty(this, "dataFormatName", "json-gson"); 094 } 095 096 if (unmarshalType == null && unmarshalTypeName != null) { 097 try { 098 unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); 099 } catch (ClassNotFoundException e) { 100 throw ObjectHelper.wrapRuntimeCamelException(e); 101 } 102 } 103 104 return super.createDataFormat(routeContext); 105 } 106 107 @Override 108 protected void configureDataFormat(DataFormat dataFormat) { 109 if (unmarshalType != null) { 110 setProperty(dataFormat, "unmarshalType", unmarshalType); 111 } 112 if (prettyPrint != null) { 113 setProperty(dataFormat, "prettyPrint", unmarshalType); 114 } 115 } 116 117 }