Skip to content

Support unwrapping in @JsonRawValue serialization #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -44,6 +46,12 @@ static class Radius {
public int value;
}

static class RawValue {
@JacksonXmlText
@JsonRawValue
public String foo = "<a>b</a>";
}


/*
/**********************************************************
Expand Down Expand Up @@ -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("<RawValue><a>b</a></RawValue>", xml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,60 @@ public void testRawCharArrayValue() throws Exception
assertEquals("<root><elem>value</elem></root>", 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("<root>value</root>", 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("<root>value</root>", 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("<root>value</root>", xml);
}

public void testRawSimpleAttribute() throws Exception
{
StringWriter out = new StringWriter();
Expand Down