Skip to content

Commit 7f01f1a

Browse files
committed
fmt
1 parent de0ab98 commit 7f01f1a

File tree

5 files changed

+191
-183
lines changed

5 files changed

+191
-183
lines changed
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.github.kuangcp.serialize;
22

3-
import java.io.Serializable;
43
import lombok.Data;
54

5+
import java.io.Serializable;
6+
67
/**
78
* Created by https://github.com/kuangcp on 17-10-24 下午2:26
89
*
@@ -11,20 +12,23 @@
1112
@Data
1213
public class Person implements Serializable {
1314

14-
private String name;
15-
private String address;
16-
private String phone;
15+
private String name;
16+
private String address;
17+
private String phone;
18+
19+
public Person() {
20+
}
1721

18-
public Person(String name) {
19-
this.name = name;
20-
}
22+
public Person(String name) {
23+
this.name = name;
24+
}
2125

22-
@Override
23-
public String toString() {
24-
return "Person{" +
25-
"name='" + name + '\'' +
26-
", address='" + address + '\'' +
27-
", phone='" + phone + '\'' +
28-
'}';
29-
}
26+
@Override
27+
public String toString() {
28+
return "Person{" +
29+
"name='" + name + '\'' +
30+
", address='" + address + '\'' +
31+
", phone='" + phone + '\'' +
32+
'}';
33+
}
3034
}
Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.github.kuangcp.serialize;
22

3-
import static org.hamcrest.CoreMatchers.equalTo;
4-
import static org.junit.Assert.assertThat;
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Assert;
5+
import org.junit.Test;
56

67
import java.io.ByteArrayInputStream;
78
import java.io.ByteArrayOutputStream;
@@ -11,9 +12,9 @@
1112
import java.io.NotSerializableException;
1213
import java.io.ObjectInputStream;
1314
import java.io.ObjectOutputStream;
14-
import lombok.extern.slf4j.Slf4j;
15-
import org.junit.Assert;
16-
import org.junit.Test;
15+
16+
import static org.hamcrest.CoreMatchers.equalTo;
17+
import static org.junit.Assert.assertThat;
1718

1819
/**
1920
* Created by https://github.com/kuangcp on 17-10-24 下午2:27 使用jdk自带的Serializable接口序列化对象 然后反序列化对象
@@ -23,90 +24,90 @@
2324
@Slf4j
2425
public class SerializeTest {
2526

26-
// 字节数组流
27-
@Test
28-
public void testSerializeWithByte() throws IOException, ClassNotFoundException {
29-
Person person = new Person("name");
30-
31-
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
32-
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
33-
output.writeObject(person);
34-
35-
log.info("content={}", byteOutput.toString());
36-
37-
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
38-
39-
ObjectInputStream input = new ObjectInputStream(byteInput);
40-
Person result = (Person) input.readObject();
41-
assertThat(result.getName(), equalTo("name"));
42-
}
43-
44-
/**
45-
* 序列化对象上某个属性没有实现序列化接口,但该属性没有值
46-
*/
47-
@Test
48-
public void testSerializeWithNonSerializableField() throws IOException, ClassNotFoundException {
49-
Address address = new Address();
50-
address.setCountry("country");
51-
52-
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
53-
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
54-
output.writeObject(address);
55-
56-
log.info("content={}", byteOutput.toString());
57-
58-
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
59-
60-
ObjectInputStream input = new ObjectInputStream(byteInput);
61-
Address result = (Address) input.readObject();
62-
assertThat(result.getCountry(), equalTo("country"));
63-
}
64-
65-
/**
66-
* 序列化对象上某个属性没有实现序列化接口,且该属性有值
67-
*/
68-
@Test(expected = NotSerializableException.class)
69-
public void testSerializeWithNonSerializableFieldAndValue() throws IOException {
70-
Address address = new Address();
71-
address.setCountry("country");
72-
Street street = new Street();
73-
street.setStreet("street");
74-
address.setStreet(street);
75-
76-
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
77-
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
78-
output.writeObject(address);
79-
}
80-
81-
@Test
82-
public void testSerializeWithFile() {
83-
try {
84-
writeFile();
85-
readFile();
86-
} catch (IOException | ClassNotFoundException e) {
87-
log.error(e.getMessage(), e);
88-
Assert.fail();
27+
// 字节数组流
28+
@Test
29+
public void testSerializeWithByte() throws IOException, ClassNotFoundException {
30+
Person person = new Person("name");
31+
32+
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
33+
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
34+
output.writeObject(person);
35+
36+
log.info("content={}", byteOutput.toString());
37+
38+
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
39+
40+
ObjectInputStream input = new ObjectInputStream(byteInput);
41+
Person result = (Person) input.readObject();
42+
assertThat(result.getName(), equalTo("name"));
43+
}
44+
45+
/**
46+
* 序列化对象上某个属性没有实现序列化接口,但该属性没有值
47+
*/
48+
@Test
49+
public void testSerializeWithNonSerializableField() throws IOException, ClassNotFoundException {
50+
Address address = new Address();
51+
address.setCountry("country");
52+
53+
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
54+
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
55+
output.writeObject(address);
56+
57+
log.info("content={}", byteOutput.toString());
58+
59+
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
60+
61+
ObjectInputStream input = new ObjectInputStream(byteInput);
62+
Address result = (Address) input.readObject();
63+
assertThat(result.getCountry(), equalTo("country"));
64+
}
65+
66+
/**
67+
* 序列化对象上某个属性没有实现序列化接口,且该属性有值
68+
*/
69+
@Test(expected = NotSerializableException.class)
70+
public void testSerializeWithNonSerializableFieldAndValue() throws IOException {
71+
Address address = new Address();
72+
address.setCountry("country");
73+
Street street = new Street();
74+
street.setStreet("street");
75+
address.setStreet(street);
76+
77+
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
78+
ObjectOutputStream output = new ObjectOutputStream(byteOutput);
79+
output.writeObject(address);
80+
}
81+
82+
@Test
83+
public void testSerializeWithFile() {
84+
try {
85+
writeFile();
86+
readFile();
87+
} catch (IOException | ClassNotFoundException e) {
88+
log.error(e.getMessage(), e);
89+
Assert.fail();
90+
}
91+
}
92+
93+
private void writeFile() throws IOException {
94+
Person person = new Person("myth");
95+
96+
FileOutputStream fileOutputStream = new FileOutputStream("/tmp/person.log");
97+
ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
98+
out.writeObject(person);
99+
out.close();
100+
fileOutputStream.close();
101+
log.debug("序列化完成, person={}", person);
102+
}
103+
104+
private void readFile() throws IOException, ClassNotFoundException {
105+
FileInputStream fileInputStream = new FileInputStream("/tmp/person.log");
106+
ObjectInputStream in = new ObjectInputStream(fileInputStream);
107+
Person object = (Person) in.readObject();
108+
in.close();
109+
fileInputStream.close();
110+
System.out.println(object.toString());
111+
assertThat(object.getName(), equalTo("myth"));
89112
}
90-
}
91-
92-
private void writeFile() throws IOException {
93-
Person person = new Person("myth");
94-
95-
FileOutputStream fileOutputStream = new FileOutputStream("/tmp/person.log");
96-
ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
97-
out.writeObject(person);
98-
out.close();
99-
fileOutputStream.close();
100-
log.debug("序列化完成, person={}", person);
101-
}
102-
103-
private void readFile() throws IOException, ClassNotFoundException {
104-
FileInputStream fileInputStream = new FileInputStream("/tmp/person.log");
105-
ObjectInputStream in = new ObjectInputStream(fileInputStream);
106-
Person object = (Person) in.readObject();
107-
in.close();
108-
fileInputStream.close();
109-
System.out.println(object.toString());
110-
assertThat(object.getName(), equalTo("myth"));
111-
}
112113
}

