com.geotab.model.serialization.ParameterGroupDeserializer Maven / Gradle / Ivy
/*
*
* 2020 Copyright (C) Geotab Inc. All rights reserved.
*/
package com.geotab.model.serialization;
import static com.geotab.model.entity.parametergroup.ParameterGroupNone.PARAMETER_GROUP_NONE_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.parametergroup.ParameterGroup;
import com.geotab.model.entity.parametergroup.ParameterGroupNone;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
public class ParameterGroupDeserializer extends JsonDeserializer {
@Override
public ParameterGroup deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
ObjectCodec parserCodec = jsonParser.getCodec();
JsonNode node = parserCodec.readTree(jsonParser);
if (node.isTextual()) {
String id = node.textValue();
if (StringUtils.isEmpty(id)) {
return null;
}
if (PARAMETER_GROUP_NONE_ID.equalsIgnoreCase(id)) {
return ParameterGroupNone.getInstance();
}
return ParameterGroup.parameterGroupBuilder()
.id(id)
.name(id)
.code(0)
.dataLength(0)
.build();
} else if (node.isObject()) {
Id id = node.get("id") != null ? parserCodec.treeToValue(node.get("id"), Id.class) : null;
if (PARAMETER_GROUP_NONE_ID.equalsIgnoreCase(id != null ? id.getId() : null)) {
return ParameterGroupNone.getInstance();
}
String name = node.get("name") != null ? node.get("name").textValue() : null;
Integer code = node.get("code") != null ? node.get("code").intValue() : null;
Integer dataLength =
node.get("dataLength") != null ? node.get("dataLength").intValue() : null;
return ParameterGroup.parameterGroupBuilder()
.id(id != null ? id.getId() : null)
.name(name)
.code(code)
.dataLength(dataLength)
.build();
}
return null;
}
}