diff --git a/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java b/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java index a0fd3c0ff9..4f233e7d47 100644 --- a/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java +++ b/gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java @@ -1,9 +1,11 @@ package org.baeldung.gson.serialization; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.lang.reflect.Type; import java.util.Collection; +import java.util.Date; import org.junit.Test; @@ -61,4 +63,17 @@ public class GsonSerializationTest { final String estimatedResult = "{\"intValue\":7}"; assertEquals(estimatedResult, jsonString); } + + @Test + public void givenDate_whenSerializing_thenCorrect() { + final Date sourceDate = new Date(1000000L); + final Gson gson = new Gson(); + final Type sourceDateType = new TypeToken() { + }.getType(); + final String jsonDate = gson.toJson(sourceDate, sourceDateType); + + // test + final Date testDate = gson.fromJson(jsonDate, sourceDateType); + assertTrue(sourceDate.equals(testDate)); + } } \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java b/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java index 27f9192ab8..02de2636c2 100644 --- a/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java +++ b/gson/src/test/java/org/baeldung/gson/serialization/SourceClass.java @@ -1,45 +1,52 @@ package org.baeldung.gson.serialization; public class SourceClass { - public int intValue; - public String stringValue; + private int intValue; + private String stringValue; public SourceClass(final int intValue, final String stringValue) { this.intValue = intValue; this.stringValue = stringValue; } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + intValue; - result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); - return result; + // API + + public int getIntValue() { + return intValue; } - @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final SourceClass other = (SourceClass) obj; - if (intValue != other.intValue) - return false; - if (stringValue == null) { - if (other.stringValue != null) - return false; - } else if (!stringValue.equals(other.stringValue)) - return false; - return true; + public String getStringValue() { + return stringValue; } + // + @Override public String toString() { return "SourceClass{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}'; } + @Override + public boolean equals(final Object o) { + if (this == o) + return true; + if (!(o instanceof SourceClass)) + return false; + + final SourceClass that = (SourceClass) o; + + if (intValue != that.intValue) + return false; + if (!stringValue.equals(that.stringValue)) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = intValue; + result = 31 * result + stringValue.hashCode(); + return result; + } } diff --git a/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java b/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java index b15a9aa4e5..aef2d31011 100644 --- a/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java +++ b/gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java @@ -15,8 +15,8 @@ public class SourceClassChangingFieldNamesSerializer implements JsonSerializer { - @Override public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) { final String intValue = "intValue"; final JsonObject jObject = new JsonObject(); - jObject.addProperty(intValue, src.intValue); + jObject.addProperty(intValue, src.getIntValue()); return jObject; }