| 1 |
package com.fasterxml.jackson.module.jaxb; |
| 2 |
|
| 3 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
| 4 |
|
| 5 |
import com.fasterxml.jackson.databind.JavaType; |
| 6 |
import com.fasterxml.jackson.databind.type.TypeFactory; |
| 7 |
import com.fasterxml.jackson.databind.util.StdConverter; |
| 8 |
|
| 9 |
public class AdapterConverter |
| 10 |
extends StdConverter<Object,Object> |
| 11 |
{ |
| 12 |
protected final JavaType _inputType, _targetType; |
| 13 |
|
| 14 |
protected final XmlAdapter<Object,Object> _adapter; |
| 15 |
|
| 16 |
protected final boolean _forSerialization; |
| 17 |
|
| 18 |
@SuppressWarnings("unchecked") |
| 19 |
public AdapterConverter(XmlAdapter<?,?> adapter, |
| 20 |
JavaType inType, JavaType outType, boolean ser) |
| 21 |
{ |
| 22 |
_adapter = (XmlAdapter<Object,Object>) adapter; |
| 23 |
_inputType = inType; |
| 24 |
_targetType = outType; |
| 25 |
_forSerialization = ser; |
| 26 |
} |
| 27 |
|
| 28 |
@Override |
| 29 |
public Object convert(Object value) |
| 30 |
{ |
| 31 |
try { |
| 32 |
if (_forSerialization) { |
| 33 |
return _adapter.marshal(value); |
| 34 |
} |
| 35 |
return _adapter.unmarshal(value); |
| 36 |
} catch (RuntimeException e) { |
| 37 |
throw e; |
| 38 |
} catch (Exception e) { |
| 39 |
throw new IllegalArgumentException(e); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public JavaType getInputType(TypeFactory typeFactory) { |
| 45 |
return _inputType; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public JavaType getOutputType(TypeFactory typeFactory) { |
| 50 |
return _targetType; |
| 51 |
} |
| 52 |
|
| 53 |
} |