com.geotab.model.serialization.WorkTimeDeserializer Maven / Gradle / Ivy
/*
*
* 2020 Copyright (C) Geotab Inc. All rights reserved.
*/
package com.geotab.model.serialization;
import static com.geotab.model.entity.worktime.WorkTimeAllHours.WORK_TIME_ALL_HOURS_ID;
import static com.geotab.model.entity.worktime.WorkTimeEarlyLeaveHours.WORK_TIME_EARLY_LEAVE_HOURS_ID;
import static com.geotab.model.entity.worktime.WorkTimeLateArrivalHours.WORK_TIME_LATE_ARRIVAL_HOURS_ID;
import static com.geotab.model.entity.worktime.WorkTimeLunchHours.WORK_TIME_LUNCH_HOURS_ID;
import static com.geotab.model.entity.worktime.WorkTimeStandardHours.WORK_TIME_STANDARD_HOURS_ID;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.geotab.model.Id;
import com.geotab.model.entity.worktime.WorkTime;
import com.geotab.model.entity.worktime.WorkTimeAllHours;
import com.geotab.model.entity.worktime.WorkTimeDetail;
import com.geotab.model.entity.worktime.WorkTimeEarlyLeaveHours;
import com.geotab.model.entity.worktime.WorkTimeHolidayGroupId;
import com.geotab.model.entity.worktime.WorkTimeLateArrivalHours;
import com.geotab.model.entity.worktime.WorkTimeLunchHours;
import com.geotab.model.entity.worktime.WorkTimeStandardHours;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
public class WorkTimeDeserializer extends JsonDeserializer {
@Override
public WorkTime deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
ObjectCodec parserCodec = jsonParser.getCodec();
JsonNode node = parserCodec.readTree(jsonParser);
if (node.isTextual()) {
String workTimeSystemId = node.textValue();
switch (workTimeSystemId) {
case WORK_TIME_ALL_HOURS_ID:
return WorkTimeAllHours.getInstance();
case WORK_TIME_STANDARD_HOURS_ID:
return new WorkTimeStandardHours();
case WORK_TIME_LUNCH_HOURS_ID:
return new WorkTimeLunchHours();
case WORK_TIME_EARLY_LEAVE_HOURS_ID:
return new WorkTimeEarlyLeaveHours();
case WORK_TIME_LATE_ARRIVAL_HOURS_ID:
return new WorkTimeLateArrivalHours();
default:
return WorkTime.builder().id(workTimeSystemId).name(workTimeSystemId).build();
}
} else if (node.isObject()) {
Id id = node.get("id") != null ? parserCodec.treeToValue(node.get("id"), Id.class) : null;
String name = node.get("name") != null ? node.get("name").textValue() : null;
String comment = node.get("comment") != null ? node.get("comment").textValue() : null;
List details = node.get("details") != null ? Lists
.newArrayList(parserCodec.treeToValue(node.get("details"), WorkTimeDetail[].class))
: null;
WorkTimeHolidayGroupId holidayGroup = node.get("holidayGroup") != null ? parserCodec
.treeToValue(node.get("holidayGroup"), WorkTimeHolidayGroupId.class) : null;
return WorkTime.builder()
.id(id != null ? id.getId() : null)
.name(name)
.comment(comment)
.holidayGroup(holidayGroup)
.details(details)
.build();
}
return null;
}
}