Skip to content

Instantly share code, notes, and snippets.

@christianbauer
Created August 24, 2015 22:26
Show Gist options
  • Save christianbauer/dd14282d8208c163b4bc to your computer and use it in GitHub Desktop.
Save christianbauer/dd14282d8208c163b4bc to your computer and use it in GitHub Desktop.
Deserialization support for @JsonRawValue in Jackson
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