Skip to content

Commit

Permalink
Implemented #156
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 2, 2015
1 parent 1a97b9d commit f4c7bb0
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 53 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project: jackson-dataformat-xml

2.7.0 (not yet released)

#156: Add `XmlMapper.setDefaultUseWrapper()` for convenience.

2.6.2 (15-Sep-2015)
2.6.1 (09-Aug-2015)
2.6.0 (20-Jul-2015)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;

import com.fasterxml.jackson.dataformat.xml.annotation.*;

/**
Expand All @@ -25,7 +24,8 @@ public class JacksonXmlAnnotationIntrospector
*/
public final static boolean DEFAULT_USE_WRAPPER = true;

protected final boolean _cfgDefaultUseWrapper;
// non-final from 2.7 on, to allow mapper to change
protected boolean _cfgDefaultUseWrapper;

public JacksonXmlAnnotationIntrospector() {
this(DEFAULT_USE_WRAPPER);
Expand Down Expand Up @@ -132,7 +132,12 @@ public Boolean isOutputAsCData(Annotated ann) {
}
return null;
}


@Override
public void setDefaultUseWrapper(boolean b) {
_cfgDefaultUseWrapper = b;
}

/*
/**********************************************************************
/* Overrides for name, property detection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public interface XmlAnnotationIntrospector
*/
public Boolean isOutputAsCData(Annotated ann);

/**
* @since 2.7
*/
public void setDefaultUseWrapper(boolean b);

/*
/**********************************************************************
/* Replacement of 'AnnotationIntrospector.Pair' to use when combining
Expand Down Expand Up @@ -89,7 +94,7 @@ public static XmlAnnotationIntrospector.Pair instance(AnnotationIntrospector a1,
public String findNamespace(Annotated ann)
{
String value = (_xmlPrimary == null) ? null : _xmlPrimary.findNamespace(ann);
if (value == null && _xmlSecondary != null) {
if ((value == null) && (_xmlSecondary != null)) {
value = _xmlSecondary.findNamespace(ann);
}
return value;
Expand All @@ -99,7 +104,7 @@ public String findNamespace(Annotated ann)
public Boolean isOutputAsAttribute(Annotated ann)
{
Boolean value = (_xmlPrimary == null) ? null : _xmlPrimary.isOutputAsAttribute(ann);
if (value == null && _xmlSecondary != null) {
if ((value == null) && (_xmlSecondary != null)) {
value = _xmlSecondary.isOutputAsAttribute(ann);
}
return value;
Expand All @@ -109,7 +114,7 @@ public Boolean isOutputAsAttribute(Annotated ann)
public Boolean isOutputAsText(Annotated ann)
{
Boolean value = (_xmlPrimary == null) ? null : _xmlPrimary.isOutputAsText(ann);
if (value == null && _xmlSecondary != null) {
if ((value == null) && (_xmlSecondary != null)) {
value = _xmlSecondary.isOutputAsText(ann);
}
return value;
Expand All @@ -118,11 +123,21 @@ public Boolean isOutputAsText(Annotated ann)
@Override
public Boolean isOutputAsCData(Annotated ann) {
Boolean value = (_xmlPrimary == null) ? null : _xmlPrimary.isOutputAsCData(ann);
if (value == null && _xmlSecondary != null) {
if ((value == null) && (_xmlSecondary != null)) {
value = _xmlSecondary.isOutputAsCData(ann);
}
return value;
}

@Override
public void setDefaultUseWrapper(boolean b) {
if (_xmlPrimary != null) {
_xmlPrimary.setDefaultUseWrapper(b);
}
if (_xmlSecondary != null) {
_xmlSecondary.setDefaultUseWrapper(b);
}
}
}

/*
Expand Down Expand Up @@ -166,5 +181,10 @@ public Boolean isOutputAsCData(Annotated ann) {
//There is no CData annotation in JAXB
return null;
}

@Override
public void setDefaultUseWrapper(boolean b) {
// not used with JAXB
}
}
}
23 changes: 13 additions & 10 deletions src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public XmlMapper(XmlFactory xmlFactory, JacksonXmlModule module)
_serializationConfig = _serializationConfig.withDefaultPrettyPrinter(DEFAULT_XML_PRETTY_PRINTER);
}

// @since 2.1
@Override
public XmlMapper copy()
{
Expand Down Expand Up @@ -112,22 +111,26 @@ public Version version() {
protected void setXMLTextElementName(String name) {
((XmlFactory) _jsonFactory).setXMLTextElementName(name);
}
/**
* Since 2.7
*/
public XmlMapper setDefaultUseWrapper(boolean state) {
// ser and deser configs should usually have the same introspector, so:
AnnotationIntrospector ai0 = getDeserializationConfig().getAnnotationIntrospector();
for (AnnotationIntrospector ai : ai0.allIntrospectors()) {
if (ai instanceof XmlAnnotationIntrospector) {
((XmlAnnotationIntrospector) ai).setDefaultUseWrapper(state);
}
}
return this;
}

