From 923de8ee4ac460e8bbdb3ccae5441ee601d8e745 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Sun, 20 Apr 2025 11:16:51 +0530 Subject: [PATCH] [BAEL-6553] Gson Expose and SerializedName tests --- json-modules/gson-3/pom.xml | 33 ++++++++++ json-modules/gson-3/src/main/java/User.java | 61 +++++++++++++++++++ .../gson-3/src/test/java/GsonUnitTest.java | 56 +++++++++++++++++ json-modules/pom.xml | 1 + 4 files changed, 151 insertions(+) create mode 100644 json-modules/gson-3/pom.xml create mode 100644 json-modules/gson-3/src/main/java/User.java create mode 100644 json-modules/gson-3/src/test/java/GsonUnitTest.java diff --git a/json-modules/gson-3/pom.xml b/json-modules/gson-3/pom.xml new file mode 100644 index 000000000000..1dd8cd3251db --- /dev/null +++ b/json-modules/gson-3/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.baeldung + json-modules + 1.0.0-SNAPSHOT + + + gson-3 + + + 2.12.1 + UTF-8 + + + + junit + junit + 4.13.1 + test + + + com.google.code.gson + gson + ${gson-version} + compile + + + + \ No newline at end of file diff --git a/json-modules/gson-3/src/main/java/User.java b/json-modules/gson-3/src/main/java/User.java new file mode 100644 index 000000000000..894a3fbeb052 --- /dev/null +++ b/json-modules/gson-3/src/main/java/User.java @@ -0,0 +1,61 @@ +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; + + 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 + '\'' + '}'; + } +} \ No newline at end of file diff --git a/json-modules/gson-3/src/test/java/GsonUnitTest.java b/json-modules/gson-3/src/test/java/GsonUnitTest.java new file mode 100644 index 000000000000..610ce901f27f --- /dev/null +++ b/json-modules/gson-3/src/test/java/GsonUnitTest.java @@ -0,0 +1,56 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class GsonUnitTest { + @Test + public void givenUserObject_whenSerialized_thenCorrectJsonProduced() { + 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 givenJsonInput_whenDeserialized_thenCorrectUserObjectProduced() { + 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 givenJsonWithAlternateNames_whenDeserialized_thenCorrectNameFieldMapped() { + 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()); + } +} \ No newline at end of file diff --git a/json-modules/pom.xml b/json-modules/pom.xml index c08d4215a666..debb8c7cdea8 100644 --- a/json-modules/pom.xml +++ b/json-modules/pom.xml @@ -23,6 +23,7 @@ json-path gson gson-2 + gson-3