| 1 |
package com.fasterxml.jackson.jr.annotationsupport; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.lang.reflect.Field; |
| 5 |
import java.lang.reflect.Method; |
| 6 |
import java.lang.reflect.Modifier; |
| 7 |
import java.util.*; |
| 8 |
|
| 9 |
import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| 10 |
import com.fasterxml.jackson.annotation.JsonCreator; |
| 11 |
import com.fasterxml.jackson.annotation.JsonProperty; |
| 12 |
|
| 13 |
import com.fasterxml.jackson.annotation.JsonValue; |
| 14 |
import com.fasterxml.jackson.core.JsonGenerator; |
| 15 |
|
| 16 |
import com.fasterxml.jackson.core.JsonParser; |
| 17 |
import com.fasterxml.jackson.jr.ob.JSONObjectException; |
| 18 |
import com.fasterxml.jackson.jr.ob.api.ReaderWriterModifier; |
| 19 |
import com.fasterxml.jackson.jr.ob.api.ValueReader; |
| 20 |
import com.fasterxml.jackson.jr.ob.api.ValueWriter; |
| 21 |
import com.fasterxml.jackson.jr.ob.impl.JSONReader; |
| 22 |
import com.fasterxml.jackson.jr.ob.impl.JSONWriter; |
| 23 |
import com.fasterxml.jackson.jr.ob.impl.POJODefinition; |
| 24 |
import com.fasterxml.jackson.jr.ob.impl.ValueLocatorBase; |
| 25 |
|
| 26 |
public class AnnotationBasedValueRWModifier extends ReaderWriterModifier |
| 27 |
{ |
| 28 |
// Has to match SER_ENUM code in ValueLocatorBase |
| 29 |
protected static final int SER_ENUM_ID = ValueLocatorBase.SER_ENUM; |
| 30 |
|
| 31 |
/** |
| 32 |
* Visibility settings to use for auto-detecting accessors. |
| 33 |
*/ |
| 34 |
protected final JsonAutoDetect.Value _visibility; |
| 35 |
|
| 36 |
public AnnotationBasedValueRWModifier(JsonAutoDetect.Value visibility) { |
| 37 |
_visibility = visibility; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public POJODefinition pojoDefinitionForDeserialization(JSONReader readContext, Class<?> pojoType) { |
| 42 |
return AnnotationBasedIntrospector.pojoDefinitionForDeserialization(readContext, pojoType, _visibility); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public POJODefinition pojoDefinitionForSerialization(JSONWriter writeContext, Class<?> pojoType) { |
| 47 |
return AnnotationBasedIntrospector.pojoDefinitionForSerialization(writeContext, pojoType, _visibility); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public ValueWriter overrideStandardValueWriter(JSONWriter writeContext, Class<?> type, int stdTypeId) { |
| 52 |
if (stdTypeId == SER_ENUM_ID) { |
| 53 |
ValueWriter writeByJsonValue = EnumJsonValueWriter.of(type); |
| 54 |
if (writeByJsonValue != null) { |
| 55 |
return writeByJsonValue; |
| 56 |
} |
| 57 |
// the EnumWriter requires this to be the actual enum type, rather than a subclass of Enum |
| 58 |
if (type.isEnum()) { |
| 59 |
return new EnumWriter(type); |
| 60 |
} |
| 61 |
} |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
@Override // since 2.14 |
| 66 |
public ValueReader modifyValueReader(JSONReader readContext, Class<?> type, ValueReader defaultReader) { |
| 67 |
if (type.isEnum()) { |
| 68 |
ValueReader readUsingJsonCreator = EnumJsonCreatorReader.of(type); |
| 69 |
if (readUsingJsonCreator != null) { |
| 70 |
return readUsingJsonCreator; |
| 71 |
} |
| 72 |
} |
| 73 |
return super.modifyValueReader(readContext, type, defaultReader); |
| 74 |
} |
| 75 |
|
| 76 |
private static class EnumWriter implements ValueWriter { |
| 77 |
private final Class<?> _valueType; |
| 78 |
private final Map<String, String> enumMap; |
| 79 |
|
| 80 |
public EnumWriter(Class<?> type) { |
| 81 |
_valueType = type; |
| 82 |
enumMap = new HashMap<String, String>(); |
| 83 |
Field[] fields = type.getDeclaredFields(); |
| 84 |
for (Field field : fields) { |
| 85 |
if (field.isAnnotationPresent(JsonProperty.class)) { |
| 86 |
enumMap.put(field.getName(), field.getAnnotation(JsonProperty.class).value()); |
| 87 |
} else { |
| 88 |
enumMap.put(field.getName(), field.getName()); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException { |
| 95 |
context.writeValue(enumMap.get(((Enum<?>) value).name())); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public Class<?> valueType() { |
| 100 |
return _valueType; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Serialize an enum using the {@link JsonValue} tagged method. |
| 106 |
* |
| 107 |
* @since 2.14 |
| 108 |
*/ |
| 109 |
private static class EnumJsonValueWriter implements ValueWriter |
| 110 |
{ |
| 111 |
private final Class<?> _valueType; |
| 112 |
private final Method _jsonValueMethod; |
| 113 |
|
| 114 |
private EnumJsonValueWriter(Class<?> _valueType, Method _jsonValueMethod) { |
| 115 |
this._valueType = _valueType; |
| 116 |
this._jsonValueMethod = _jsonValueMethod; |
| 117 |
_jsonValueMethod.setAccessible(true); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException { |
| 122 |
try { |
| 123 |
context.writeValue(_jsonValueMethod.invoke(value)); |
| 124 |
} catch (Exception e) { |
| 125 |
throw new JSONObjectException("Cannot call JsonValue method", e); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public Class<?> valueType() { |
| 131 |
return _valueType; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Scan the methods of the enum to find a {@link JsonValue} tagged method |
| 136 |
* which takes no parameters and returns a {@link String} |
| 137 |
* @param type the type |
| 138 |
* @return either a {@link EnumJsonValueWriter} to write this enum with, or <code>null</code> if no suitable |
| 139 |
* method found |
| 140 |
*/ |
| 141 |
public static EnumJsonValueWriter of(Class<?> type) { |
| 142 |
return getJsonValueFunction(type, type); |
| 143 |
} |
| 144 |
|
| 145 |
private static EnumJsonValueWriter getJsonValueFunction(Class<?> type, Class<?> inspectionType) { |
| 146 |
for (Method method : inspectionType.getDeclaredMethods()) { |
| 147 |
JsonValue jsonValueAnnotation = method.getDeclaredAnnotation(JsonValue.class); |
| 148 |
if (!Modifier.isStatic(method.getModifiers()) && |
| 149 |
jsonValueAnnotation != null && |
| 150 |
method.getParameterCount() == 0 && |
| 151 |
method.getReturnType().equals(String.class)) { |
| 152 |
// this is the @JsonValue on the lowest descendent of any hierarchy |
| 153 |
// it may be put here to disable it |
| 154 |
if (jsonValueAnnotation.value()) { |
| 155 |
return new EnumJsonValueWriter(type, method); |
| 156 |
} |
| 157 |
|
| 158 |
// if @JsonValue is disabled, then we deliberately stop searching for another one |
| 159 |
return null; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
Class<?> superClass = inspectionType.getSuperclass(); |
| 164 |
if (superClass != null) { |
| 165 |
EnumJsonValueWriter writer = getJsonValueFunction(type, superClass); |
| 166 |
if (writer != null) { |
| 167 |
return writer; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
for (Class<?> parentInterface : inspectionType.getInterfaces()) { |
| 172 |
EnumJsonValueWriter writer = getJsonValueFunction(type, parentInterface); |
| 173 |
if (writer != null) { |
| 174 |
return writer; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return null; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Deserialize into an enum using the {@link JsonCreator} tagged method |
| 184 |
* |
| 185 |
* @since 2.14 |
| 186 |
*/ |
| 187 |
private static class EnumJsonCreatorReader extends ValueReader |
| 188 |
{ |
| 189 |
private final Method _jsonCreatorMethod; |
| 190 |
|
| 191 |
private EnumJsonCreatorReader(Class<?> valueType, Method jsonCreatorMethod) { |
| 192 |
super(valueType); |
| 193 |
this._jsonCreatorMethod = jsonCreatorMethod; |
| 194 |
jsonCreatorMethod.setAccessible(true); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public Object read(JSONReader reader, JsonParser p) throws IOException { |
| 199 |
try { |
| 200 |
return _jsonCreatorMethod.invoke(_valueType, p.getText()); |
| 201 |
} catch (Exception e) { |
| 202 |
throw new JSONObjectException("Cannot call JsonCreator method", e); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Scan the methods of the enum type to find a static method tagged with {@link JsonCreator} |
| 208 |
* which takes exactly one parameter and returns a value of the enum's type |
| 209 |
* @param type the type to scan |
| 210 |
* @return a new {@link EnumJsonCreatorReader} to deserialize with, or <code>null</code> if there |
| 211 |
* is no {@link JsonCreator} method to use |
| 212 |
*/ |
| 213 |
public static EnumJsonCreatorReader of(Class<?> type) { |
| 214 |
for (Method method : type.getDeclaredMethods()) { |
| 215 |
if (Modifier.isStatic(method.getModifiers()) && |
| 216 |
method.getDeclaredAnnotation(JsonCreator.class) != null && |
| 217 |
method.getParameterCount() == 1 && |
| 218 |
method.getReturnType().equals(type)) { |
| 219 |
return new EnumJsonCreatorReader(type, method); |
| 220 |
} |
| 221 |
} |
| 222 |
return null; |
| 223 |
} |
| 224 |
} |
| 225 |
} |