Skip to content

Commit 7b1415c

Browse files
committed
feat: 增加 Jackson 相关操作(https://www.wdbyte.com/tool/jackson.html)
1 parent 63a6b60 commit 7b1415c

13 files changed

Lines changed: 479 additions & 0 deletions

File tree

tool-java-jackson/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## tool-java-jackson
2+
3+
### 相关文章
4+
- [Jackson 解析 JSON 教程](https://www.wdbyte.com/tool/jackson.html)

tool-java-jackson/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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>parent-modules</artifactId>
7+
<groupId>com.wdbyte</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>com.wdbyte.jackson</groupId>
13+
<artifactId>tool-java-jackson</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>8</maven.compiler.source>
17+
<maven.compiler.target>8</maven.compiler.target>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.fasterxml.jackson.core</groupId>
23+
<artifactId>jackson-databind</artifactId>
24+
<version>2.13.3</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.fasterxml.jackson.datatype</groupId>
29+
<artifactId>jackson-datatype-jsr310</artifactId>
30+
<version>2.13.3</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.junit.jupiter</groupId>
35+
<artifactId>junit-jupiter</artifactId>
36+
<version>5.8.2</version>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"name": "aLang",
4+
"age": 27,
5+
"skillList": [
6+
"java",
7+
"c++"
8+
]
9+
},
10+
{
11+
"name": "darcy",
12+
"age": 26,
13+
"skillList": [
14+
"go",
15+
"rust"
16+
]
17+
}
18+
]

tool-java-jackson/src/Person.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "aLang",
3+
"age": 27,
4+
"skillList": [
5+
"java",
6+
"c++"
7+
]
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wdbyte.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonGetter;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
5+
import com.fasterxml.jackson.annotation.JsonSetter;
6+
import lombok.Data;
7+
8+
/**
9+
* @author https://www.wdbyte.com
10+
* @date 2022/07/17
11+
*/
12+
@Data
13+
public class Cat {
14+
15+
@JsonSetter(value = "catName")
16+
private String name;
17+
18+
private Integer age;
19+
20+
@JsonGetter(value = "catName")
21+
public String getName() {
22+
return name;
23+
}
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.wdbyte.jackson;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.Date;
5+
6+
import com.fasterxml.jackson.annotation.JsonFormat;
7+
import com.fasterxml.jackson.annotation.JsonGetter;
8+
import com.fasterxml.jackson.annotation.JsonSetter;
9+
import lombok.AllArgsConstructor;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
import lombok.ToString;
13+
14+
/**
15+
* @author https://www.wdbyte.com
16+
* @date 2022/07/17
17+
*/
18+
//@Data
19+
@AllArgsConstructor
20+
@NoArgsConstructor
21+
@ToString
22+
public class Order {
23+
24+
@JsonSetter(value = "orderId")
25+
private Integer id;
26+
27+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
28+
private Date createTime;
29+
30+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
31+
private LocalDateTime updateTime;
32+
33+
//@JsonGetter(value = "orderId")
34+
public Integer getId() {
35+
return id;
36+
}
37+
38+
public void setId(Integer id) {
39+
this.id = id;
40+
}
41+
42+
public Date getCreateTime() {
43+
return createTime;
44+
}
45+
46+
public void setCreateTime(Date createTime) {
47+
this.createTime = createTime;
48+
}
49+
50+
public LocalDateTime getUpdateTime() {
51+
return updateTime;
52+
}
53+
54+
public void setUpdateTime(LocalDateTime updateTime) {
55+
this.updateTime = updateTime;
56+
}
57+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.wdbyte.jackson;
2+
3+
import java.util.List;
4+
5+
import lombok.Data;
6+
7+
/**
8+
* @author https://www.wdbyte.com
9+
* @date 2022/07/16
10+
*/
11+
@Data
12+
public class Person {
13+
private String name;
14+
private Integer age;
15+
private List<String> skillList;
16+
}
17+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.wdbyte.jackson;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7+
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.google.common.collect.Maps;
9+
import lombok.AllArgsConstructor;
10+
import lombok.Data;
11+
import lombok.Getter;
12+
import lombok.NoArgsConstructor;
13+
import lombok.Setter;
14+
import lombok.ToString;
15+
16+
/**
17+
* @author https://www.wdbyte.com
18+
* @date 2022/07/17
19+
*/
20+
@ToString
21+
@AllArgsConstructor
22+
@NoArgsConstructor
23+
public class Student {
24+
@Getter
25+
@Setter
26+
private String name;
27+
28+
@Getter
29+
@Setter
30+
private Integer age;
31+
32+
@Getter
33+
@Setter
34+
private Map<String, Object> diyMap = new HashMap<>();
35+
36+
@JsonAnyGetter
37+
private Map<String, Object> initMap = new HashMap() {{
38+
put("a", 111);
39+
put("b", 222);
40+
put("c", 333);
41+
}};
42+
43+
@JsonAnySetter
44+
public void otherField(String key, String value) {
45+
this.diyMap.put(key, value);
46+
}
47+
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.wdbyte.jackson;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* @author https://www.wdbyte.com
10+
* @date 2022/07/17
11+
*/
12+
class CatTest {
13+
14+
ObjectMapper objectMapper = new ObjectMapper();
15+
16+
@Test
17+
void testPojoToJson() throws JsonProcessingException {
18+
Cat cat = new Cat();
19+
cat.setName("Tom");
20+
cat.setAge(2);
21+
String json = objectMapper.writeValueAsString(cat);
22+
System.out.println(json);
23+
24+
Assertions.assertEquals(json, "{\"name\":\"Tom\"}");
25+
26+
cat = objectMapper.readValue(json, Cat.class);
27+
Assertions.assertEquals(cat.getName(), "Tom");
28+
Assertions.assertEquals(cat.getAge(), null);
29+
}
30+
31+
@Test
32+
void testJsonToPojo() throws JsonProcessingException {
33+
String json = "{\"name\":\"tom\"}";
34+
Cat cat = objectMapper.readValue(json, Cat.class);
35+
Assertions.assertEquals(cat.getName(), "Tom");
36+
Assertions.assertEquals(cat.getAge(), null);
37+
}
38+
39+
@Test
40+
void testPojoToJson2() throws JsonProcessingException {
41+
String json = "{\"age\":2,\"catName\":\"Tom\"}";
42+
Cat cat = objectMapper.readValue(json, Cat.class);
43+
System.out.println(cat.toString());
44+
Assertions.assertEquals(cat.getName(), "Tom");
45+
}
46+
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.wdbyte.jackson;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.Date;
5+
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
/**
12+
* @author https://www.wdbyte.com
13+
* @date 2022/07/17
14+
*/
15+
class OrderTest {
16+
17+
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
18+
//ObjectMapper objectMapper = new ObjectMapper();
19+
20+
@Test
21+
void testPojoToJson() throws JsonProcessingException {
22+
Order order = new Order(1, new Date(), LocalDateTime.now());
23+
String json = objectMapper.writeValueAsString(order);
24+
System.out.println(json);
25+
26+
order = objectMapper.readValue(json, Order.class);
27+
System.out.println(order.toString());
28+
29+
Assertions.assertEquals(order.getId(), 1);
30+
}
31+
32+
@Test
33+
void testPojoToJson0() throws JsonProcessingException {
34+
Order order = new Order(1, new Date(), null);
35+
String json = objectMapper.writeValueAsString(order);
36+
System.out.println(json);
37+
38+
order = objectMapper.readValue(json, Order.class);
39+
System.out.println( order.toString());
40+
41+
Assertions.assertEquals(order.getId(), 1);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)