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

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

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

package com.geotab.model.serialization;

import static com.geotab.model.serialization.ApiCustomDeserializerModifier.defaultDeserialize;

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.entity.diagnostic.BasicDiagnostic;
import com.geotab.model.entity.diagnostic.BrpFaultDiagnostic;
import com.geotab.model.entity.diagnostic.DataDiagnostic;
import com.geotab.model.entity.diagnostic.Diagnostic;
import com.geotab.model.entity.diagnostic.GmcccFaultDiagnostic;
import com.geotab.model.entity.diagnostic.GoDiagnostic;
import com.geotab.model.entity.diagnostic.GoFaultDiagnostic;
import com.geotab.model.entity.diagnostic.LegacyFaultDiagnostic;
import com.geotab.model.entity.diagnostic.NoDiagnostic;
import com.geotab.model.entity.diagnostic.ObdFaultDiagnostic;
import com.geotab.model.entity.diagnostic.ObdSaFaultDiagnostic;
import com.geotab.model.entity.diagnostic.Pid;
import com.geotab.model.entity.diagnostic.ProprietaryFaultDiagnostic;
import com.geotab.model.entity.diagnostic.Sid;
import com.geotab.model.entity.diagnostic.SuspectParameter;
import com.geotab.model.entity.parametergroup.ParameterGroupNone;
import com.geotab.model.enumeration.DiagnosticType;
import java.io.IOException;

public class DiagnosticDeserializer extends JsonDeserializer {

  @Override
  public Diagnostic deserialize(JsonParser jsonParser, DeserializationContext context)
      throws IOException {
    ObjectCodec parserCodec = jsonParser.getCodec();
    JsonNode node = parserCodec.readTree(jsonParser);

    if (node.isTextual()) {
      String id = node.textValue();
      if (NoDiagnostic.NO_DIAGNOSTIC_ID.equals(id)) {
        return NoDiagnostic.getInstance();
      }
    }

    if (node.isObject()) {
      String id = node.get("id") != null ? node.get("id").textValue() : null;
      if (NoDiagnostic.NO_DIAGNOSTIC_ID.equals(id)) {
        return NoDiagnostic.getInstance();
      }
      return deserializeByDiagnosticType(parserCodec, context, node);
    }

    return null;
  }

  private Diagnostic deserializeByDiagnosticType(ObjectCodec parserCodec,
      DeserializationContext context, JsonNode node) throws IOException {

    DiagnosticType diagnosticType = node.get("diagnosticType") != null ? parserCodec
        .treeToValue(node.get("diagnosticType"), DiagnosticType.class) : null;

    if (diagnosticType == null) {
      return defaultDeserialize(parserCodec, context, node, BasicDiagnostic.class);
    }

    switch (diagnosticType) {
      case DATA_DIAGNOSTIC:
        return defaultDeserialize(parserCodec, context, node, DataDiagnostic.class);
      case GO_DIAGNOSTIC:
        return defaultDeserialize(parserCodec, context, node, GoDiagnostic.class);
      case PID:
        return defaultDeserialize(parserCodec, context, node, Pid.class);
      case SID:
        return defaultDeserialize(parserCodec, context, node, Sid.class);
      case SUSPECT_PARAMETER:
        SuspectParameter suspectParameter = defaultDeserialize(parserCodec, context, node,
            SuspectParameter.class);
        if (suspectParameter.getParameterGroup() == null) {
          suspectParameter.setParameterGroup(ParameterGroupNone.getInstance());
        }
        return suspectParameter;
      case OBD_FAULT:
        return defaultDeserialize(parserCodec, context, node, ObdFaultDiagnostic.class);
      case GO_FAULT:
        return defaultDeserialize(parserCodec, context, node, GoFaultDiagnostic.class);
      case OBD_WWH_FAULT:
        return defaultDeserialize(parserCodec, context, node, ObdSaFaultDiagnostic.class);
      case PROPRIETARY_FAULT:
        return defaultDeserialize(parserCodec, context, node, ProprietaryFaultDiagnostic.class);
      case LEGACY_FAULT:
        return defaultDeserialize(parserCodec, context, node, LegacyFaultDiagnostic.class);
      case GMCCC_FAULT:
        return defaultDeserialize(parserCodec, context, node, GmcccFaultDiagnostic.class);
      case BRP_FAULT:
        return defaultDeserialize(parserCodec, context, node, BrpFaultDiagnostic.class);

      default:
        return defaultDeserialize(parserCodec, context, node, BasicDiagnostic.class);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy