| 1 |
package com.fasterxml.jackson.module.jaxb; |
| 2 |
|
| 3 |
import com.fasterxml.jackson.annotation.JsonInclude; |
| 4 |
import com.fasterxml.jackson.core.Version; |
| 5 |
import com.fasterxml.jackson.databind.Module; |
| 6 |
|
| 7 |
/** |
| 8 |
* Module that can be registered to add support for JAXB annotations. |
| 9 |
* It does basically equivalent of |
| 10 |
*<pre> |
| 11 |
* objectMapper.setAnnotationIntrospector(...); |
| 12 |
*</pre> |
| 13 |
* with combination of {@link JaxbAnnotationIntrospector} and existing |
| 14 |
* default introspector(s) (if any), depending on configuration |
| 15 |
* (by default, JAXB annotations are used as {@link Priority#PRIMARY} |
| 16 |
* annotations). |
| 17 |
*/ |
| 18 |
public class JaxbAnnotationModule extends Module |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Enumeration that defines how we use JAXB Annotations: either |
| 22 |
* as "primary" annotations (before any other already configured |
| 23 |
* introspector -- most likely default JacksonAnnotationIntrospector) or |
| 24 |
* as "secondary" annotations (after any other already configured |
| 25 |
* introspector(s)). |
| 26 |
*<p> |
| 27 |
* Default choice is <b>PRIMARY</b> |
| 28 |
*<p> |
| 29 |
* Note that if you want to use JAXB annotations as the only annotations, |
| 30 |
* you must directly set annotation introspector by calling |
| 31 |
* {@link com.fasterxml.jackson.databind.ObjectMapper#setAnnotationIntrospector}. |
| 32 |
*/ |
| 33 |
public enum Priority { |
| 34 |
PRIMARY, SECONDARY; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Priority to use when registering annotation introspector: default |
| 39 |
* value is {@link Priority#PRIMARY}. |
| 40 |
*/ |
| 41 |
protected Priority _priority = Priority.PRIMARY; |
| 42 |
|
| 43 |
/** |
| 44 |
* If the introspector is explicitly set or passed, we'll hold on to that |
| 45 |
* until registration. |
| 46 |
* |
| 47 |
* @since 2.7 |
| 48 |
*/ |
| 49 |
protected JaxbAnnotationIntrospector _introspector; |
| 50 |
|
| 51 |
/** |
| 52 |
* Value to pass to |
| 53 |
* {@link JaxbAnnotationIntrospector#setNonNillableInclusion} |
| 54 |
* if defined and non-null. |
| 55 |
* |
| 56 |
* @since 2.7 |
| 57 |
*/ |
| 58 |
protected JsonInclude.Include _nonNillableInclusion; |
| 59 |
|
| 60 |
/* |
| 61 |
/********************************************************** |
| 62 |
/* Life cycle |
| 63 |
/********************************************************** |
| 64 |
*/ |
| 65 |
|
| 66 |
public JaxbAnnotationModule() { } |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 2.7 |
| 70 |
*/ |
| 71 |
public JaxbAnnotationModule(JaxbAnnotationIntrospector intr) { |
| 72 |
_introspector = intr; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public String getModuleName() { |
| 77 |
return getClass().getSimpleName(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public Version version() { |
| 82 |
return PackageVersion.VERSION; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void setupModule(SetupContext context) |
| 87 |
{ |
| 88 |
JaxbAnnotationIntrospector intr = _introspector; |
| 89 |
if (intr == null) { |
| 90 |
intr = new JaxbAnnotationIntrospector(context.getTypeFactory()); |
| 91 |
if (_nonNillableInclusion != null) { |
| 92 |
intr.setNonNillableInclusion(_nonNillableInclusion); |
| 93 |
} |
| 94 |
} |
| 95 |
switch (_priority) { |
| 96 |
case PRIMARY: |
| 97 |
context.insertAnnotationIntrospector(intr); |
| 98 |
break; |
| 99 |
case SECONDARY: |
| 100 |
context.appendAnnotationIntrospector(intr); |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
/********************************************************** |
| 107 |
/* Configuration |
| 108 |
/********************************************************** |
| 109 |
*/ |
| 110 |
|
| 111 |
/** |
| 112 |
* Method for defining whether JAXB annotations should be added |
| 113 |
* as primary or secondary annotations (compared to already registered |
| 114 |
* annotations). |
| 115 |
*<p> |
| 116 |
* NOTE: method MUST be called before registering the module -- calling |
| 117 |
* afterwards will not have any effect on previous registrations. |
| 118 |
*/ |
| 119 |
public JaxbAnnotationModule setPriority(Priority p) { |
| 120 |
_priority = p; |
| 121 |
return this; |
| 122 |
} |
| 123 |
|
| 124 |
public Priority getPriority() { return _priority; } |
| 125 |
|
| 126 |
/** |
| 127 |
* @since 2.7 |
| 128 |
*/ |
| 129 |
public JaxbAnnotationModule setNonNillableInclusion(JsonInclude.Include incl) { |
| 130 |
_nonNillableInclusion = incl; |
| 131 |
if (_introspector != null) { |
| 132 |
_introspector.setNonNillableInclusion(incl); |
| 133 |
} |
| 134 |
return this; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @since 2.7 |
| 139 |
*/ |
| 140 |
public JsonInclude.Include getNonNillableInclusion() { |
| 141 |
return _nonNillableInclusion; |
| 142 |
} |
| 143 |
} |