Created
August 24, 2015 22:26
-
-
Save christianbauer/dd14282d8208c163b4bc to your computer and use it in GitHub Desktop.
Deserialization support for @JsonRawValue in Jackson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JacksonRawValueModule extends SimpleModule { | |
class RawValueDeserializer extends JsonDeserializer<String> implements ContextualDeserializer { | |
@Override | |
public String deserialize(JsonParser jp, DeserializationContext ctx) throws IOException { | |
TreeNode tree = jp.getCodec().readTree(jp); | |
return tree.toString(); | |
} | |
@Override | |
public JsonDeserializer<?> createContextual(DeserializationContext ctx, | |
BeanProperty property) throws JsonMappingException { | |
if (property != null && property.getMember().getAnnotation(JsonRawValue.class) != null) { | |
return this; | |
} | |
return new StringDeserializer(); | |
} | |
} | |
public JacksonRawValueModule() { | |
addDeserializer(String.class, new RawValueDeserializer()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment