Skip to content

Commit db2b65c

Browse files
committed
commit
1 parent 712c791 commit db2b65c

19 files changed

Lines changed: 664 additions & 0 deletions

json/gson/GsonDataBindApiRead.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.io.Reader;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
class Car {
16+
17+
private final String name;
18+
private final String model;
19+
private final int price;
20+
private final String[] colours;
21+
22+
public Car(String name, String model, int price, String[] colours) {
23+
this.name = name;
24+
this.model = model;
25+
this.price = price;
26+
this.colours = colours;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Car{" + "name=" + name + ", model=" + model +
32+
", price=" + price + ", colours=" + Arrays.toString(colours) + '}';
33+
}
34+
}
35+
36+
public class GsonDataBindApiRead {
37+
38+
public static void main(String[] args) throws FileNotFoundException, IOException {
39+
40+
String fileName = "src/main/resources/cars.json";
41+
Path path = Paths.get(fileName);
42+
43+
try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
44+
45+
Gson gson = new Gson();
46+
List<Car> cars = gson.fromJson(reader,
47+
new TypeToken<List<Car>>(){}.getType());
48+
49+
cars.forEach(System.out::println);
50+
}
51+
}
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.Writer;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
class Car {
15+
16+
private final String name;
17+
private final String model;
18+
private final int price;
19+
private final String[] colours;
20+
21+
public Car(String name, String model, int price, String[] colours) {
22+
this.name = name;
23+
this.model = model;
24+
this.price = price;
25+
this.colours = colours;
26+
}
27+
}
28+
29+
public class GsonDataBindApiWrite {
30+
31+
public static void main(String[] args) throws FileNotFoundException, IOException {
32+
33+
List<Car> cars = new ArrayList<>();
34+
cars.add(new Car("Audi", "2012", 22000, new String[]{"gray", "red", "white"}));
35+
cars.add(new Car("Skoda", "2016", 14000, new String[]{"black", "gray", "white"}));
36+
cars.add(new Car("Volvo", "2010", 19500, new String[]{"black", "silver", "beige"}));
37+
38+
String fileName = "src/main/resources/cars.json";
39+
Path path = Paths.get(fileName);
40+
41+
try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
42+
43+
Gson gson = new Gson();
44+
gson.toJson(cars, writer);
45+
}
46+
47+
System.out.println("Cars written to file");
48+
}
49+
}

json/gson/GsonExcludeFileds.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.annotations.Expose;
6+
7+
enum MaritalStatus {
8+
9+
SINGLE,
10+
MARRIED,
11+
DIVORCED,
12+
UNKNOWN
13+
}
14+
15+
class Person {
16+
17+
@Expose
18+
private String firstName;
19+
20+
@Expose
21+
private String lastName;
22+
23+
private MaritalStatus maritalStatus;
24+
25+
public Person(String firstName, String lastName,
26+
MaritalStatus maritalStatus) {
27+
28+
this.firstName = firstName;
29+
this.lastName = lastName;
30+
this.maritalStatus = maritalStatus;
31+
}
32+
33+
public Person() {}
34+
}
35+
36+
public class GsonExcludeFileds {
37+
38+
public static void main(String[] args) {
39+
40+
Gson gson = new GsonBuilder()
41+
.excludeFieldsWithoutExposeAnnotation()
42+
.setPrettyPrinting()
43+
.create();
44+
45+
Person p = new Person("Jack", "Sparrow", MaritalStatus.UNKNOWN);
46+
47+
gson.toJson(p, System.out);
48+
}
49+
}

json/gson/GsonFromJson.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
5+
class User {
6+
7+
private final String firstName;
8+
private final String lastName;
9+
10+
public User(String firstName, String lastName) {
11+
this.firstName = firstName;
12+
this.lastName = lastName;
13+
}
14+
15+
16+
@Override
17+
public String toString() {
18+
return new StringBuilder().append("User{").append("First name: ")
19+
.append(firstName).append(", Last name: ")
20+
.append(lastName).append("}").toString();
21+
}
22+
}
23+
24+
public class GsonFromJson {
25+
26+
public static void main(String[] args) {
27+
28+
String json_string = "{\"firstName\":\"Tom\", \"lastName\": \"Broody\"}";
29+
30+
Gson gson = new Gson();
31+
32+
User user = gson.fromJson(json_string, User.class);
33+
34+
System.out.println(user);
35+
}
36+
}

