JIRA BAEL-6693, difference between Gson @Expose and @SerializedName (#14370)

Co-authored-by: parthiv39731 <parthiv39731@gmail.com>
This commit is contained in:
parthiv39731 2023-07-08 13:52:04 +05:30 committed by GitHub
parent 5f025f6dd1
commit 571df122f7
5 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.baeldung.gson.entities;
import com.google.gson.annotations.Expose;
public class BankAccount {
@Expose(serialize = false, deserialize = false)
private String accountNumber;
@Expose(serialize = true, deserialize = true)
private String bankName;
public BankAccount(String accountNumber, String bankName) {
this.accountNumber = accountNumber;
this.bankName = bankName;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.gson.entities;
import com.google.gson.annotations.SerializedName;
public class Country {
@SerializedName(value = "name")
private String countryName;
@SerializedName(value = "capital")
private String countryCapital;
@SerializedName(value = "continent")
private String continentName;
public Country(String countryName, String countryCapital, String continentName) {
this.countryName = countryName;
this.countryCapital = countryCapital;
this.continentName = continentName;
}
@Override
public String toString() {
return "Country{" +
"countryName='" + countryName + '\'' +
", countryCapital='" + countryCapital + '\'' +
", continentName='" + continentName + '\'' +
'}';
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCountryCapital() {
return countryCapital;
}
public void setCountryCapital(String countryCapital) {
this.countryCapital = countryCapital;
}
public String getContinentName() {
return continentName;
}
public void setContinentName(String continentName) {
this.continentName = continentName;
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.gson.entities;
import com.google.gson.annotations.Expose;
import java.util.List;
public class Person {
public Person(String firstName, String lastName, String emailAddress, String password, List<BankAccount> bankAccounts) {
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
this.password = password;
this.bankAccounts = bankAccounts;
}
@Expose(serialize = true)
private String firstName;
@Expose(serialize = true)
private String lastName;
@Expose()
private String emailAddress;
@Expose(serialize = false)
private String password;
@Expose(serialize = true)
private List<BankAccount> bankAccounts;
public List<BankAccount> getBankAccounts() {
return bankAccounts;
}
public void setBankAccounts(List<BankAccount> bankAccounts) {
this.bankAccounts = bankAccounts;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.gson.exposevsserializedname;
import com.baeldung.gson.entities.Country;
import com.baeldung.gson.entities.Person;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class PersonSerializer {
private static final Gson configuredGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
private static final Gson defaultGson = new Gson();
public static String serializeWithConfiguredGson(Person person) {
return configuredGson.toJson(person);
}
public static String serializeWithDefaultGson(Person person) {
return defaultGson.toJson(person);
}
public static String toJsonString(Object obj) {
return defaultGson.toJson(obj);
}
public static Country fromJsonString(String json) {
return defaultGson.fromJson(json, Country.class);
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.gson.exposevsserializedname;
import com.baeldung.gson.entities.BankAccount;
import com.baeldung.gson.entities.Country;
import com.baeldung.gson.entities.Person;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class PersonSerializerUnitTest {
private static final Logger logger = LoggerFactory.getLogger(PersonSerializerUnitTest.class);
private Person person = null;
private Country country = null;
@Before
public void preparePersonObj() {
List<BankAccount> accounts = new ArrayList<>();
BankAccount acc1 = new BankAccount("4565432312", "Bank of America");
BankAccount acc2 = new BankAccount("4565432616", "Bank of America");
accounts.add(acc1);
accounts.add(acc2);
person = new Person(
"James", "Cameron", "james.cameron@gmail.com",
"secret", accounts);
country = new Country("India", "New Delhi", "Asia");
}
@Test
public void WhenUseCustomGson_ThenDonotSerializeAccountNumAndPassword () {
String personJson = PersonSerializer.serializeWithConfiguredGson(person);
logger.info(personJson);
assertFalse("Test failed: password found", personJson.contains("password"));
assertFalse("Test failed: account number found", personJson.contains("accountNumber:"));
}
@Test
public void WhenUseDefaultGson_ThenSerializeAccountNumAndPassword () {
String personJson = PersonSerializer.serializeWithDefaultGson(person);
logger.info(personJson);
assertTrue("Test failed: password not found", personJson.contains("password"));
assertTrue("Test failed: account number not found", personJson.contains("accountNumber"));
}
@Test
public void whenUseSerializedAnnotation_ThenUseSerializedNameinJsonString() {
String countryJson = PersonSerializer.toJsonString(country);
logger.info(countryJson);
assertFalse("Test failed: No change in the keys", countryJson.contains("countryName"));
assertFalse("Test failed: No change in the keys", countryJson.contains("contentName"));
assertFalse("Test failed: No change in the keys", countryJson.contains("countryCapital"));
assertTrue("Test failed: No change in the keys", countryJson.contains("name"));
assertTrue("Test failed: No change in the keys", countryJson.contains("continent"));
assertTrue("Test failed: No change in the keys", countryJson.contains("capital"));
}
@Test
public void whenJsonStrCreatedWithCustomKeys_ThenCreateObjUsingGson() {
String countryJson = PersonSerializer.toJsonString(country);
Country country = PersonSerializer.fromJsonString(countryJson);
logger.info(country.toString());
assertEquals("Fail: Object creation failed", country.getCountryName(), "India");
assertEquals("Fail: Object creation failed", country.getCountryCapital(), "New Delhi");
assertEquals("Fail: Object creation failed", country.getContinentName(), "Asia");
}
}