Skip to content

Commit 75e9b0a

Browse files
committed
Gson example
1 parent 020cd81 commit 75e9b0a

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="lib" path="libs/gson-2.3.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>com.vogella.java.library.gson</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
202 KB
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.vogella.java.library.gson;
2+
3+
import java.lang.reflect.Type;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import com.google.gson.Gson;
8+
import com.google.gson.reflect.TypeToken;
9+
10+
11+
12+
public class MainTest {
13+
14+
public static void main(String[] args) {
15+
List<Task> list = new ArrayList<Task>();
16+
for (int i = 0; i < 20; i++) {
17+
list.add(new Task(i, "Test1", "Test2", Task.Status.ASSIGNED, 10));
18+
}
19+
Gson gson = new Gson();
20+
Type type = new TypeToken<List<Task>>() {}.getType();
21+
String json = gson.toJson(list, type);
22+
System.out.println(json);
23+
List<Task> fromJson = gson.fromJson(json, type);
24+
25+
for (Task task : fromJson) {
26+
System.out.println(task);
27+
}
28+
}
29+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.vogella.java.library.gson;
2+
3+
public class Task {
4+
private final long id;
5+
private String summary;
6+
private String description;
7+
private Status status;
8+
private int priority;
9+
10+
public enum Status {
11+
CREATED, ASSIGNED, CANCELED, COMPLETED
12+
}
13+
14+
15+
16+
17+
public Task(long id, String summary, String description, Status status,
18+
int priority) {
19+
this.id = id;
20+
this.summary = summary;
21+
this.description = description;
22+
this.status = status;
23+
this.priority = priority;
24+
}
25+
26+
public long getId() {
27+
return id;
28+
}
29+
30+
public String getSummary() {
31+
return summary;
32+
}
33+
34+
public void setSummary(String summary) {
35+
this.summary = summary;
36+
}
37+
38+
public String getDescription() {
39+
return description;
40+
}
41+
42+
public void setDescription(String description) {
43+
this.description = description;
44+
}
45+
46+
public Status getStatus() {
47+
return status;
48+
}
49+
50+
public void setStatus(Status status) {
51+
this.status = status;
52+
}
53+
54+
public int getPriority() {
55+
return priority;
56+
}
57+
58+
public void setPriority(int priority) {
59+
this.priority = priority;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Task [id=" + id + ", summary=" + summary + ", description="
65+
+ description + ", status=" + status + ", priority=" + priority
66+
+ "]";
67+
}
68+
69+
70+
71+
}

0 commit comments

Comments
 (0)