1
1
/*
2
- * Copyright 2013-2017 the original author or authors.
2
+ * Copyright 2013-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
import com .fasterxml .jackson .databind .node .ObjectNode ;
35
35
36
36
/**
37
- * A SpEL {@link PropertyAccessor} that knows how to read on Jackson JSON objects.
37
+ * A SpEL {@link PropertyAccessor} that knows how to read properties from JSON objects.
38
+ * Uses Jackson {@link JsonNode} API for nested properties access.
38
39
*
39
40
* @author Eric Bottard
40
41
* @author Artem Bilan
41
42
* @author Paul Martin
43
+ *
42
44
* @since 3.0
43
45
*/
44
46
public class JsonPropertyAccessor implements PropertyAccessor {
45
47
46
48
/**
47
49
* The kind of types this can work with.
48
50
*/
49
- private static final Class <?>[] SUPPORTED_CLASSES = new Class <?>[] { String .class , ToStringFriendlyJsonNode .class ,
50
- ArrayNodeAsList .class , ObjectNode .class , ArrayNode .class };
51
+ private static final Class <?>[] SUPPORTED_CLASSES =
52
+ new Class <?>[] {
53
+ String .class ,
54
+ ToStringFriendlyJsonNode .class ,
55
+ ArrayNodeAsList .class ,
56
+ ObjectNode .class ,
57
+ ArrayNode .class
58
+ };
51
59
52
60
// Note: ObjectMapper is thread-safe
53
61
private ObjectMapper objectMapper = new ObjectMapper ();
54
62
63
+ public void setObjectMapper (ObjectMapper objectMapper ) {
64
+ Assert .notNull (objectMapper , "'objectMapper' cannot be null" );
65
+ this .objectMapper = objectMapper ;
66
+ }
67
+
55
68
@ Override
56
69
public Class <?>[] getSpecificTargetClasses () {
57
70
return SUPPORTED_CLASSES ;
@@ -133,20 +146,15 @@ public TypedValue read(EvaluationContext context, Object target, String name) th
133
146
}
134
147
135
148
@ Override
136
- public boolean canWrite (EvaluationContext context , Object target , String name ) throws AccessException {
149
+ public boolean canWrite (EvaluationContext context , Object target , String name ) {
137
150
return false ;
138
151
}
139
152
140
153
@ Override
141
- public void write (EvaluationContext context , Object target , String name , Object newValue ) throws AccessException {
154
+ public void write (EvaluationContext context , Object target , String name , Object newValue ) {
142
155
throw new UnsupportedOperationException ("Write is not supported" );
143
156
}
144
157
145
- public void setObjectMapper (ObjectMapper objectMapper ) {
146
- Assert .notNull (objectMapper , "'objectMapper' cannot be null" );
147
- this .objectMapper = objectMapper ;
148
- }
149
-
150
158
private static TypedValue typedValue (JsonNode json ) {
151
159
if (json == null ) {
152
160
return TypedValue .NULL ;
@@ -172,21 +180,32 @@ else if (json instanceof ArrayNode) {
172
180
* The base interface for wrapped {@link JsonNode}.
173
181
* @since 5.0
174
182
*/
175
- public interface WrappedJsonNode {
183
+ public interface WrappedJsonNode <T extends JsonNode > {
184
+
185
+ /**
186
+ * Return the wrapped {@link JsonNode}
187
+ * @return the wrapped JsonNode
188
+ */
189
+ T getTarget ();
176
190
177
191
}
178
192
179
193
/**
180
194
* A {@link WrappedJsonNode} implementation to represent {@link JsonNode} as string.
181
195
*/
182
- public static class ToStringFriendlyJsonNode implements WrappedJsonNode {
196
+ public static class ToStringFriendlyJsonNode implements WrappedJsonNode < JsonNode > {
183
197
184
198
private final JsonNode node ;
185
199
186
200
ToStringFriendlyJsonNode (JsonNode node ) {
187
201
this .node = node ;
188
202
}
189
203
204
+ @ Override
205
+ public JsonNode getTarget () {
206
+ return this .node ;
207
+ }
208
+
190
209
@ Override
191
210
public String toString () {
192
211
if (this .node == null ) {
@@ -224,14 +243,19 @@ public int hashCode() {
224
243
* An {@link AbstractList} implementation around {@link ArrayNode} with {@link WrappedJsonNode} aspect.
225
244
* @since 5.0
226
245
*/
227
- public static class ArrayNodeAsList extends AbstractList <WrappedJsonNode > implements WrappedJsonNode {
246
+ public static class ArrayNodeAsList extends AbstractList <WrappedJsonNode > implements WrappedJsonNode < ArrayNode > {
228
247
229
248
private final ArrayNode node ;
230
249
231
250
ArrayNodeAsList (ArrayNode node ) {
232
251
this .node = node ;
233
252
}
234
253
254
+ @ Override
255
+ public ArrayNode getTarget () {
256
+ return this .node ;
257
+ }
258
+
235
259
@ Override
236
260
public WrappedJsonNode get (int index ) {
237
261
return wrap (this .node .get (index ));
0 commit comments