Skip to content

Commit 4a3cff4

Browse files
committed
Java json 库示例
1 parent 59cd5ce commit 4a3cff4

7 files changed

Lines changed: 373 additions & 0 deletions

File tree

codes/javalib/json/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.github.dunwu.javalib</groupId>
7+
<artifactId>javalib-json</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
<name>JavaLib Demos - Json</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<java.version>1.8</java.version>
15+
<maven.compiler.source>${java.version}</maven.compiler.source>
16+
<maven.compiler.target>${java.version}</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.alibaba</groupId>
22+
<artifactId>fastjson</artifactId>
23+
<version>1.2.56</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.fasterxml.jackson.core</groupId>
27+
<artifactId>jackson-databind</artifactId>
28+
<version>2.9.8</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>4.12</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<resources>
40+
<resource>
41+
<filtering>true</filtering>
42+
<directory>src/main/resources</directory>
43+
</resource>
44+
</resources>
45+
</build>
46+
47+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.dunwu.javalib.json;
2+
3+
public class Person {
4+
private String name;
5+
private int age;
6+
7+
public Person() {}
8+
9+
public Person(String name, int age) {
10+
this.name = name;
11+
this.age = age;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public int getAge() {
23+
return age;
24+
}
25+
26+
public void setAge(int age) {
27+
this.age = age;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
33+
}
34+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package io.github.dunwu.javalib.json.fastjson;
2+
3+
import com.alibaba.fastjson.annotation.JSONField;
4+
5+
import java.util.Date;
6+
7+
/**
8+
* @author Zhang Peng
9+
* @date 2019-03-18
10+
*/
11+
public class FastjsonAnnotationBean {
12+
private int id;
13+
14+
// 配置date序列化和反序列使用yyyyMMdd日期格式
15+
@JSONField(format = "yyyy-MM-dd")
16+
private Date date1;
17+
18+
// 不序列化
19+
@JSONField(serialize = false)
20+
private Date date2;
21+
22+
// 不反序列化
23+
@JSONField(deserialize = false)
24+
private Date date3;
25+
26+
// 按ordinal排序
27+
@JSONField(ordinal = 2)
28+
private int f1;
29+
30+
@JSONField(ordinal = 1)
31+
private int f2;
32+
33+
public FastjsonAnnotationBean() { }
34+
35+
public FastjsonAnnotationBean(int id, Date date1, Date date2, Date date3, int f1, int f2) {
36+
this.id = id;
37+
this.date1 = date1;
38+
this.date2 = date2;
39+
this.date3 = date3;
40+
this.f1 = f1;
41+
this.f2 = f2;
42+
}
43+
44+
@JSONField(name = "ID")
45+
private int getId() {
46+
return id;
47+
}
48+
49+
private void setId(int id) {
50+
this.id = id;
51+
}
52+
53+
private Date getDate1() {
54+
return date1;
55+
}
56+
57+
private void setDate1(Date date1) {
58+
this.date1 = date1;
59+
}
60+
61+
private Date getDate2() {
62+
return date2;
63+
}
64+
65+
private void setDate2(Date date2) {
66+
this.date2 = date2;
67+
}
68+
69+
private Date getDate3() {
70+
return date3;
71+
}
72+
73+
private void setDate3(Date date3) {
74+
this.date3 = date3;
75+
}
76+
77+
private int getF1() {
78+
return f1;
79+
}
80+
81+
private void setF1(int f1) {
82+
this.f1 = f1;
83+
}
84+
85+
private int getF2() {
86+
return f2;
87+
}
88+
89+
private void setF2(int f2) {
90+
this.f2 = f2;
91+
}
92+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.dunwu.javalib.json.fastjson;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import io.github.dunwu.javalib.json.Person;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.Date;
9+
10+
/**
11+
* Fastjson 使用示例
12+
* @author Zhang Peng
13+
* @date 2019-03-18
14+
*/
15+
public class FastjsonTests {
16+
17+
/**
18+
* 序列化测试
19+
*/
20+
@Test
21+
public void serialize() {
22+
Person p = new Person("Tom", 20);
23+
String json = JSON.toJSONString(p);
24+
Assert.assertNotNull(json);
25+
System.out.println("json = [" + json + "]");
26+
}
27+
28+
/**
29+
* 反序列化测试
30+
*/
31+
@Test
32+
public void deserialize() {
33+
final String json = "{\"age\":20,\"name\":\"Tom\"}";
34+
Person p = JSON.parseObject(json, Person.class);
35+
Assert.assertNotNull(p);
36+
System.out.println("p = [" + p + "]");
37+
}
38+
39+
/**
40+
* 序列化测试
41+
*/
42+
@Test
43+
public void serializeAnnotation() {
44+
FastjsonAnnotationBean bean = new FastjsonAnnotationBean(1, new Date(), new Date(), new Date(), 10, 20);
45+
String json = JSON.toJSONString(bean);
46+
Assert.assertNotNull(json);
47+
System.out.println("json = [" + json + "]");
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.dunwu.javalib.json.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
6+
7+
/**
8+
* @author Zhang Peng
9+
* @date 2019-03-18
10+
*/
11+
@JsonPropertyOrder(alphabetic = true)
12+
public class JacksonAnnotationBean {
13+
private String Name;
14+
private int Age;
15+
@JsonIgnore
16+
private String Sex;
17+
18+
public JacksonAnnotationBean() { }
19+
20+
public JacksonAnnotationBean(String name, int age, String sex) {
21+
Name = name;
22+
Age = age;
23+
Sex = sex;
24+
}
25+
26+
@JsonProperty("username")
27+
public String getName() {
28+
return Name;
29+
}
30+
31+
public void setName(String name) {
32+
Name = name;
33+
}
34+
35+
public int getAge() {
36+
return Age;
37+
}
38+
39+
public void setAge(int age) {
40+
Age = age;
41+
}
42+
43+
public String getSex() {
44+
return Sex;
45+
}
46+
47+
public void setSex(String sex) {
48+
Sex = sex;
49+
}
50+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.github.dunwu.javalib.json.jackson;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import io.github.dunwu.javalib.json.Person;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.util.HashMap;
11+
import java.util.LinkedList;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
/**
16+
* Jackson 使用示例
17+
* @author Zhang Peng
18+
* @date 2019-03-18
19+
*/
20+
public class JacksonTests {
21+
22+
final ObjectMapper mapper = new ObjectMapper();
23+
24+
/**
25+
* 序列化测试
26+
*/
27+
@Test
28+
public void serialize() {
29+
Person p = new Person("Tom", 20);
30+
String json = null;
31+
try {
32+
json = mapper.writeValueAsString(p);
33+
} catch (JsonProcessingException e) {
34+
e.printStackTrace();
35+
}
36+
Assert.assertNotNull(json);
37+
System.out.println("json = [" + json + "]");
38+
}
39+
40+
/**
41+
* 反序列化测试
42+
*/
43+
@Test
44+
public void deserialize() {
45+
final String json = "{\"age\":20,\"name\":\"Tom\"}";
46+
Person p = null;
47+
try {
48+
p = mapper.readValue(json, Person.class);
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
}
52+
Assert.assertNotNull(p);
53+
System.out.println("p = [" + p + "]");
54+
}
55+
56+
/**
57+
* 序列化测试
58+
*/
59+
@Test
60+
public void serialize2() {
61+
Person p = new Person("Tom", 20);
62+
Person p2 = new Person("Jack", 22);
63+
Person p3 = new Person("Mary", 18);
64+
65+
List<Person> persons = new LinkedList<>();
66+
persons.add(p);
67+
persons.add(p2);
68+
persons.add(p3);
69+
70+
Map<String, List> map = new HashMap<>();
71+
map.put("persons", persons);
72+
73+
String json = null;
74+
try {
75+
json = mapper.writeValueAsString(map);
76+
} catch (JsonProcessingException e) {
77+
e.printStackTrace();
78+
}
79+
80+
Assert.assertNotNull(json);
81+
System.out.println("json = [" + json + "]");
82+
}
83+
84+
/**
85+
* 序列化测试
86+
*/
87+
@Test
88+
public void serialize3() {
89+
JacksonAnnotationBean jacksonAnnotationBean = new JacksonAnnotationBean("jack", 19, "男");
90+
String json = null;
91+
try {
92+
json = mapper.writeValueAsString(jacksonAnnotationBean);
93+
} catch (JsonProcessingException e) {
94+
e.printStackTrace();
95+
}
96+
Assert.assertNotNull(json);
97+
System.out.println("json = [" + json + "]");
98+
}
99+
100+
}

codes/javalib/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<modules>
1414
<module>bean</module>
15+
<module>json</module>
1516
<module>log</module>
1617
<module>mvel</module>
1718
<module>test</module>

0 commit comments

Comments
 (0)