All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.geotab.model.serialization.FailureModeDeserializer Maven / Gradle / Ivy

/*
 *
 * 2020 Copyright (C) Geotab Inc. All rights reserved.
 */

package com.geotab.model.serialization;

import static com.geotab.model.entity.failuremode.NoFailureMode.NO_FAILURE_MODE_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.failuremode.FailureMode;
import com.geotab.model.entity.failuremode.NoFailureMode;
import com.geotab.model.entity.source.Source;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;

public class FailureModeDeserializer extends JsonDeserializer {

  @Override
  public FailureMode 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 (NO_FAILURE_MODE_ID.equalsIgnoreCase(id)) {
        return NoFailureMode.getInstance();
      }
      return FailureMode.failureModeBuilder()
          .id(id)
          .name(id)
          .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;
      Double code = node.get("code") != null ? node.get("code").doubleValue() : null;
      Source source = node.get("source") != null ? parserCodec
          .treeToValue(node.get("source"), Source.class) : null;

      return FailureMode.failureModeBuilder()
          .id(id != null ? id.getId() : null)
          .name(name)
          .code(code)
          .source(source)
          .build();
    }

    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy