Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Avoid call of deprecated Jettison method. Avoid side-effects for othe…
Browse files Browse the repository at this point in the history
…r elements with the name of collection elements.
  • Loading branch information
joehni committed Dec 27, 2018
1 parent e14d233 commit a925ad0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014 XStream Committers.
* Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2018 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -51,7 +51,7 @@ public class JettisonMappedXmlDriver extends AbstractDriver {
* Construct a JettisonMappedXmlDriver.
*/
public JettisonMappedXmlDriver() {
this(new Configuration());
this(null);
}

/**
Expand All @@ -75,7 +75,8 @@ public JettisonMappedXmlDriver(final Configuration config) {
* @param useSerializeAsArray flag to use XStream's hints for collections and arrays
* @since 1.4
*/
public JettisonMappedXmlDriver(final Configuration config, final boolean useSerializeAsArray) {
public JettisonMappedXmlDriver(Configuration config, final boolean useSerializeAsArray) {
config = config == null ? new Configuration() : config;
mof = new MappedXMLOutputFactory(config);
mif = new MappedXMLInputFactory(config);
convention = new MappedNamespaceConvention(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2014 XStream Committers.
* Copyright (c) 2008, 2009, 2010, 2011, 2014, 2018 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*
* Created on 17.04.2008 by Joerg Schaible.
*/
package com.thoughtworks.xstream.io.json;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.Map;

import javax.xml.namespace.QName;
Expand All @@ -28,13 +31,14 @@

/**
* A specialized {@link StaxWriter} that makes usage of internal functionality of Jettison.
*
*
* @author Jörg Schaible
* @since 1.3.1
*/
public class JettisonStaxWriter extends StaxWriter {

private final MappedNamespaceConvention convention;
private final Deque<String> stack = new ArrayDeque<String>();

/**
* @since 1.4
Expand All @@ -56,13 +60,15 @@ public JettisonStaxWriter(
public JettisonStaxWriter(
final QNameMap qnameMap, final XMLStreamWriter out, final boolean writeEnclosingDocument,
final boolean namespaceRepairingMode, final XmlFriendlyReplacer replacer,
final MappedNamespaceConvention convention) throws XMLStreamException {
final MappedNamespaceConvention convention)
throws XMLStreamException {
this(qnameMap, out, writeEnclosingDocument, namespaceRepairingMode, (NameCoder)replacer, convention);
}

public JettisonStaxWriter(
final QNameMap qnameMap, final XMLStreamWriter out, final boolean writeEnclosingDocument,
final boolean namespaceRepairingMode, final MappedNamespaceConvention convention) throws XMLStreamException {
final boolean namespaceRepairingMode, final MappedNamespaceConvention convention)
throws XMLStreamException {
super(qnameMap, out, writeEnclosingDocument, namespaceRepairingMode);
this.convention = convention;
}
Expand All @@ -79,26 +85,45 @@ public JettisonStaxWriter(
*/
public JettisonStaxWriter(
final QNameMap qnameMap, final XMLStreamWriter out, final NameCoder nameCoder,
final MappedNamespaceConvention convention) throws XMLStreamException {
final MappedNamespaceConvention convention)
throws XMLStreamException {
super(qnameMap, out, nameCoder);
this.convention = convention;
}

@Override
public void startNode(final String name, final Class<?> clazz) {
final XMLStreamWriter out = getXMLStreamWriter();
String key = "";
if (clazz != null && out instanceof AbstractXMLStreamWriter) {
if (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz) || clazz.isArray()) {
final QName qname = getQNameMap().getQName(encodeNode(name));
final String prefix = qname.getPrefix();
final String uri = qname.getNamespaceURI();
final String key = convention.createKey(prefix, uri, qname.getLocalPart());
if (!((AbstractXMLStreamWriter)out).getSerializedAsArrays().contains(key)) {
// Typo is in the API of Jettison ...
((AbstractXMLStreamWriter)out).seriliazeAsArray(key);
}
key = convention.createKey(prefix, uri, qname.getLocalPart());
}
}
startNode(name);
stack.push(key);
super.startNode(name);
}

@Override
public void startNode(final String name) {
startNode(name, null);
}

@Override
public void endNode() {
final String key = stack.pop();
if (key.length() == 0)
super.endNode();
else {
final XMLStreamWriter out = getXMLStreamWriter();
@SuppressWarnings("unchecked")
final ArrayList<String> serializedAsArrays = ((AbstractXMLStreamWriter)out).getSerializedAsArrays();
serializedAsArrays.add(key);
super.endNode();
serializedAsArrays.remove(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void testDoesEscapeValuesAccordingRfc4627() {
assertEquals(expected, xstream.toXML("\u0000\u0001\u001f\u0020\uffee"));
}

public void testSingletonListWithSimpleObject() {
public void testListWithOneSimpleObject() {
final ArrayList<String> list1 = new ArrayList<String>();
list1.add("one");
final String json = xstream.toXML(list1);
Expand All @@ -177,6 +177,17 @@ public void testListWithSimpleObjects() {
assertEquals(json, xstream.toXML(list2));
}

public void testListWithDifferentSimpleObjects() {
final ArrayList<Object> list1 = new ArrayList<Object>();
list1.add("one");
list1.add(Integer.valueOf(2));
list1.add(Float.valueOf(3.3f));
final String json = xstream.toXML(list1);
assertEquals("{'list':[{'string':'one','int':2,'float':3.3}]}".replace('\'', '"'), json);
final ArrayList<Object> list2 = xstream.<ArrayList<Object>>fromXML(json);
assertEquals(json, xstream.toXML(list2));
}

public void testSingletonListWithComplexObject() {
final Product product = new Product("Banana", "123", 23.00);
final ArrayList<Product> list1 = new ArrayList<Product>();
Expand Down Expand Up @@ -255,13 +266,10 @@ public void testEmbeddedXml() {

public void testArrayList() {
final ArrayList<Object> list1 = new ArrayList<Object>();
list1.clear();
list1.add(new Integer(12));

list1.add("string");
list1.add(new Integer(13));
final String json = xstream.toXML(list1);

final ArrayList<?> list2 = xstream.<ArrayList<?>>fromXML(json);
assertEquals(json, xstream.toXML(list2));
}
Expand Down

0 comments on commit a925ad0

Please sign in to comment.