01: /*
02: * Copyright (c) 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 30. March 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.io.json;
12:
13: import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
14: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
15: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
16: import com.thoughtworks.xstream.io.StreamException;
17: import com.thoughtworks.xstream.io.xml.QNameMap;
18: import com.thoughtworks.xstream.io.xml.StaxReader;
19: import com.thoughtworks.xstream.io.xml.StaxWriter;
20:
21: import org.codehaus.jettison.mapped.MappedXMLInputFactory;
22: import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
23:
24: import javax.xml.stream.XMLStreamException;
25:
26: import java.io.InputStream;
27: import java.io.OutputStream;
28: import java.io.Reader;
29: import java.io.Writer;
30: import java.util.HashMap;
31:
32: /**
33: * Simple XStream driver wrapping Jettison's Mapped reader and writer. Serializes object from
34: * and to JSON.
35: *
36: * @author Dejan Bosanac
37: */
38: public class JettisonMappedXmlDriver implements
39: HierarchicalStreamDriver {
40:
41: private final MappedXMLOutputFactory mof;
42: private final MappedXMLInputFactory mif;
43:
44: public JettisonMappedXmlDriver() {
45: final HashMap nstjsons = new HashMap();
46: mof = new MappedXMLOutputFactory(nstjsons);
47: mif = new MappedXMLInputFactory(nstjsons);
48: }
49:
50: public HierarchicalStreamReader createReader(final Reader reader) {
51: try {
52: return new StaxReader(new QNameMap(), mif
53: .createXMLStreamReader(reader));
54: } catch (final XMLStreamException e) {
55: throw new StreamException(e);
56: }
57: }
58:
59: public HierarchicalStreamReader createReader(final InputStream input) {
60: try {
61: return new StaxReader(new QNameMap(), mif
62: .createXMLStreamReader(input));
63: } catch (final XMLStreamException e) {
64: throw new StreamException(e);
65: }
66: }
67:
68: public HierarchicalStreamWriter createWriter(final Writer writer) {
69: try {
70: return new StaxWriter(new QNameMap(), mof
71: .createXMLStreamWriter(writer));
72: } catch (final XMLStreamException e) {
73: throw new StreamException(e);
74: }
75: }
76:
77: public HierarchicalStreamWriter createWriter(
78: final OutputStream output) {
79: try {
80: return new StaxWriter(new QNameMap(), mof
81: .createXMLStreamWriter(output));
82: } catch (final XMLStreamException e) {
83: throw new StreamException(e);
84: }
85: }
86:
87: }
|