class/src/test/java/com/github/kuangcp/serialize/json/GsonTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@
1414
@Slf4j
1515
public class GsonTest {
1616

17-
private Gson gson = new Gson();
17+
private Gson gson = new Gson();
1818

19-
@Test
20-
public void testRead() {
19+
@Test
20+
public void testRead() {
2121

22-
Code code = gson.fromJson("{\"code\":12, \"name\":\"ui\"}", Code.class);
23-
log.info(": code={}", code);
24-
}
22+
Code code = gson.fromJson("{\"code\":12, \"name\":\"ui\"}", Code.class);
23+
log.info(": code={}", code);
24+
}
2525

2626

27-
/**
28-
* fromJSON a standard json string
29-
*/
30-
@Test
31-
public void testRead2() {
32-
String origin = "{\"code\":3131,\"playerId\":216,\"title\":\"new email\","
33-
+ "\"content\":\"send by admin platform\","
34-
+ "\"attachment\":\"[{\\\"func\\\":\\\"addItem\\\", \\\"args\\\":[\\\"item_1\\\", 100]}]\"}";
27+
/**
28+
* fromJSON a standard json string
29+
*/
30+
@Test
31+
public void testRead2() {
32+
String origin = "{\"code\":3131,\"playerId\":216,\"title\":\"new email\","
33+
+ "\"content\":\"send by admin platform\","
34+
+ "\"attachment\":\"[{\\\"func\\\":\\\"addItem\\\", \\\"args\\\":[\\\"item_1\\\", 100]}]\"}";
3535

36-
JsonElement element = new JsonParser().parse(origin);
37-
JsonObject rawJsonObject = element.getAsJsonObject();
38-
int code = rawJsonObject.get("code").getAsInt();
39-
log.info("result: code={}", code);
40-
log.info(": jsonElement={}", element);
41-
}
36+
JsonElement element = new JsonParser().parse(origin);
37+
JsonObject rawJsonObject = element.getAsJsonObject();
38+
int code = rawJsonObject.get("code").getAsInt();
39+
log.info("result: code={}", code);
40+
log.info(": jsonElement={}", element);
41+
}
4242

43-
@Data
44-
private class Code {
43+
@Data
44+
private class Code {
4545

46-
private int code;
46+
private int code;
4747

48-
}
48+
}
4949
}

