Skip to content

Commit 3c55e98

Browse files
committed
Support unwrapping in @JsonRawValue serialization
Fixes https://github.com/FasterXML/jackson-databind/issues/3581
1 parent 6c03760 commit 3c55e98

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

+6
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@ public void writeRawValue(String text) throws IOException {
732732

733733
if (_nextIsAttribute) {
734734
_xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), text);
735+
} else if (checkNextIsUnwrapped()) {
736+
_xmlWriter.writeRaw(text);
735737
} else {
736738
_xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart());
737739
_xmlWriter.writeRaw(text);
@@ -756,6 +758,8 @@ public void writeRawValue(String text, int offset, int len) throws IOException {
756758

757759
if (_nextIsAttribute) {
758760
_xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), text.substring(offset, offset + len));
761+
} else if (checkNextIsUnwrapped()) {
762+
_xmlWriter.writeRaw(text, offset, len);
759763
} else {
760764
_xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart());
761765
_xmlWriter.writeRaw(text, offset, len);
@@ -779,6 +783,8 @@ public void writeRawValue(char[] text, int offset, int len) throws IOException {
779783
try {
780784
if (_nextIsAttribute) {
781785
_xmlWriter.writeAttribute(_nextName.getNamespaceURI(), _nextName.getLocalPart(), new String(text, offset, len));
786+
} else if (checkNextIsUnwrapped()) {
787+
_xmlWriter.writeRaw(text, offset, len);
782788
} else {
783789
_xmlWriter.writeStartElement(_nextName.getNamespaceURI(), _nextName.getLocalPart());
784790
_xmlWriter.writeRaw(text, offset, len);

src/test/java/com/fasterxml/jackson/dataformat/xml/misc/XmlTextTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
44
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import com.fasterxml.jackson.annotation.JsonRawValue;
56
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
67
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
78
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
89
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
10+
import org.junit.Assert;
911

1012
public class XmlTextTest extends XmlTestBase
1113
{
@@ -44,6 +46,12 @@ static class Radius {
4446
public int value;
4547
}
4648

49+
static class RawValue {
50+
@JacksonXmlText
51+
@JsonRawValue
52+
public String foo = "<a>b</a>";
53+
}
54+
4755

4856
/*
4957
/**********************************************************
@@ -79,4 +87,11 @@ public void testSimple198() throws Exception
7987
Phone result = MAPPER.readValue(xml, Phone.class);
8088
assertNotNull(result);
8189
}
90+
91+
// for [dataformat-xml#3581]
92+
public void testRawValue() throws Exception
93+
{
94+
String xml = MAPPER.writeValueAsString(new RawValue());
95+
Assert.assertEquals("<RawValue><a>b</a></RawValue>", xml);
96+
}
8297
}

src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlGeneratorTest.java

+54
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,60 @@ public void testRawCharArrayValue() throws Exception
183183
assertEquals("<root><elem>value</elem></root>", xml);
184184
}
185185

186+
public void testRawSimpleValueUnwrapped() throws Exception
187+
{
188+
StringWriter out = new StringWriter();
189+
ToXmlGenerator gen = XML_F.createGenerator(out);
190+
// root name is special, need to be fed first:
191+
gen.setNextName(new QName("root"));
192+
gen.writeStartObject();
193+
gen.setNextIsUnwrapped(true);
194+
gen.writeFieldName("elem");
195+
gen.writeRawValue("value");
196+
gen.writeEndObject();
197+
gen.close();
198+
String xml = out.toString();
199+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
200+
xml = removeSjsxpNamespace(xml);
201+
assertEquals("<root>value</root>", xml);
202+
}
203+
204+
public void testRawOffsetValueUnwrapped() throws Exception
205+
{
206+
StringWriter out = new StringWriter();
207+
ToXmlGenerator gen = XML_F.createGenerator(out);
208+
// root name is special, need to be fed first:
209+
gen.setNextName(new QName("root"));
210+
gen.writeStartObject();
211+
gen.setNextIsUnwrapped(true);
212+
gen.writeFieldName("elem");
213+
gen.writeRawValue("NotAValue_value_NotAValue", 10, 5);
214+
gen.writeEndObject();
215+
gen.close();
216+
String xml = out.toString();
217+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
218+
xml = removeSjsxpNamespace(xml);
219+
assertEquals("<root>value</root>", xml);
220+
}
221+
222+
public void testRawCharArrayValueUnwrapped() throws Exception
223+
{
224+
StringWriter out = new StringWriter();
225+
ToXmlGenerator gen = XML_F.createGenerator(out);
226+
// root name is special, need to be fed first:
227+
gen.setNextName(new QName("root"));
228+
gen.writeStartObject();
229+
gen.setNextIsUnwrapped(true);
230+
gen.writeFieldName("elem");
231+
gen.writeRawValue(new char[] {'!', 'v', 'a', 'l', 'u', 'e', '!'}, 1, 5);
232+
gen.writeEndObject();
233+
gen.close();
234+
String xml = out.toString();
235+
// one more thing: remove that annoying 'xmlns' decl, if it's there:
236+
xml = removeSjsxpNamespace(xml);
237+
assertEquals("<root>value</root>", xml);
238+
}
239+
186240
public void testRawSimpleAttribute() throws Exception
187241
{
188242
StringWriter out = new StringWriter();

0 commit comments

Comments
 (0)