diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java index 00f051d68..f43309c2b 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java @@ -732,6 +732,8 @@ public void writeRawValue(String text) throws IOException { if (_nextIsAttribute) { _xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), text); + } else if (checkNextIsUnwrapped()) { + _xmlWriter.writeRaw(text); } else { _xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart()); _xmlWriter.writeRaw(text); @@ -756,6 +758,8 @@ public void writeRawValue(String text, int offset, int len) throws IOException { if (_nextIsAttribute) { _xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), text.substring(offset, offset + len)); + } else if (checkNextIsUnwrapped()) { + _xmlWriter.writeRaw(text, offset, len); } else { _xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart()); _xmlWriter.writeRaw(text, offset, len); @@ -779,6 +783,8 @@ public void writeRawValue(char[] text, int offset, int len) throws IOException { try { if (_nextIsAttribute) { _xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), new String(text, offset, len)); + } else if (checkNextIsUnwrapped()) { + _xmlWriter.writeRaw(text, offset, len); } else { _xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart()); _xmlWriter.writeRaw(text, offset, len); diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/misc/XmlTextTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/misc/XmlTextTest.java index 6bfc69faa..ee7d66009 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/misc/XmlTextTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/misc/XmlTextTest.java @@ -2,10 +2,12 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonRawValue; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlTestBase; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; +import org.junit.Assert; public class XmlTextTest extends XmlTestBase { @@ -44,6 +46,12 @@ static class Radius { public int value; } + static class RawValue { + @JacksonXmlText + @JsonRawValue + public String foo = "b"; + } + /* /********************************************************** @@ -79,4 +87,11 @@ public void testSimple198() throws Exception Phone result = MAPPER.readValue(xml, Phone.class); assertNotNull(result); } + + // for [dataformat-xml#3581] + public void testRawValue() throws Exception + { + String xml = MAPPER.writeValueAsString(new RawValue()); + Assert.assertEquals("b", xml); + } } diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlGeneratorTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlGeneratorTest.java index f6ff0f882..9852322c8 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlGeneratorTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlGeneratorTest.java @@ -183,6 +183,60 @@ public void testRawCharArrayValue() throws Exception assertEquals("value", xml); } + public void testRawSimpleValueUnwrapped() throws Exception + { + StringWriter out = new StringWriter(); + ToXmlGenerator gen = XML_F.createGenerator(out); + // root name is special, need to be fed first: + gen.setNextName(new QName("root")); + gen.writeStartObject(); + gen.setNextIsUnwrapped(true); + gen.writeFieldName("elem"); + gen.writeRawValue("value"); + gen.writeEndObject(); + gen.close(); + String xml = out.toString(); + // one more thing: remove that annoying 'xmlns' decl, if it's there: + xml = removeSjsxpNamespace(xml); + assertEquals("value", xml); + } + + public void testRawOffsetValueUnwrapped() throws Exception + { + StringWriter out = new StringWriter(); + ToXmlGenerator gen = XML_F.createGenerator(out); + // root name is special, need to be fed first: + gen.setNextName(new QName("root")); + gen.writeStartObject(); + gen.setNextIsUnwrapped(true); + gen.writeFieldName("elem"); + gen.writeRawValue("NotAValue_value_NotAValue", 10, 5); + gen.writeEndObject(); + gen.close(); + String xml = out.toString(); + // one more thing: remove that annoying 'xmlns' decl, if it's there: + xml = removeSjsxpNamespace(xml); + assertEquals("value", xml); + } + + public void testRawCharArrayValueUnwrapped() throws Exception + { + StringWriter out = new StringWriter(); + ToXmlGenerator gen = XML_F.createGenerator(out); + // root name is special, need to be fed first: + gen.setNextName(new QName("root")); + gen.writeStartObject(); + gen.setNextIsUnwrapped(true); + gen.writeFieldName("elem"); + gen.writeRawValue(new char[] {'!', 'v', 'a', 'l', 'u', 'e', '!'}, 1, 5); + gen.writeEndObject(); + gen.close(); + String xml = out.toString(); + // one more thing: remove that annoying 'xmlns' decl, if it's there: + xml = removeSjsxpNamespace(xml); + assertEquals("value", xml); + } + public void testRawSimpleAttribute() throws Exception { StringWriter out = new StringWriter();