-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestJsonAccessing.java
98 lines (70 loc) · 2.74 KB
/
TestJsonAccessing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.example.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.json.JsonPropertyAccessor;
/**
* @author pilak
*
*/
public class TestJsonAccessing {
private static final String COMMON_SPEL_EXPRESSION = "property.^[name == 'value1'].name";
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
StandardEvaluationContext context = new StandardEvaluationContext();
SpelExpressionParser parser = new SpelExpressionParser();
Example example = new Example();
JavaBean javaBean1 = new JavaBean();
javaBean1.setName("value1");
JavaBean javaBean2 = new JavaBean();
javaBean2.setName("value2");
example.setProperty(new JavaBean[] { javaBean1, javaBean2 });
context.setPropertyAccessors(List.of(new ReflectivePropertyAccessor()));
context.setRootObject(example);
System.out.println("Example with JavaBean");
System.out.println(parser.parseExpression(COMMON_SPEL_EXPRESSION).getValue(context));
List<Map<String, Object>> property = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
HashMap<String, Object> value1 = new HashMap<>();
HashMap<String, Object> value2 = new HashMap<>();
map.put("property", property);
property.add(value1);
property.add(value2);
value1.put("name", "value1");
value2.put("name", "value2");
context.setPropertyAccessors(List.of(new MapAccessor()));
context.setRootObject(map);
System.out.println("Example with Map");
System.out.println(parser.parseExpression(COMMON_SPEL_EXPRESSION).getValue(context));
context.setPropertyAccessors(List.of(new JsonPropertyAccessor()));
context.setRootObject(
new ObjectMapper().readTree("{\"property\":[{\"name\":\"value1\"},{\"name\":\"value2\"}]}"));
System.out.println("Example with JsonNode");
System.out.println(parser.parseExpression(COMMON_SPEL_EXPRESSION).getValue(context));
}
static class Example {
private JavaBean[] property;
public JavaBean[] getProperty() {
return property;
}
public void setProperty(JavaBean[] property) {
this.property = property;
}
}
static class JavaBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}