use of jodd.json.mock.Person in project jodd by oblac.
the class JSONDeserializerTest method testDeserializeWithIncludes.
@Test
public void testDeserializeWithIncludes() {
Person igor = creator.createJodder();
String json = new JsonSerializer().include("phones", "hobbies").serialize(igor);
Person jsonIgor = new JsonParser().parse(json, Person.class);
assertEquals(2, jsonIgor.getPhones().size());
assertEquals(0, jsonIgor.getHobbies().size());
}
use of jodd.json.mock.Person in project jodd by oblac.
the class JSONDeserializerTest method testUnixEpoch.
@Test
public void testUnixEpoch() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.AM_PM, Calendar.AM);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Person hank = new Person("Hank", "Paulsen", cal.getTime(), null, null);
String json = new JsonSerializer().serialize(hank);
Person deHank = new JsonParser().parse(json, Person.class);
assertEquals(hank.getFirstname(), deHank.getFirstname());
assertEquals(hank.getLastname(), deHank.getLastname());
assertEquals(hank.getBirthdate(), deHank.getBirthdate());
}
use of jodd.json.mock.Person in project jodd by oblac.
the class JSONDeserializerTest method testDateTransforming.
@Test
public void testDateTransforming() throws ParseException {
final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
Person foo = new Person("Foo", "Bar", new Date(), null, null);
foo.setBirthdate(df.parse("2009/01/02"));
String json = new JsonSerializer().withSerializer("birthdate", new DateJsonSerializer() {
@Override
public boolean serialize(JsonContext jsonContext, Date date) {
jsonContext.writeString(df.format(date));
return true;
}
}).serialize(foo);
Person newUser = new JsonParser().withValueConverter("birthdate", new ValueConverter<String, Date>() {
public Date convert(String data) {
try {
return df.parse(data);
} catch (ParseException pe) {
throw new JsonException(pe);
}
}
}).parse(json, Person.class);
assertEquals(foo.getBirthdate(), newUser.getBirthdate());
assertEquals("2009/01/02", df.format(newUser.getBirthdate()));
}
use of jodd.json.mock.Person in project jodd by oblac.
the class ObjectToMapTest method testConvertPersonToMap.
@Test
public void testConvertPersonToMap() {
Person jodder = new DataCreator().createJodder();
final Map<String, Object> target = new HashMap<>();
JsonContext jsonContext = new JsonSerializer().createJsonContext(null);
BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) {
@Override
protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
target.put(propertyName, value);
}
};
beanSerializer.serialize();
assertEquals(6, target.size());
assertSame(jodder.getBirthdate(), target.get("birthdate"));
assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame"));
assertSame(jodder.getLastname(), target.get("lastname"));
assertSame(jodder.getFirstname(), target.get("firstname"));
assertSame(jodder.getHome(), target.get("home"));
assertSame(jodder.getWork(), target.get("work"));
}
use of jodd.json.mock.Person in project jodd by oblac.
the class ObjectToMapTest method testConvertPersonToMap2.
@Test
public void testConvertPersonToMap2() {
Person jodder = new DataCreator().createJodder();
final Map<String, Object> target = new HashMap<>();
JsonContext jsonContext = new JsonSerializer().include("phones").excludeTypes(Address.class).createJsonContext(null);
BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) {
@Override
protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
target.put(propertyName, value);
}
};
beanSerializer.serialize();
assertEquals(5, target.size());
assertSame(jodder.getBirthdate(), target.get("birthdate"));
assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame"));
assertSame(jodder.getLastname(), target.get("lastname"));
assertSame(jodder.getFirstname(), target.get("firstname"));
assertSame(jodder.getPhones(), target.get("phones"));
}
Aggregations