Search in sources :

Example 1 with XmlFactory

use of com.fasterxml.jackson.dataformat.xml.XmlFactory in project immutables by immutables.

the class GsonJacksonBridgeSerializationTest method xmlFactoryTest.

@Test
public void xmlFactoryTest() throws IOException {
    TestObject value = createTestObject();
    XmlFactory factory = new XmlFactory();
    Class<TestObject> clazz = TestObject.class;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ToXmlGenerator g = factory.createGenerator(outputStream);
    g.setNextName(QName.valueOf(clazz.getAnnotation(JacksonXmlRootElement.class).localName()));
    JsonGeneratorWriter generatorWriter = new JsonGeneratorWriter(g);
    gson.toJson(value, clazz, generatorWriter);
    generatorWriter.flush();
    TestObject value2 = testXmlReading(factory, clazz, outputStream);
    Assert.assertEquals(value2.toString(), value.toString());
}
Also used : JsonGeneratorWriter(org.immutables.gson.stream.JsonGeneratorWriter) XmlFactory(com.fasterxml.jackson.dataformat.xml.XmlFactory) ToXmlGenerator(com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator) JacksonXmlRootElement(com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with XmlFactory

use of com.fasterxml.jackson.dataformat.xml.XmlFactory in project dhis2-core by dhis2.

the class CustomLastUpdatedUserSerializerTest method serializeXml.

@Test
void serializeXml() throws Exception {
    Writer jsonWriter = new StringWriter();
    ToXmlGenerator jsonGenerator = new XmlFactory().createGenerator(jsonWriter);
    SerializerProvider serializerProvider = new ObjectMapper().getSerializerProvider();
    jsonGenerator.setNextName(new QName("urn:test", "lastUpdatedBy"));
    new CustomLastUpdatedUserSerializer().serialize(user, jsonGenerator, serializerProvider);
    jsonGenerator.flush();
    assertEquals("<wstxns1:lastUpdatedBy xmlns:wstxns1=\"urn:test\" id=\"jshfdkd323\" name=\"Peter Brown\"/>", jsonWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) QName(javax.xml.namespace.QName) XmlFactory(com.fasterxml.jackson.dataformat.xml.XmlFactory) ToXmlGenerator(com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) StringWriter(java.io.StringWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 3 with XmlFactory

use of com.fasterxml.jackson.dataformat.xml.XmlFactory in project dhis2-core by dhis2.

the class StAXNodeSerializer method startSerialize.

@Override
protected void startSerialize(RootNode rootNode, OutputStream outputStream) throws Exception {
    writer = xmlFactory.createXMLStreamWriter(outputStream);
    generator = (new XmlFactory()).createGenerator(writer);
    generator.setCodec(new XmlMapper());
    writer.setDefaultNamespace(rootNode.getDefaultNamespace());
}
Also used : XmlFactory(com.fasterxml.jackson.dataformat.xml.XmlFactory) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

XmlFactory (com.fasterxml.jackson.dataformat.xml.XmlFactory)3 ToXmlGenerator (com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 JacksonXmlRootElement (com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 QName (javax.xml.namespace.QName)1 JsonGeneratorWriter (org.immutables.gson.stream.JsonGeneratorWriter)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1