From 571df122f71da6db614b93404e30dffee0876079 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Sat, 8 Jul 2023 13:52:04 +0530 Subject: [PATCH] JIRA BAEL-6693, difference between Gson @Expose and @SerializedName (#14370) Co-authored-by: parthiv39731 --- .../baeldung/gson/entities/BankAccount.java | 31 ++++++++ .../com/baeldung/gson/entities/Country.java | 50 +++++++++++++ .../com/baeldung/gson/entities/Person.java | 69 +++++++++++++++++ .../PersonSerializer.java | 27 +++++++ .../PersonSerializerUnitTest.java | 75 +++++++++++++++++++ 5 files changed, 252 insertions(+) create mode 100644 json-modules/gson-2/src/main/java/com/baeldung/gson/entities/BankAccount.java create mode 100644 json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Country.java create mode 100644 json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Person.java create mode 100644 json-modules/gson-2/src/main/java/com/baeldung/gson/exposevsserializedname/PersonSerializer.java create mode 100644 json-modules/gson-2/src/test/java/com/baeldung/gson/exposevsserializedname/PersonSerializerUnitTest.java diff --git a/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/BankAccount.java b/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/BankAccount.java new file mode 100644 index 0000000000..0ac01f0477 --- /dev/null +++ b/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/BankAccount.java @@ -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; + } +} diff --git a/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Country.java b/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Country.java new file mode 100644 index 0000000000..070c91849f --- /dev/null +++ b/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Country.java @@ -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; + } +} diff --git a/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Person.java b/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Person.java new file mode 100644 index 0000000000..6178668ff9 --- /dev/null +++ b/json-modules/gson-2/src/main/java/com/baeldung/gson/entities/Person.java @@ -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 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 bankAccounts; + + public List getBankAccounts() { + return bankAccounts; + } + + public void setBankAccounts(List 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; + } +} diff --git a/json-modules/gson-2/src/main/java/com/baeldung/gson/exposevsserializedname/PersonSerializer.java b/json-modules/gson-2/src/main/java/com/baeldung/gson/exposevsserializedname/PersonSerializer.java new file mode 100644 index 0000000000..23fc282caa --- /dev/null +++ b/json-modules/gson-2/src/main/java/com/baeldung/gson/exposevsserializedname/PersonSerializer.java @@ -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); + } +} diff --git a/json-modules/gson-2/src/test/java/com/baeldung/gson/exposevsserializedname/PersonSerializerUnitTest.java b/json-modules/gson-2/src/test/java/com/baeldung/gson/exposevsserializedname/PersonSerializerUnitTest.java new file mode 100644 index 0000000000..007b34c032 --- /dev/null +++ b/json-modules/gson-2/src/test/java/com/baeldung/gson/exposevsserializedname/PersonSerializerUnitTest.java @@ -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 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"); + } +}