From c4c4e51627c79e2878e75677f4c3e4bb24ea513a Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 11 Jan 2018 14:25:06 -0500 Subject: [PATCH] Some JsonPropertyAccessor polishing * Add `WrappedJsonNode.getTarget()` API to let target users to get access to the target `JsonNode` for their logic * Some code style and JavaDocs improvements in the `JsonPropertyAccessor` --- .../json/JsonPropertyAccessor.java | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPropertyAccessor.java b/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPropertyAccessor.java index d7c9fb5fc5..b612005409 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPropertyAccessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPropertyAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2017 the original author or authors. + * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,11 +34,13 @@ import com.fasterxml.jackson.databind.node.ContainerNode; import com.fasterxml.jackson.databind.node.ObjectNode; /** - * A SpEL {@link PropertyAccessor} that knows how to read on Jackson JSON objects. + * A SpEL {@link PropertyAccessor} that knows how to read properties from JSON objects. + * Uses Jackson {@link JsonNode} API for nested properties access. * * @author Eric Bottard * @author Artem Bilan * @author Paul Martin + * * @since 3.0 */ public class JsonPropertyAccessor implements PropertyAccessor { @@ -46,12 +48,23 @@ public class JsonPropertyAccessor implements PropertyAccessor { /** * The kind of types this can work with. */ - private static final Class[] SUPPORTED_CLASSES = new Class[] { String.class, ToStringFriendlyJsonNode.class, - ArrayNodeAsList.class, ObjectNode.class, ArrayNode.class }; + private static final Class[] SUPPORTED_CLASSES = + new Class[] { + String.class, + ToStringFriendlyJsonNode.class, + ArrayNodeAsList.class, + ObjectNode.class, + ArrayNode.class + }; // Note: ObjectMapper is thread-safe private ObjectMapper objectMapper = new ObjectMapper(); + public void setObjectMapper(ObjectMapper objectMapper) { + Assert.notNull(objectMapper, "'objectMapper' cannot be null"); + this.objectMapper = objectMapper; + } + @Override public Class[] getSpecificTargetClasses() { return SUPPORTED_CLASSES; @@ -133,20 +146,15 @@ public class JsonPropertyAccessor implements PropertyAccessor { } @Override - public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { + public boolean canWrite(EvaluationContext context, Object target, String name) { return false; } @Override - public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { + public void write(EvaluationContext context, Object target, String name, Object newValue) { throw new UnsupportedOperationException("Write is not supported"); } - public void setObjectMapper(ObjectMapper objectMapper) { - Assert.notNull(objectMapper, "'objectMapper' cannot be null"); - this.objectMapper = objectMapper; - } - private static TypedValue typedValue(JsonNode json) { if (json == null) { return TypedValue.NULL; @@ -172,14 +180,20 @@ public class JsonPropertyAccessor implements PropertyAccessor { * The base interface for wrapped {@link JsonNode}. * @since 5.0 */ - public interface WrappedJsonNode { + public interface WrappedJsonNode { + + /** + * Return the wrapped {@link JsonNode} + * @return the wrapped JsonNode + */ + T getTarget(); } /** * A {@link WrappedJsonNode} implementation to represent {@link JsonNode} as string. */ - public static class ToStringFriendlyJsonNode implements WrappedJsonNode { + public static class ToStringFriendlyJsonNode implements WrappedJsonNode { private final JsonNode node; @@ -187,6 +201,11 @@ public class JsonPropertyAccessor implements PropertyAccessor { this.node = node; } + @Override + public JsonNode getTarget() { + return this.node; + } + @Override public String toString() { if (this.node == null) { @@ -224,7 +243,7 @@ public class JsonPropertyAccessor implements PropertyAccessor { * An {@link AbstractList} implementation around {@link ArrayNode} with {@link WrappedJsonNode} aspect. * @since 5.0 */ - public static class ArrayNodeAsList extends AbstractList implements WrappedJsonNode { + public static class ArrayNodeAsList extends AbstractList implements WrappedJsonNode { private final ArrayNode node; @@ -232,6 +251,11 @@ public class JsonPropertyAccessor implements PropertyAccessor { this.node = node; } + @Override + public ArrayNode getTarget() { + return this.node; + } + @Override public WrappedJsonNode get(int index) { return wrap(this.node.get(index));