diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml
index f941dd3265e9..3f3b9da6a3b8 100644
--- a/libraries-data/pom.xml
+++ b/libraries-data/pom.xml
@@ -163,6 +163,12 @@
${flink.version}
test
+
+ junit
+ junit
+ 4.13.1
+ test
+
diff --git a/libraries-data/src/main/java/com/baeldung/gson/pojo/User.java b/libraries-data/src/main/java/com/baeldung/gson/pojo/User.java
new file mode 100644
index 000000000000..7e50417f5e27
--- /dev/null
+++ b/libraries-data/src/main/java/com/baeldung/gson/pojo/User.java
@@ -0,0 +1,64 @@
+package com.baeldung.gson.pojo;
+
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
+public class User {
+
+ @Expose
+ @SerializedName(value = "firstName", alternate = { "fullName", "name" })
+ String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ @Expose
+ int age;
+
+ @Expose(serialize = true, deserialize = false)
+ public long id;
+
+ @Expose(serialize = false, deserialize = false)
+ private String email;
+
+ // Constructors, Getters, and Setters
+ public User(String name, int age, String email) {
+ this.name = name;
+ this.age = age;
+ this.email = email;
+ }
+
+ @Override
+ public String toString() {
+ return "User{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + ", email='" + email + '\'' + '}';
+ }
+}
diff --git a/libraries-data/src/test/java/com/baeldung/gson/GsonUnitTest.java b/libraries-data/src/test/java/com/baeldung/gson/GsonUnitTest.java
new file mode 100644
index 000000000000..5fd733f9388f
--- /dev/null
+++ b/libraries-data/src/test/java/com/baeldung/gson/GsonUnitTest.java
@@ -0,0 +1,60 @@
+package com.baeldung.gson;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.junit.jupiter.api.Test;
+
+import com.baeldung.gson.pojo.User;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+public class GsonUnitTest {
+
+ @Test
+ public void testSerialization() {
+ User user = new User("John Doe", 30, "john.doe@example.com");
+ user.setId(12345L);
+
+ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
+ .create();
+ String json = gson.toJson(user);
+
+ // Verify that name, age, and id are serialized, but email is not
+ assertEquals("{\"firstName\":\"John Doe\",\"age\":30,\"id\":12345}", json);
+ }
+
+ @Test
+ public void testDeserialization() {
+ String jsonInput = "{\"firstName\":\"Jane Doe\",\"age\":25,\"id\":67890,\"email\":\"jane.doe@example.com\"}";
+
+ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
+ .create();
+ User user = gson.fromJson(jsonInput, User.class);
+
+ // Verify that name and age are deserialized, but email and id are not
+ assertEquals("Jane Doe", user.getName());
+ assertEquals(25, user.getAge());
+ assertEquals(0, user.getId());
+ assertNull(user.getEmail());
+ }
+
+ @Test
+ public void testDeserializationWithAlternateNames() {
+ String jsonInput1 = "{\"firstName\":\"Jane Doe\",\"age\":25,\"id\":67890,\"email\":\"jane.doe@example.com\"}";
+ String jsonInput2 = "{\"fullName\":\"John Doe\",\"age\":30,\"id\":12345,\"email\":\"john.doe@example.com\"}";
+ String jsonInput3 = "{\"name\":\"Alice\",\"age\":28,\"id\":54321,\"email\":\"alice@example.com\"}";
+
+ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
+ .create();
+
+ User user1 = gson.fromJson(jsonInput1, User.class);
+ User user2 = gson.fromJson(jsonInput2, User.class);
+ User user3 = gson.fromJson(jsonInput3, User.class);
+
+ // Verify that the name field is correctly deserialized from different JSON field names
+ assertEquals("Jane Doe", user1.getName());
+ assertEquals("John Doe", user2.getName());
+ assertEquals("Alice", user3.getName());
+ }
+}