| 1 |
package com.fasterxml.jackson.module.jaxb.misc; |
| 2 |
|
| 3 |
import org.w3c.dom.Document; |
| 4 |
import org.w3c.dom.Element; |
| 5 |
|
| 6 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 7 |
import java.io.ByteArrayInputStream; |
| 8 |
import java.io.StringWriter; |
| 9 |
|
| 10 |
import com.fasterxml.jackson.core.Version; |
| 11 |
import com.fasterxml.jackson.databind.*; |
| 12 |
import com.fasterxml.jackson.databind.module.SimpleModule; |
| 13 |
import com.fasterxml.jackson.databind.ser.Serializers; |
| 14 |
|
| 15 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 16 |
import com.fasterxml.jackson.module.jaxb.deser.DomElementJsonDeserializer; |
| 17 |
import com.fasterxml.jackson.module.jaxb.ser.DomElementJsonSerializer; |
| 18 |
|
| 19 |
/** |
| 20 |
* @author Ryan Heaton |
| 21 |
*/ |
| 22 |
public class TestDomElementSerialization extends BaseJaxbTest |
| 23 |
{ |
| 24 |
/* |
| 25 |
/********************************************************** |
| 26 |
/* Helper classes |
| 27 |
/********************************************************** |
| 28 |
*/ |
| 29 |
|
| 30 |
@SuppressWarnings("serial") |
| 31 |
private final static class DomModule extends SimpleModule |
| 32 |
{ |
| 33 |
public DomModule() |
| 34 |
{ |
| 35 |
super("DomModule", Version.unknownVersion()); |
| 36 |
addDeserializer(Element.class, new DomElementJsonDeserializer()); |
| 37 |
/* 19-Feb-2011, tatu: Note: since SimpleModule does not support "generic" |
| 38 |
* serializers, need to add bit more code here. |
| 39 |
*/ |
| 40 |
//testModule.addSerializer(new DomElementJsonSerializer()); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void setupModule(SetupContext context) |
| 45 |
{ |
| 46 |
super.setupModule(context); |
| 47 |
context.addSerializers(new DomSerializers()); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
private final static class DomSerializers extends Serializers.Base |
| 52 |
{ |
| 53 |
@Override |
| 54 |
public JsonSerializer<?> findSerializer(SerializationConfig config, |
| 55 |
JavaType type, BeanDescription beanDesc) |
| 56 |
{ |
| 57 |
if (Element.class.isAssignableFrom(type.getRawClass())) { |
| 58 |
return new DomElementJsonSerializer(); |
| 59 |
} |
| 60 |
return null; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
/********************************************************** |
| 66 |
/* Test methods |
| 67 |
/********************************************************** |
| 68 |
*/ |
| 69 |
|
| 70 |
public void testBasicDomElementSerializationDeserialization() throws Exception |
| 71 |
{ |
| 72 |
ObjectMapper mapper = new ObjectMapper(); |
| 73 |
mapper.registerModule(new DomModule()); |
| 74 |
|
| 75 |
StringBuilder builder = new StringBuilder() |
| 76 |
.append("<document xmlns=\"urn:hello\" att1=\"value1\" att2=\"value2\">") |
| 77 |
.append("<childel>howdy</childel>") |
| 78 |
.append("</document>"); |
| 79 |
|
| 80 |
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance(); |
| 81 |
bf.setNamespaceAware(true); |
| 82 |
Document document = bf.newDocumentBuilder().parse(new ByteArrayInputStream(builder.toString().getBytes("utf-8"))); |
| 83 |
StringWriter jsonElement = new StringWriter(); |
| 84 |
mapper.writeValue(jsonElement, document.getDocumentElement()); |
| 85 |
|
| 86 |
Element el = mapper.readValue(jsonElement.toString(), Element.class); |
| 87 |
assertEquals(3, el.getAttributes().getLength()); |
| 88 |
assertEquals("value1", el.getAttributeNS(null, "att1")); |
| 89 |
assertEquals("value2", el.getAttributeNS(null, "att2")); |
| 90 |
assertEquals(1, el.getChildNodes().getLength()); |
| 91 |
assertEquals("childel", el.getChildNodes().item(0).getLocalName()); |
| 92 |
assertEquals("urn:hello", el.getChildNodes().item(0).getNamespaceURI()); |
| 93 |
assertEquals("howdy", el.getChildNodes().item(0).getTextContent()); |
| 94 |
} |
| 95 |
} |