Skip to content

Commit 923de8e

Browse files
committed
[BAEL-6553] Gson Expose and SerializedName tests
1 parent 69e0343 commit 923de8e

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

json-modules/gson-3/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>json-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>gson-3</artifactId>
13+
14+
<properties>
15+
<gson-version>2.12.1</gson-version>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.13.1</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.code.gson</groupId>
27+
<artifactId>gson</artifactId>
28+
<version>${gson-version}</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import com.google.gson.annotations.Expose;
2+
import com.google.gson.annotations.SerializedName;
3+
4+
public class User {
5+
6+
@Expose
7+
@SerializedName(value = "firstName", alternate = { "fullName", "name" })
8+
String name;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public int getAge() {
19+
return age;
20+
}
21+
22+
public void setAge(int age) {
23+
this.age = age;
24+
}
25+
26+
public long getId() {
27+
return id;
28+
}
29+
30+
public void setId(long id) {
31+
this.id = id;
32+
}
33+
34+
public String getEmail() {
35+
return email;
36+
}
37+
38+
public void setEmail(String email) {
39+
this.email = email;
40+
}
41+
42+
@Expose
43+
int age;
44+
45+
@Expose(serialize = true, deserialize = false)
46+
public long id;
47+
48+
@Expose(serialize = false, deserialize = false)
49+
private String email;
50+
51+
public User(String name, int age, String email) {
52+
this.name = name;
53+
this.age = age;
54+
this.email = email;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "User{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + ", email='" + email + '\'' + '}';
60+
}
61+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertNull;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
9+
public class GsonUnitTest {
10+
@Test
11+
public void givenUserObject_whenSerialized_thenCorrectJsonProduced() {
12+
User user = new User("John Doe", 30, "[email protected]");
13+
user.setId(12345L);
14+
15+
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
16+
.create();
17+
String json = gson.toJson(user);
18+
19+
// Verify that name, age, and id are serialized, but email is not
20+
assertEquals("{\"firstName\":\"John Doe\",\"age\":30,\"id\":12345}", json);
21+
}
22+
23+
@Test
24+
public void givenJsonInput_whenDeserialized_thenCorrectUserObjectProduced() {
25+
String jsonInput = "{\"firstName\":\"Jane Doe\",\"age\":25,\"id\":67890,\"email\":\"[email protected]\"}";
26+
27+
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
28+
.create();
29+
User user = gson.fromJson(jsonInput, User.class);
30+
31+
// Verify that name and age are deserialized, but email and id are not
32+
assertEquals("Jane Doe", user.getName());
33+
assertEquals(25, user.getAge());
34+
assertEquals(0, user.getId());
35+
assertNull(user.getEmail());
36+
}
37+
38+
@Test
39+
public void givenJsonWithAlternateNames_whenDeserialized_thenCorrectNameFieldMapped() {
40+
String jsonInput1 = "{\"firstName\":\"Jane Doe\",\"age\":25,\"id\":67890,\"email\":\"[email protected]\"}";
41+
String jsonInput2 = "{\"fullName\":\"John Doe\",\"age\":30,\"id\":12345,\"email\":\"[email protected]\"}";
42+
String jsonInput3 = "{\"name\":\"Alice\",\"age\":28,\"id\":54321,\"email\":\"[email protected]\"}";
43+
44+
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
45+
.create();
46+
47+
User user1 = gson.fromJson(jsonInput1, User.class);
48+
User user2 = gson.fromJson(jsonInput2, User.class);
49+
User user3 = gson.fromJson(jsonInput3, User.class);
50+
51+
// Verify that the name field is correctly deserialized from different JSON field names
52+
assertEquals("Jane Doe", user1.getName());
53+
assertEquals("John Doe", user2.getName());
54+
assertEquals("Alice", user3.getName());
55+
}
56+
}

json-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<module>json-path</module>
2424
<module>gson</module>
2525
<module>gson-2</module>
26+
<module>gson-3</module>
2627
</modules>
2728

2829
<build>

0 commit comments

Comments
 (0)