use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.
the class EntityDictionary method getId.
/**
* Gets id.
*
* @param value the value
* @return the id
*/
public String getId(Object value) {
if (value == null) {
return null;
}
try {
AccessibleObject idField = null;
Type<?> valueClass = getType(value);
for (; idField == null && valueClass != null; valueClass = valueClass.getSuperclass()) {
try {
idField = getEntityBinding(valueClass).getIdField();
} catch (NullPointerException e) {
log.warn("Class: {} ID Field: {}", valueClass.getSimpleName(), idField);
}
}
Type<?> idClass;
Object idValue;
if (idField instanceof Field) {
idValue = ((Field) idField).get(value);
idClass = ((Field) idField).getType();
} else if (idField instanceof Method) {
idValue = ((Method) idField).invoke(value, (Object[]) null);
idClass = ((Method) idField).getReturnType();
} else {
return null;
}
Serde serde = serdeLookup.apply(((ClassType) idClass).getCls());
if (serde != null) {
return String.valueOf(serde.serialize(idValue));
}
return String.valueOf(idValue);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
return null;
}
}
use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.
the class DaySerdeTest method testDateSerialize.
@Test
public void testDateSerialize() {
LocalDateTime localDate = LocalDateTime.of(2020, java.time.Month.of(01), 01, 00, 00, 00);
Day expectedDate = new Day(localDate);
Serde serde = new Day.DaySerde();
Object actual = serde.serialize(expectedDate);
assertEquals("2020-01-01", actual);
}
use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.
the class DaySerdeTest method testDateDeserialize.
@Test
public void testDateDeserialize() {
LocalDateTime localDate = LocalDateTime.of(2020, java.time.Month.of(01), 01, 00, 00, 00);
Day expectedDate = new Day(localDate);
Serde serde = new Day.DaySerde();
Object actualDate = serde.deserialize("2020-01-01");
assertEquals(expectedDate, actualDate);
}
use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.
the class DaySerdeTest method testDeserializeOffsetDateTime.
@Test
public void testDeserializeOffsetDateTime() {
LocalDateTime localDate = LocalDateTime.of(2020, java.time.Month.of(01), 01, 00, 00, 00);
Day expectedDate = new Day(localDate);
OffsetDateTime dateTime = OffsetDateTime.of(2020, 01, 01, 00, 00, 00, 00, ZoneOffset.UTC);
Serde serde = new Day.DaySerde();
Object actualDate = serde.deserialize(dateTime);
assertEquals(expectedDate, actualDate);
}
use of com.yahoo.elide.core.utils.coerce.converters.Serde in project elide by yahoo.
the class HourSerdeTest method testDeserializeTimestamp.
@Test
public void testDeserializeTimestamp() {
String dateInString = "2020-01-01T01";
Hour expectedDate = new Hour(LocalDateTime.from(formatter.parse(dateInString)));
Timestamp timestamp = new Timestamp(expectedDate.getTime());
Serde serde = new Hour.HourSerde();
Object actualDate = serde.deserialize(timestamp);
assertEquals(expectedDate, actualDate);
}
Aggregations