Skip to content

Commit 5e3c196

Browse files
Merge pull request #18492 from bhaskar16/BAEL-6553-serialised
[BAEL-6553] Gson Expose and SerializedName tests
2 parents 4d86cee + e75cf91 commit 5e3c196

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

json-modules/gson-3/pom.xml

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

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)