Skip to content

Commit c9f92be

Browse files
author
eugenp
committed
gson work
1 parent 9254032 commit c9f92be

4 files changed

Lines changed: 47 additions & 26 deletions

File tree

‎gson/src/test/java/org/baeldung/gson/serialization/GsonSerializationTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.baeldung.gson.serialization;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
45

56
import java.lang.reflect.Type;
67
import java.util.Collection;
8+
import java.util.Date;
79

810
import org.junit.Test;
911

@@ -61,4 +63,17 @@ public void givenUsingCustomSerializer_whenSerializingObject_thenFieldIgnored()
6163
final String estimatedResult = "{\"intValue\":7}";
6264
assertEquals(estimatedResult, jsonString);
6365
}
66+
67+
@Test
68+
public void givenDate_whenSerializing_thenCorrect() {
69+
final Date sourceDate = new Date(1000000L);
70+
final Gson gson = new Gson();
71+
final Type sourceDateType = new TypeToken<Date>() {
72+
}.getType();
73+
final String jsonDate = gson.toJson(sourceDate, sourceDateType);
74+
75+
// test
76+
final Date testDate = gson.fromJson(jsonDate, sourceDateType);
77+
assertTrue(sourceDate.equals(testDate));
78+
}
6479
}
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
11
package org.baeldung.gson.serialization;
22

33
public class SourceClass {
4-
public int intValue;
5-
public String stringValue;
4+
private int intValue;
5+
private String stringValue;
66

77
public SourceClass(final int intValue, final String stringValue) {
88
this.intValue = intValue;
99
this.stringValue = stringValue;
1010
}
1111

12+
// API
13+
14+
public int getIntValue() {
15+
return intValue;
16+
}
17+
18+
public String getStringValue() {
19+
return stringValue;
20+
}
21+
22+
//
23+
1224
@Override
13-
public int hashCode() {
14-
final int prime = 31;
15-
int result = 1;
16-
result = prime * result + intValue;
17-
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
18-
return result;
25+
public String toString() {
26+
return "SourceClass{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}';
1927
}
2028

2129
@Override
22-
public boolean equals(final Object obj) {
23-
if (this == obj)
30+
public boolean equals(final Object o) {
31+
if (this == o)
2432
return true;
25-
if (obj == null)
33+
if (!(o instanceof SourceClass))
2634
return false;
27-
if (getClass() != obj.getClass())
28-
return false;
29-
final SourceClass other = (SourceClass) obj;
30-
if (intValue != other.intValue)
35+
36+
final SourceClass that = (SourceClass) o;
37+
38+
if (intValue != that.intValue)
3139
return false;
32-
if (stringValue == null) {
33-
if (other.stringValue != null)
34-
return false;
35-
} else if (!stringValue.equals(other.stringValue))
40+
if (!stringValue.equals(that.stringValue))
3641
return false;
42+
3743
return true;
3844
}
3945

4046
@Override
41-
public String toString() {
42-
return "SourceClass{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}';
47+
public int hashCode() {
48+
int result = intValue;
49+
result = 31 * result + stringValue.hashCode();
50+
return result;
4351
}
44-
4552
}

‎gson/src/test/java/org/baeldung/gson/serialization/SourceClassChangingFieldNamesSerializer.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final
1515
final String otherStringValueName = "otherStringValue";
1616

1717
final JsonObject jObject = new JsonObject();
18-
jObject.addProperty(otherIntValueName, src.intValue);
19-
jObject.addProperty(otherStringValueName, src.stringValue);
18+
jObject.addProperty(otherIntValueName, src.getIntValue());
19+
jObject.addProperty(otherStringValueName, src.getStringValue());
2020

2121
return jObject;
2222
}

‎gson/src/test/java/org/baeldung/gson/serialization/SourceClassIgnoringExtraFieldsSerializer.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
import com.google.gson.JsonSerializer;
99

1010
public class SourceClassIgnoringExtraFieldsSerializer implements JsonSerializer<SourceClass> {
11-
1211
@Override
1312
public JsonElement serialize(final SourceClass src, final Type typeOfSrc, final JsonSerializationContext context) {
1413
final String intValue = "intValue";
1514
final JsonObject jObject = new JsonObject();
16-
jObject.addProperty(intValue, src.intValue);
15+
jObject.addProperty(intValue, src.getIntValue());
1716

1817
return jObject;
1918
}

0 commit comments

Comments
 (0)