class/src/test/java/com/github/kuangcp/serialize/json/ReadJsonTest.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import com.github.kuangcp.serialize.json.speed.FastJsonTool;
55
import com.github.kuangcp.serialize.json.speed.GsonTool;
66
import com.github.kuangcp.serialize.json.speed.JacksonTool;
7+
78
import java.io.IOException;
89
import java.util.concurrent.TimeUnit;
10+
911
import org.junit.Test;
1012
import org.openjdk.jmh.annotations.Benchmark;
1113
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -30,28 +32,28 @@
3032
@OutputTimeUnit(TimeUnit.MILLISECONDS)
3133
public class ReadJsonTest {
3234

33-
private static final String json = "{\"name\":\"one\",\"address\":\"any province\",\"phone\":null}";
34-
35-
@Benchmark
36-
public void testGsonRead() throws IOException {
37-
new GsonTool().fromJSON(json, Person.class);
38-
}
39-
40-
@Benchmark
41-
public void testJacksonRead() throws IOException {
42-
new JacksonTool().fromJSON(json, Person.class);
43-
}
44-
45-
@Benchmark
46-
public void testFastJsonRead() throws IOException {
47-
new FastJsonTool().fromJSON(json, Person.class);
48-
}
49-
50-
@Test
51-
public void testCompareRead() throws Exception {
52-
Options options = new OptionsBuilder()
53-
.include(ReadJsonTest.class.getSimpleName())
54-
.output("/tmp/" + ReadJsonTest.class.getSimpleName() + ".log").build();
55-
new Runner(options).run();
56-
}
35+
private static final String json = "{\"name\":\"one\",\"address\":\"any province\",\"phone\":null}";
36+
37+
@Benchmark
38+
public void testGsonRead() throws IOException {
39+
new GsonTool().fromJSON(json, Person.class);
40+
}
41+
42+
@Benchmark
43+
public void testJacksonRead() throws IOException {
44+
new JacksonTool().fromJSON(json, Person.class);
45+
}
46+
47+
@Benchmark
48+
public void testFastJsonRead() throws IOException {
49+
new FastJsonTool().fromJSON(json, Person.class);
50+
}
51+
52+
@Test
53+
public void testCompareRead() throws Exception {
54+
Options options = new OptionsBuilder()
55+
.include(ReadJsonTest.class.getSimpleName())
56+
.output("/tmp/" + ReadJsonTest.class.getSimpleName() + ".log").build();
57+
new Runner(options).run();
58+
}
5759
}

0 commit comments

Comments
 (0)