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.dataformat.xstream;
018
019 import java.io.InputStream;
020 import java.io.OutputStream;
021 import java.util.HashMap;
022 import java.util.Map;
023
024 import javax.xml.stream.XMLStreamException;
025
026 import com.thoughtworks.xstream.XStream;
027 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
028 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
029 import com.thoughtworks.xstream.io.xml.QNameMap;
030 import com.thoughtworks.xstream.io.xml.StaxReader;
031 import com.thoughtworks.xstream.io.xml.StaxWriter;
032
033 import org.apache.camel.Exchange;
034 import org.apache.camel.spi.ClassResolver;
035 import org.codehaus.jettison.mapped.MappedXMLInputFactory;
036 import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
037
038 /**
039 * A <a href="http://camel.apache.org/data-format.html">data format</a>
040 * ({@link DataFormat}) using XStream and Jettison to marshal to and from JSON
041 *
042 * @version
043 */
044
045 public class JsonDataFormat extends AbstractXStreamWrapper {
046 private final MappedXMLOutputFactory mof;
047 private final MappedXMLInputFactory mif;
048
049 public JsonDataFormat() {
050 final Map nstjsons = new HashMap();
051 mof = new MappedXMLOutputFactory(nstjsons);
052 mif = new MappedXMLInputFactory(nstjsons);
053 }
054
055 @Override
056 protected XStream createXStream(ClassResolver resolver) {
057 XStream xs = super.createXStream(resolver);
058 xs.setMode(XStream.NO_REFERENCES);
059 return xs;
060 }
061
062 protected HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException {
063 return new StaxWriter(new QNameMap(), mof.createXMLStreamWriter(stream));
064 }
065
066 protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
067 return new StaxReader(new QNameMap(), mif.createXMLStreamReader(stream));
068 }
069 }