Package com.fasterxml.jackson.dataformat.xml

Examples of com.fasterxml.jackson.dataformat.xml.XmlMapper


       
    private void _testListSerialization(boolean useWrapping) throws Exception
    {
        JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(useWrapping);
        XmlMapper xmlMapper = new XmlMapper(module);
        AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
        xmlMapper.setAnnotationIntrospector(introspector);

        SampleResource r1 = new SampleResource();
        r1.setId(123L);
        r1.setName("Albert");
        r1.setDescription("desc");

        SampleResource r2 = new SampleResource();
        r2.setId(123L);
        r2.setName("William");
        r2.setDescription("desc2");

        List<SampleResource> l = new ArrayList<SampleResource>();
        l.add(r1);
        l.add(r2);

        // to see what JAXB might do, uncomment:
//System.out.println("By JAXB: "+jaxbSerialized(l)); //  ArrayList.class, SampleResource.class));

        String xml = xmlMapper
            .writerWithDefaultPrettyPrinter()
            .writeValueAsString(l)
            .trim();

        // first trivial sanity checks
        assertNotNull(xml);
        if (xml.indexOf("<ArrayList>") < 0) {
            fail("Unexpected output: should have <ArrayList> as root element, got: "+xml);
        }

        // and then try reading back
        JavaType resListType = xmlMapper.getTypeFactory()
                .constructCollectionType(List.class, SampleResource.class);
        Object ob = xmlMapper.reader(resListType).readValue(xml);
        assertNotNull(ob);

//      System.err.println("XML -> "+xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(ob));
       
        assertTrue(ob instanceof List);
View Full Code Here


   
    private void _testArraySerialization(boolean useWrapping) throws Exception
    {
        JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(useWrapping);
        XmlMapper xmlMapper = new XmlMapper(module);
        AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
        xmlMapper.setAnnotationIntrospector(introspector);

        SampleResource r1 = new SampleResource();
        r1.setId(123L);
        r1.setName("Albert");
        r1.setDescription("desc");

        SampleResource r2 = new SampleResource();
        r2.setId(123L);
        r2.setName("William");
        r2.setDescription("desc2");

        SampleResource[] input = new SampleResource[] { r1, r2 };

        // to see what JAXB might do, uncomment:
//System.out.println("By JAXB: "+jaxbSerialized(input));

        String xml = xmlMapper
            .writerWithDefaultPrettyPrinter()
            .writeValueAsString(input)
            .trim();

        // first trivial sanity checks
        assertNotNull(xml);
        // Is this good name? If not, what should be used instead?
        if (xml.indexOf("<SampleResources>") < 0) {
            fail("Unexpected output: should have <SampleResources> as root element, got: "+xml);
        }

        // and then try reading back
        SampleResource[] result = xmlMapper.reader(SampleResource[].class).readValue(xml);
        assertNotNull(result);

//      System.err.println("XML -> "+xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(ob));
       
        assertEquals(2, result.length);
View Full Code Here

public class VersionInfoTest extends XmlTestBase
{
    public void testMapperVersions()
    {
        assertVersion(new XmlMapper());
        assertVersion(new XmlFactory());
    }
View Full Code Here

    // @since 2.1
    // [Issue#48]: ObjectMapper.copy()
    public void testMapperCopy()
    {
        XmlMapper mapper1 = new XmlMapper();
        mapper1.setXMLTextElementName("foo");
        mapper1.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
       
        XmlMapper mapper2 = mapper1.copy();
        assertNotSame(mapper1, mapper2);
        XmlFactory xf1 = mapper1.getFactory();
        XmlFactory xf2 = mapper2.getFactory();
        assertNotSame(xf1, xf2);
        assertEquals(XmlFactory.class, xf2.getClass());

        // and [Issue#48] as well, incomplete copy...
        assertEquals(xf1.getXMLTextElementName(), xf2.getXMLTextElementName());
View Full Code Here

    }

    // Another test for [Issue#48]
    public void testMapperSerialization() throws Exception
    {
        XmlMapper mapper1 = new XmlMapper();
        mapper1.setXMLTextElementName("foo");
        assertEquals("foo", mapper1.getFactory().getXMLTextElementName());

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ObjectOutputStream objectStream = new ObjectOutputStream(bytes);
        objectStream.writeObject(mapper1);
        objectStream.close();
       
        ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
        XmlMapper mapper2 = (XmlMapper) input.readObject();
        input.close();

        assertEquals("foo", mapper2.getFactory().getXMLTextElementName());
    }
View Full Code Here

    /**********************************************************
     */

    public void testIssue42() throws Exception
    {
        XmlMapper xmlMapper = new XmlMapper();
        SimpleModule m = new SimpleModule("module", new Version(1,0,0,null,null,null));
        m.addSerializer(Item.class, new ItemSerializer());
        m.addDeserializer(Item.class, new ItemDeserializer());
        xmlMapper.registerModule(m);

        Item value = new Item("itemName", new Foo("fooName"));
        String xml = xmlMapper.writeValueAsString(value);
       
        Item result = xmlMapper.readValue(xml, Item.class);
        assertNotNull(result);
        assertEquals("itemName", result.name);
        assertNotNull(result.obj);
        assertEquals("fooName", result.obj.name);
    }
View Full Code Here

    @Override
    public void setUp() throws Exception {
        super.setUp();
        _jsonFactory = new JsonFactory();
        _xmlFactory = new XmlFactory();
        _xmlMapper = new XmlMapper();
    }
View Full Code Here

    /**
     * [Issue#60]: should be able to detect "no content", ideally.
     */
    public void testNoContent() throws Exception
    {
        XmlMapper mapper = new XmlMapper();
        assertNull(mapper.readValue("", EmptyBean.class));
    }
View Full Code Here

            new Issue86("0.2", null),
            new Issue86("0.3",
                Arrays.asList(
                    new Issue86("0.3.1", null)))));

    final XmlMapper mapper = new XmlMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);

    final String xml = mapper.writeValueAsString(before);
    assertEquals(source, xml);

    final Issue86 after = mapper.readValue(xml, Issue86.class);
    assertEquals(before, after);
  }
View Full Code Here

    // let's actually reuse XmlMapper to make things bit faster
    @Override
    public void setUp() throws Exception {
        super.setUp();
        _xmlMapper = new XmlMapper();
    }
View Full Code Here

TOP

Related Classes of com.fasterxml.jackson.dataformat.xml.XmlMapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.