Skip to content

Commit 712c791

Browse files
committed
add gson examples
1 parent 8ab0d2e commit 712c791

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

json/gson/GsonBuilderEx.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.zetcode;
2+
3+
import com.google.gson.FieldNamingPolicy;
4+
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
6+
import java.io.IOException;
7+
import java.io.PrintStream;
8+
9+
class User {
10+
11+
private final String firstName;
12+
private final String lastName;
13+
14+
public User(String firstName, String lastName) {
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
}
18+
}
19+
20+
public class GsonBuilderEx {
21+
22+
public static void main(String[] args) throws IOException {
23+
24+
try (PrintStream prs = new PrintStream(System.out, true,
25+
"UTF8")) {
26+
27+
Gson gson = new GsonBuilder()
28+
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
29+
.create();
30+
31+
User user = new User("Peter", "Flemming");
32+
gson.toJson(user, prs);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)