这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
6 changes: 6 additions & 0 deletions libraries-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
64 changes: 64 additions & 0 deletions libraries-data/src/main/java/com/baeldung/gson/pojo/User.java
Original file line number Diff line number Diff line change
@@ -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 + '\'' + '}';
}
}
60 changes: 60 additions & 0 deletions libraries-data/src/test/java/com/baeldung/gson/GsonUnitTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}