Skip to content

[BAEL-6553] Gson Expose and SerializedName tests #18492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions json-modules/gson-3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>json-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>gson-3</artifactId>

<properties>
<gson-version>2.12.1</gson-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson-version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
61 changes: 61 additions & 0 deletions json-modules/gson-3/src/main/java/User.java
Original file line number Diff line number Diff line change
@@ -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 + '\'' + '}';
}
}
56 changes: 56 additions & 0 deletions json-modules/gson-3/src/test/java/GsonUnitTest.java
Original file line number Diff line number Diff line change
@@ -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, "[email protected]");
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\":\"[email protected]\"}";

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\":\"[email protected]\"}";
String jsonInput2 = "{\"fullName\":\"John Doe\",\"age\":30,\"id\":12345,\"email\":\"[email protected]\"}";
String jsonInput3 = "{\"name\":\"Alice\",\"age\":28,\"id\":54321,\"email\":\"[email protected]\"}";

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());
}
}
1 change: 1 addition & 0 deletions json-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>json-path</module>
<module>gson</module>
<module>gson-2</module>
<module>gson-3</module>
</modules>

<build>
Expand Down