Skip to content

Commit a02bba3

Browse files
committed
DataOutput test
1 parent cb7b84a commit a02bba3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.xml;
22

3+
import java.io.DataOutput;
34
import java.io.File;
45
import java.io.FileOutputStream;
56
import java.io.IOException;
@@ -13,6 +14,7 @@
1314

1415
import com.fasterxml.jackson.core.*;
1516
import com.fasterxml.jackson.core.exc.StreamWriteException;
17+
import com.fasterxml.jackson.core.io.DataOutputAsStream;
1618
import com.fasterxml.jackson.core.type.TypeReference;
1719
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
1820
import com.fasterxml.jackson.databind.*;
@@ -459,6 +461,12 @@ public void writeValue(OutputStream out, Object value, Charset encoding)
459461
_writeValueAndClose(createGenerator(out, encoding), value);
460462
}
461463

464+
public void writeValue(DataOutput out, Object value, Charset encoding)
465+
throws IOException, StreamWriteException, DatabindException
466+
{
467+
_writeValueAndClose(createGenerator(out, encoding), value);
468+
}
469+
462470
protected final JsonGenerator createGenerator(OutputStream out, Charset encoding) throws IOException {
463471
this._assertNotNull("out", out);
464472
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(out, encoding);
@@ -473,4 +481,11 @@ protected final JsonGenerator createGenerator(File outputFile, Charset encoding)
473481
_serializationConfig.initialize(g);
474482
return g;
475483
}
484+
485+
protected final JsonGenerator createGenerator(DataOutput out, Charset encoding) throws IOException {
486+
this._assertNotNull("out", out);
487+
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(new DataOutputAsStream(out), encoding);
488+
this._serializationConfig.initialize(g);
489+
return g;
490+
}
476491
}

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestCharset.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static class StringBean {
1212
public String 象形字;
1313
}
1414

15-
public void testBig5() throws IOException
15+
public void testBig5Bytes() throws IOException
1616
{
1717
Charset big5 = Charset.forName("Big5");
1818
StringBean stringBean = new StringBean();
@@ -25,4 +25,16 @@ public void testBig5() throws IOException
2525
"<?xml version='1.1' encoding='Big5'?><StringBean><象形字>pictogram</象形字></StringBean>";
2626
assertEquals(expected, xmlText);
2727
}
28+
29+
public void testBig5RoundTrip() throws IOException
30+
{
31+
Charset big5 = Charset.forName("Big5");
32+
StringBean stringBean = new StringBean();
33+
stringBean.象形字 = "pictogram";
34+
XmlMapper xmlMapper = new XmlMapper();
35+
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
36+
byte[] xml = xmlMapper.writeValueAsBytes(stringBean, big5);
37+
StringBean stringBean1 = xmlMapper.readValue(xml, StringBean.class);
38+
assertEquals(stringBean.象形字, stringBean1.象形字);
39+
}
2840
}

0 commit comments

Comments
 (0)