Last active
July 28, 2018 17:53
-
-
Save mageddo/ed880e856f36803b3e4dccf8bc03593d to your computer and use it in GitHub Desktop.
Include source JSON at the object
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 static class IncludeRawJsonDeserializer extends JsonDeserializer implements ContextualDeserializer { | |
| private JavaType type; | |
| private JsonDeserializer deserializer; | |
| private final TokenBufferDeserializer tokenBufferDeserializer; | |
| public IncludeRawJsonDeserializer() { | |
| tokenBufferDeserializer = new TokenBufferDeserializer(); | |
| } | |
| @Override | |
| public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { | |
| final TokenBuffer tokenBuffer = tokenBufferDeserializer.deserialize(jp, ctxt); | |
| final Object parsedObject = deserializer.deserialize(tokenBuffer.asParserOnFirstToken(), ctxt); | |
| if(parsedObject != null){ | |
| for (Field f : type.getRawClass().getDeclaredFields()) { | |
| if(f.getAnnotation(JsonRawValue.class) != null){ | |
| try { | |
| f.setAccessible(true); | |
| f.set(parsedObject, ctxt.readValue(tokenBuffer.asParserOnFirstToken(), JsonNode.class)); | |
| } catch (IllegalAccessException e) { | |
| throw new IOException(e); | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| return parsedObject; | |
| } | |
| @Override | |
| public JsonDeserializer createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException { | |
| this.type = property != null ? property.getType() : ctxt.getContextualType(); | |
| deserializer = BeanDeserializerFactory.instance.buildBeanDeserializer(ctxt, type, ctxt.getConfig().introspect(type)); | |
| // need to call this otherwise deserializing throws JsonMappingException | |
| ((ResolvableDeserializer) deserializer).resolve(ctxt); | |
| return this; | |
| } | |
| } | |
| @JsonIgnoreProperties(ignoreUnknown = true) | |
| @JsonDeserialize(using = IncludeRawJsonDeserializer.class) | |
| public static class Fruit { | |
| @JsonRawValue | |
| private Object json; | |
| private String fruitName; | |
| public Object getJson() { | |
| return json; | |
| } | |
| public void setJson(Object json) { | |
| this.json = json; | |
| } | |
| public Fruit(String fruitName) { | |
| this.fruitName = fruitName; | |
| } | |
| public Fruit() { | |
| } | |
| public String getFruitName() { | |
| return fruitName; | |
| } | |
| public void setFruitName(String fruitName) { | |
| this.fruitName = fruitName; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Fruit{" + | |
| "fruitName='" + fruitName + '\'' + | |
| "json='" + json + '\'' + | |
| '}'; | |
| } | |
| } | |
| public static class FruitWrapper { | |
| private Fruit fruit; | |
| public Fruit getFruit() { | |
| return fruit; | |
| } | |
| public void setFruit(Fruit fruit) { | |
| this.fruit = fruit; | |
| } | |
| @Override | |
| public String toString() { | |
| try { | |
| return new ObjectMapper().writeValueAsString(this); | |
| } catch (JsonProcessingException e) { | |
| return e.getMessage(); | |
| } | |
| } | |
| } | |
| public static void main(String[] args) throws IOException { | |
| final ObjectMapper mapper = new BackofficeConfig().objectMapper(null); | |
| System.out.println(mapper.readValue("{\"fruit\":{\"fruitName\": \"Orange\"}}", FruitWrapper.class)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment