Skip to content

Commit 5a82b61

Browse files
committed
add jackson
1 parent 487d155 commit 5a82b61

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.iml
2+
.idea
3+
target

jackson/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>tutorial-java</artifactId>
7+
<groupId>biz.chenxu</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>jackson</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.fasterxml.jackson.core</groupId>
17+
<artifactId>jackson-core</artifactId>
18+
<version>2.9.10</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.fasterxml.jackson.core</groupId>
22+
<artifactId>jackson-databind</artifactId>
23+
<version>2.9.10</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.projectlombok</groupId>
27+
<artifactId>lombok</artifactId>
28+
<version>1.18.4</version>
29+
<optional>true</optional>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package biz.chenxu.tutorial;
2+
3+
import lombok.Data;
4+
import lombok.ToString;
5+
6+
import java.util.Date;
7+
import java.util.List;
8+
9+
@Data
10+
@ToString
11+
public class ComplexType {
12+
13+
private String stringField;
14+
15+
private Date dateField;
16+
17+
private Integer intField;
18+
19+
private Double doubleField;
20+
21+
private Float floatField;
22+
23+
private List<ComplexType> complexTypeList;
24+
25+
private ComplexType[] complexArray;
26+
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package biz.chenxu.tutorial;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
import java.io.IOException;
9+
import java.util.List;
10+
11+
public class JacksonApplication {
12+
13+
public static void main(String[] args) throws IOException {
14+
ObjectMapper objectMapper = new ObjectMapper();
15+
16+
String json = "{\n" +
17+
" \"stringField\": \"demoData\",\n" +
18+
" \"dateField\": \"2020-02-23\",\n" +
19+
" \"intField\": 1,\n" +
20+
" \"doubleField\": 1.0,\n" +
21+
" \"floatField\": 1.0,\n" +
22+
" \"complexTypeList\": [\n" +
23+
" {\n" +
24+
" \"stringField\": \"innerDemoData\",\n" +
25+
" \"dateField\": \"2020-02-23\",\n" +
26+
" \"intField\": 1,\n" +
27+
" \"doubleField\": 1.0,\n" +
28+
" \"floatField\": 1.0\n" +
29+
" }\n" +
30+
" ]\n" +
31+
"}";
32+
33+
JsonNode rootNode = objectMapper.readTree(json);
34+
JsonNode listNode = rootNode.get("complexTypeList");
35+
JsonParser token = objectMapper.treeAsTokens(listNode);
36+
List<ComplexType> list = objectMapper.readValue(token, new TypeReference<List<ComplexType>>(){});
37+
for (ComplexType complexType : list) {
38+
System.out.println(complexType);
39+
}
40+
}
41+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>fast-json</module>
1515
<module>guava</module>
1616
<module>mysql</module>
17+
<module>jackson</module>
1718
</modules>
1819

1920
<properties>

0 commit comments

Comments
 (0)