/*
/**********************************************************
/* Access to configuration settings
/**********************************************************
*/

/**
* @deprecated Since 2.1, use {@link #getFactory} instead
*/
@Override
@Deprecated
public XmlFactory getJsonFactory() {
return (XmlFactory) _jsonFactory;
}

@Override
public XmlFactory getFactory() {
return (XmlFactory) _jsonFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* implementation that
* builds on {@link JaxbAnnotationIntrospector}.
*<p>
* NOTE: since version 2.4, it should NOT be necessary to use this class;
* NOTE: since version 2.4, it may NOT be necessary to use this class;
* instead, plain {@link JaxbAnnotationIntrospector} should fully work.
* With previous versions some aspects were not fully working and this
* class was necessary.
Expand All @@ -23,7 +23,7 @@ public class XmlJaxbAnnotationIntrospector
extends JaxbAnnotationIntrospector
implements XmlAnnotationIntrospector
{
private static final long serialVersionUID = 6477843393758275877L;
private static final long serialVersionUID = 1L; // since 2.7

@Deprecated
public XmlJaxbAnnotationIntrospector() {
Expand All @@ -40,19 +40,16 @@ public XmlJaxbAnnotationIntrospector(TypeFactory typeFactory) {
/**********************************************************************
*/

// Since 2.4.0, JaxbAnnotationIntrospector has implementation, so delegate
@Override
public String findNamespace(Annotated ann) {
return super.findNamespace(ann);
}

// Since 2.4.0, JaxbAnnotationIntrospector has implementation, so delegate
@Override
public Boolean isOutputAsAttribute(Annotated ann) {
return super.isOutputAsAttribute(ann);
}

// Since 2.4.0, JaxbAnnotationIntrospector has implementation, so delegate
@Override
public Boolean isOutputAsText(Annotated ann) {
return super.isOutputAsText(ann);
Expand All @@ -64,6 +61,11 @@ public Boolean isOutputAsCData(Annotated ann) {
return null;
}

@Override
public void setDefaultUseWrapper(boolean b) {
// nothing to do with JAXB
}

/*
/**********************************************************************
/* Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ protected void serializeFields(Object bean, JsonGenerator gen0, SerializerProvid
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
} catch (StackOverflowError e) { // Bit tricky, can't do more calls as stack is full; so:
JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)");
JsonMappingException mapE = JsonMappingException.from(gen0,
"Infinite recursion (StackOverflowError)");
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
throw mapE;
Expand Down Expand Up @@ -294,7 +295,7 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator gen0,
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
} catch (StackOverflowError e) {
JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)", e);
JsonMappingException mapE = JsonMappingException.from(gen0, "Infinite recursion (StackOverflowError)", e);
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
throw mapE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public DefaultSerializerProvider createInstance(SerializationConfig config,

@SuppressWarnings("resource")
@Override
public void serializeValue(JsonGenerator jgen, Object value)
public void serializeValue(JsonGenerator gen, Object value)
throws IOException
{
if (value == null) {
_serializeXmlNull(jgen);
_serializeXmlNull(gen);
return;
}
final Class<?> cls = value.getClass();
final boolean asArray;
final ToXmlGenerator xgen = _asXmlGenerator(jgen);
final ToXmlGenerator xgen = _asXmlGenerator(gen);
if (xgen == null) { // called by convertValue()
asArray = false;
} else {
Expand All @@ -90,34 +90,34 @@ public void serializeValue(JsonGenerator jgen, Object value)
// From super-class implementation
final JsonSerializer<Object> ser = findTypedValueSerializer(cls, true, null);
try {
ser.serialize(value, jgen, this);
ser.serialize(value, gen, this);
} catch (IOException ioe) { // As per [JACKSON-99], pass IOException and subtypes as-is
throw ioe;
} catch (Exception e) { // but wrap RuntimeExceptions, to get path information
String msg = e.getMessage();
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw new JsonMappingException(msg, e);
throw JsonMappingException.from(gen, msg, e);
}
// end of super-class implementation

if (asArray) {
jgen.writeEndObject();
gen.writeEndObject();
}
}

@SuppressWarnings("resource")
@Override
public void serializeValue(JsonGenerator jgen, Object value, JavaType rootType)
public void serializeValue(JsonGenerator gen, Object value, JavaType rootType)
throws IOException
{
if (value == null) {
_serializeXmlNull(jgen);
_serializeXmlNull(gen);
return;
}
final boolean asArray;
final ToXmlGenerator xgen = _asXmlGenerator(jgen);
final ToXmlGenerator xgen = _asXmlGenerator(gen);
if (xgen == null) { // called by convertValue()
asArray = false;
} else {
Expand All @@ -135,35 +135,35 @@ public void serializeValue(JsonGenerator jgen, Object value, JavaType rootType)
final JsonSerializer<Object> ser = findTypedValueSerializer(rootType, true, null);
// From super-class implementation
try {
ser.serialize(value, jgen, this);
ser.serialize(value, gen, this);
} catch (IOException ioe) { // no wrapping for IO (and derived)
throw ioe;
} catch (Exception e) { // but others do need to be, to get path etc
String msg = e.getMessage();
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw new JsonMappingException(msg, e);
throw JsonMappingException.from(gen, msg, e);
}
// end of super-class implementation

if (asArray) {
jgen.writeEndObject();
gen.writeEndObject();
}
}

// @since 2.1
@SuppressWarnings("resource")
@Override
public void serializeValue(JsonGenerator jgen, Object value, JavaType rootType,
public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,
JsonSerializer<Object> ser) throws IOException
{
if (value == null) {
_serializeXmlNull(jgen);
_serializeXmlNull(gen);
return;
}
final boolean asArray;
final ToXmlGenerator xgen = _asXmlGenerator(jgen);
final ToXmlGenerator xgen = _asXmlGenerator(gen);
if (xgen == null) { // called by convertValue()
asArray = false;
} else {
Expand All @@ -182,19 +182,19 @@ public void serializeValue(JsonGenerator jgen, Object value, JavaType rootType,
}
// From super-class implementation
try {
ser.serialize(value, jgen, this);
ser.serialize(value, gen, this);
} catch (IOException ioe) { // no wrapping for IO (and derived)
throw ioe;
} catch (Exception e) { // but others do need to be, to get path etc
String msg = e.getMessage();
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw new JsonMappingException(msg, e);
throw JsonMappingException.from(gen, msg, e);
}
// end of super-class implementation
if (asArray) {
jgen.writeEndObject();
gen.writeEndObject();
}
}

Expand Down Expand Up @@ -252,18 +252,18 @@ protected QName _rootNameFromConfig()
return new QName(ns, name.getSimpleName());
}

protected ToXmlGenerator _asXmlGenerator(JsonGenerator jgen)
protected ToXmlGenerator _asXmlGenerator(JsonGenerator gen)
throws JsonMappingException
{
// [Issue#71]: When converting, we actually get TokenBuffer, which is fine
if (!(jgen instanceof ToXmlGenerator)) {
if (!(gen instanceof ToXmlGenerator)) {
// but verify
if (!(jgen instanceof TokenBuffer)) {
throw new JsonMappingException("XmlMapper does not with generators of type other than ToXmlGenerator; got: "
+jgen.getClass().getName());
}
return null;
if (!(gen instanceof TokenBuffer)) {
throw JsonMappingException.from(gen,
"XmlMapper does not with generators of type other than ToXmlGenerator; got: "+gen.getClass().getName());
}
return null;
}
return (ToXmlGenerator) jgen;
return (ToXmlGenerator) gen;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ static class VehicleActivity {
public void setUp() throws Exception
{
super.setUp();
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
_xmlMapper = new XmlMapper(module);
_xmlMapper = new XmlMapper()
.setDefaultUseWrapper(false);
_xmlMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy());
_xmlMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}
Expand Down

0 comments on commit f4c7bb0

Please sign in to comment.