1
1
/*
2
- * Copyright 2013-2023 the original author or authors.
2
+ * Copyright 2013-2024 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.
24
24
import com .fasterxml .jackson .databind .JsonNode ;
25
25
import com .fasterxml .jackson .databind .ObjectMapper ;
26
26
import com .fasterxml .jackson .databind .node .ArrayNode ;
27
+ import com .fasterxml .jackson .databind .node .NullNode ;
27
28
28
29
import org .springframework .expression .AccessException ;
29
30
import org .springframework .expression .EvaluationContext ;
35
36
36
37
/**
37
38
* A SpEL {@link PropertyAccessor} that knows how to read properties from JSON objects.
38
- * Uses Jackson {@link JsonNode} API for nested properties access.
39
+ * <p> Uses Jackson {@link JsonNode} API for nested properties access.
39
40
*
40
41
* @author Eric Bottard
41
42
* @author Artem Bilan
42
43
* @author Paul Martin
43
44
* @author Gary Russell
44
45
* @author Pierre Lakreb
45
46
* @author Vladislav Fefelov
47
+ * @author Sam Brannen
46
48
*
47
49
* @since 3.0
50
+ * @see JsonIndexAccessor
48
51
*/
49
52
public class JsonPropertyAccessor implements PropertyAccessor {
50
53
@@ -80,23 +83,22 @@ public boolean canRead(EvaluationContext context, Object target, String name) th
80
83
// Cannot parse - treat as not a JSON
81
84
return false ;
82
85
}
83
- Integer index = maybeIndex (name );
84
86
if (node instanceof ArrayNode ) {
85
- return index != null ;
87
+ return maybeIndex ( name ) != null ;
86
88
}
87
89
return true ;
88
90
}
89
91
90
92
private JsonNode asJson (Object target ) throws AccessException {
91
- if (target instanceof JsonNode ) {
92
- return ( JsonNode ) target ;
93
+ if (target instanceof JsonNode jsonNode ) {
94
+ return jsonNode ;
93
95
}
94
- else if (target instanceof JsonNodeWrapper ) {
95
- return (( JsonNodeWrapper <?>) target ) .getRealNode ();
96
+ else if (target instanceof JsonNodeWrapper <?> jsonNodeWrapper ) {
97
+ return jsonNodeWrapper .getRealNode ();
96
98
}
97
- else if (target instanceof String ) {
99
+ else if (target instanceof String content ) {
98
100
try {
99
- return this .objectMapper .readTree (( String ) target );
101
+ return this .objectMapper .readTree (content );
100
102
}
101
103
catch (JsonProcessingException e ) {
102
104
throw new AccessException ("Exception while trying to deserialize String" , e );
@@ -160,8 +162,8 @@ private static boolean isNumeric(String str) {
160
162
return true ;
161
163
}
162
164
163
- private static TypedValue typedValue (JsonNode json ) throws AccessException {
164
- if (json == null ) {
165
+ static TypedValue typedValue (JsonNode json ) throws AccessException {
166
+ if (json == null || json instanceof NullNode ) {
165
167
return TypedValue .NULL ;
166
168
}
167
169
else if (json .isValueNode ()) {
@@ -199,8 +201,8 @@ public static Object wrap(JsonNode json) throws AccessException {
199
201
if (json == null ) {
200
202
return null ;
201
203
}
202
- else if (json instanceof ArrayNode ) {
203
- return new ArrayNodeAsList (( ArrayNode ) json );
204
+ else if (json instanceof ArrayNode arrayNode ) {
205
+ return new ArrayNodeAsList (arrayNode );
204
206
}
205
207
else if (json .isValueNode ()) {
206
208
return getValue (json );
@@ -212,8 +214,6 @@ else if (json.isValueNode()) {
212
214
213
215
interface JsonNodeWrapper <T > extends Comparable <T > {
214
216
215
- String toString ();
216
-
217
217
JsonNode getRealNode ();
218
218
219
219
}
@@ -309,10 +309,8 @@ public Object next() {
309
309
310
310
@ Override
311
311
public int compareTo (Object o ) {
312
- if (o instanceof JsonNodeWrapper <?>) {
313
- return this .delegate .equals (((JsonNodeWrapper <?>) o ).getRealNode ()) ? 0 : 1 ;
314
- }
315
- return this .delegate .equals (o ) ? 0 : 1 ;
312
+ Object that = (o instanceof JsonNodeWrapper <?> wrapper ? wrapper .getRealNode () : o );
313
+ return this .delegate .equals (that ) ? 0 : 1 ;
316
314
}
317
315
318
316
}
0 commit comments