Skip to content

Commit d63e5a5

Browse files
committed
更新 json lib 示例
1 parent e57ae00 commit d63e5a5

File tree

8 files changed

+254
-2
lines changed

8 files changed

+254
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.dunwu.javalib.json.bean;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Group {
7+
8+
private Long id;
9+
10+
private String name;
11+
12+
private List<User> users = new ArrayList<User>();
13+
14+
public Long getId() {
15+
return id;
16+
}
17+
18+
public void setId(Long id) {
19+
this.id = id;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
public List<User> getUsers() {
31+
return users;
32+
}
33+
34+
public void setUsers(List<User> users) {
35+
this.users = users;
36+
}
37+
38+
public void addUser(User user) {
39+
users.add(user);
40+
}
41+
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.dunwu.javalib.json.bean;
2+
3+
public class User {
4+
5+
private Long id;
6+
7+
private String name;
8+
9+
public Long getId() {
10+
return id;
11+
}
12+
13+
public void setId(Long id) {
14+
this.id = id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.dunwu.javalib.json.gson;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
9+
* @since 2019-11-24
10+
*/
11+
public class GsonAnnotationBean {
12+
13+
@SerializedName("custom_naming")
14+
private String someField;
15+
16+
private String someOtherField;
17+
18+
@Override
19+
public boolean equals(Object o) {
20+
if (this == o) {
21+
return true;
22+
}
23+
if (!(o instanceof GsonAnnotationBean)) {
24+
return false;
25+
}
26+
GsonAnnotationBean that = (GsonAnnotationBean) o;
27+
return Objects.equals(someField, that.someField) &&
28+
Objects.equals(someOtherField, that.someOtherField);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return Objects.hash(someField, someOtherField);
34+
}
35+
36+
public String getSomeField() {
37+
return someField;
38+
}
39+
40+
public void setSomeField(String someField) {
41+
this.someField = someField;
42+
}
43+
44+
public String getSomeOtherField() {
45+
return someOtherField;
46+
}
47+
48+
public void setSomeOtherField(String someOtherField) {
49+
this.someOtherField = someOtherField;
50+
}
51+
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.dunwu.javalib.json.gson;
2+
3+
import com.google.gson.annotations.Since;
4+
5+
public class VersionedClass {
6+
7+
@Since(1.1)
8+
private final String newerField;
9+
10+
@Since(1.0)
11+
private final String newField;
12+
13+
private final String field;
14+
15+
public VersionedClass() {
16+
this.newerField = "newer";
17+
this.newField = "new";
18+
this.field = "old";
19+
}
20+
21+
}

javalib-io-json/src/test/java/io/github/dunwu/javalib/json/fastjson/FastjsonCaseTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.dunwu.javalib.json.fastjson;
22

33
import com.alibaba.fastjson.JSON;
4+
import io.github.dunwu.javalib.json.bean.Group;
5+
import io.github.dunwu.javalib.json.util.BeanUtils;
46
import io.github.dunwu.util.time.DateExtUtils;
57
import org.junit.Test;
68

@@ -16,6 +18,15 @@
1618
*/
1719
public class FastjsonCaseTests {
1820

21+
@Test
22+
public void test() {
23+
Group oldGroup = BeanUtils.initGroupBean();
24+
String jsonString = JSON.toJSONString(oldGroup);
25+
System.out.println(jsonString);
26+
Group newGroup = JSON.parseObject(jsonString, Group.class);
27+
assertThat(newGroup).isNotNull();
28+
}
29+
1930
/**
2031
* 序列化测试
2132
*/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.github.dunwu.javalib.json.gson;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import org.junit.Test;
6+
7+
import java.lang.reflect.Modifier;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
/**
12+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
13+
* @since 2019-11-24
14+
*/
15+
public class GsonCaseTests {
16+
17+
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
18+
19+
private Gson gson2 = new GsonBuilder()
20+
.setVersion(1.0)
21+
.setPrettyPrinting()
22+
.setDateFormat("yyyy-MM-dd HH:mm:ss")
23+
.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE)
24+
.create();
25+
26+
@Test
27+
public void test() {
28+
// Serialization
29+
Gson gson = new Gson();
30+
gson.toJson(1); // ==> 1
31+
gson.toJson("abcd"); // ==> "abcd"
32+
gson.toJson(10L); // ==> 10
33+
int[] values = { 1 };
34+
gson.toJson(values); // ==> [1]
35+
36+
// Deserialization
37+
int i1 = gson.fromJson("1", int.class);
38+
Integer i2 = gson.fromJson("1", Integer.class);
39+
Long l1 = gson.fromJson("1", Long.class);
40+
Boolean b1 = gson.fromJson("false", Boolean.class);
41+
String str = gson.fromJson("\"abc\"", String.class);
42+
String[] anotherStr = gson.fromJson("[\"abc\"]", String[].class);
43+
44+
assertThat(i1).isEqualTo(1);
45+
assertThat(i2).isEqualTo(1);
46+
assertThat(l1).isEqualTo(1L);
47+
assertThat(b1).isFalse();
48+
assertThat(str).isEqualTo("abc");
49+
}
50+
51+
@Test
52+
public void testAnnotation() {
53+
GsonAnnotationBean oldBean = new GsonAnnotationBean();
54+
oldBean.setSomeField("hello");
55+
oldBean.setSomeOtherField("world");
56+
57+
String expectStr = "{\"custom_naming\":\"hello\",\"someOtherField\":\"world\"}";
58+
String json = gson.toJson(oldBean);
59+
assertThat(json).isEqualTo(expectStr);
60+
61+
GsonAnnotationBean newBean = gson.fromJson(expectStr, GsonAnnotationBean.class);
62+
assertThat(newBean).isEqualTo(oldBean);
63+
}
64+
65+
@Test
66+
public void testVersionedClass() {
67+
VersionedClass versionedObject = new VersionedClass();
68+
String jsonOutput = gson2.toJson(versionedObject);
69+
System.out.println(jsonOutput);
70+
assertThat(jsonOutput).isEqualTo("{\n"
71+
+ " \"newField\": \"new\",\n"
72+
+ " \"field\": \"old\"\n"
73+
+ "}");
74+
75+
jsonOutput = gson.toJson(versionedObject);
76+
System.out.println(jsonOutput);
77+
assertThat(jsonOutput).isEqualTo("{\"newerField\":\"newer\",\"newField\":\"new\",\"field\":\"old\"}");
78+
}
79+
80+
}

javalib-io-json/src/test/java/io/github/dunwu/javalib/json/jackson/JacksonPerformanceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
public class JacksonPerformanceTests {
2020

21-
final ObjectMapper mapper = new ObjectMapper();
21+
private final ObjectMapper mapper = new ObjectMapper();
2222

23-
public static final int BATCH_SIZE = 100000;
23+
private static final int BATCH_SIZE = 100000;
2424

2525
/**
2626
* 循环序列化、反序列 {@link #BATCH_SIZE} 条数据,测试性能

javalib-io-json/src/test/java/io/github/dunwu/javalib/json/util/BeanUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.github.dunwu.javalib.json.TestBean;
44
import io.github.dunwu.javalib.json.TestBean2;
5+
import io.github.dunwu.javalib.json.bean.Group;
6+
import io.github.dunwu.javalib.json.bean.User;
57

68
import java.sql.Date;
79
import java.time.LocalDate;
@@ -14,6 +16,25 @@
1416
*/
1517
public class BeanUtils {
1618

19+
public static Group initGroupBean() {
20+
Group group = new Group();
21+
group.setId(0L);
22+
group.setName("admin");
23+
24+
User guestUser = new User();
25+
guestUser.setId(2L);
26+
guestUser.setName("guest");
27+
28+
User rootUser = new User();
29+
rootUser.setId(3L);
30+
rootUser.setName("root");
31+
32+
group.addUser(guestUser);
33+
group.addUser(rootUser);
34+
35+
return group;
36+
}
37+
1738
public static TestBean initJdk8Bean() {
1839
String[] strArray = { "a", "b", "c" };
1940
Integer[] intArray = { 1, 2, 3, 4, 5 };

0 commit comments

Comments
 (0)