json/gson/GsonPrettyPrinting.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class GsonPrettyPrinting {
9+
10+
public static void main(String[] args) {
11+
12+
Gson gson = new GsonBuilder()
13+
.setPrettyPrinting()
14+
.create();
15+
16+
Map<String, Integer> items = new HashMap<>();
17+
18+
items.put("chair", 3);
19+
items.put("pencil", 1);
20+
items.put("book", 5);
21+
22+
gson.toJson(items, System.out);
23+
}
24+
}

json/gson/GsonReadArray.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.Reader;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.Arrays;
12+
13+
class User {
14+
15+
private final String firstName;
16+
private final String lastName;
17+
18+
public User(String firstName, String lastName) {
19+
this.firstName = firstName;
20+
this.lastName = lastName;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return new StringBuilder().append("{User").append("First name: ")
26+
.append(firstName).append(", Last name: ")
27+
.append(lastName).append("}").toString();
28+
}
29+
}
30+
31+
public class GsonReadArray {
32+
33+
public static void main(String[] args) throws IOException {
34+
35+
Gson gson = new GsonBuilder().create();
36+
37+
String fileName = "src/main/resources/users.json";
38+
Path path = new File(fileName).toPath();
39+
40+
try (Reader reader = Files.newBufferedReader(path,
41+
StandardCharsets.UTF_8)) {
42+
43+
User[] users = gson.fromJson(reader, User[].class);
44+
45+
Arrays.stream(users).forEach( e -> {
46+
System.out.println(e);
47+
});
48+
}
49+
}
50+
}

json/gson/GsonReadList.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.reflect.TypeToken;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.io.Reader;
9+
import java.util.List;
10+
11+
class Item {
12+
13+
private String name;
14+
private int quantity;
15+
16+
@Override
17+
public String toString() {
18+
return "Item{" + "name=" + name + ", quantity=" + quantity + '}';
19+
}
20+
}
21+
22+
public class GsonReadList {
23+
24+
public static void main(String[] args) throws IOException {
25+
26+
Gson gson = new GsonBuilder().create();
27+
28+
try (Reader reader = new FileReader("src/main/resources/items.json")) {
29+
30+
List<Item> items = gson.fromJson(reader,
31+
new TypeToken<List<Item>>(){}.getType());
32+
33+
items.forEach(System.out::println);
34+
}
35+
}
36+
}

json/gson/GsonReadWebPage.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import java.io.IOException;
5+
import org.jsoup.Jsoup;
6+
7+
class TimeData {
8+
9+
private String time;
10+
private Long milliseconds_since_epoch;
11+
private String date;
12+
13+
@Override
14+
public String toString() {
15+
return "TimeData{" + "time=" + time + ", milliseconds_since_epoch="
16+
+ milliseconds_since_epoch + ", date=" + date + '}';
17+
}
18+
}
19+
20+
21+
public class GsonReadWebPage {
22+
23+
public static void main(String[] args) throws IOException {
24+
25+
String webPage = "http://time.jsontest.com.";
26+
27+
String data = Jsoup.connect(webPage).ignoreContentType(true).execute().body();
28+
29+
Gson gson = new Gson();
30+
TimeData td = gson.fromJson(data, TimeData.class);
31+
32+
System.out.println(td);
33+
}
34+
}

json/gson/GsonSerializeNulls.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
6+
class User {
7+
8+
private String firstName;
9+
private String lastName;
10+
11+
public User() {};
12+
13+
public User(String firstName, String lastName) {
14+
this.firstName = firstName;
15+
this.lastName = lastName;
16+
}
17+
18+
public String getFirstName() {
19+
return firstName;
20+
}
21+
22+
public void setFirstName(String firstName) {
23+
this.firstName = firstName;
24+
}
25+
26+
public String getLastName() {
27+
return lastName;
28+
}
29+
30+
public void setLastName(String lastName) {
31+
this.lastName = lastName;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return new StringBuilder().append("User{").append("First name: ")
37+
.append(firstName).append(", Last name: ")
38+
.append(lastName).append("}").toString();
39+
}
40+
}
41+
42+
public class GsonSerializeNulls {
43+
44+
public static void main(String[] args) {
45+
46+
GsonBuilder builder = new GsonBuilder();
47+
48+
builder.serializeNulls();
49+
50+
Gson gson = builder.create();
51+
52+
User user = new User();
53+
user.setFirstName("Norman");
54+
55+
String json = gson.toJson(user);
56+
System.out.println(json);
57+
}
58+
}

0 commit comments

Comments
 (0)