gson work

This commit is contained in:
eugenp 2014-09-08 20:05:53 +03:00
parent 9254032a03
commit c9f92be3b1
4 changed files with 51 additions and 30 deletions

View File

@ -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<Date>() {
}.getType();
final String jsonDate = gson.toJson(sourceDate, sourceDateType);
// test
final Date testDate = gson.fromJson(jsonDate, sourceDateType);
assertTrue(sourceDate.equals(testDate));
}
}

View File

@ -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;
}
}

View File

@ -15,8 +15,8 @@ public class SourceClassChangingFieldNamesSerializer implements JsonSerializer<S
final String otherStringValueName = "otherStringValue";
final JsonObject jObject = new JsonObject();
jObject.addProperty(otherIntValueName, src.intValue);
jObject.addProperty(otherStringValueName, src.stringValue);
jObject.addProperty(otherIntValueName, src.getIntValue());
jObject.addProperty(otherStringValueName, src.getStringValue());
return jObject;
}

View File

@ -8,12 +8,11 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class SourceClassIgnoringExtraFieldsSerializer implements JsonSerializer<SourceClass> {
@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;
}