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

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

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

package com.geotab.model.serialization;

import static com.google.common.base.Preconditions.checkNotNull;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.geotab.model.entity.failuremode.FailureMode;
import java.io.IOException;

public class FailureModeSerializer extends JsonSerializer {

  private final JsonSerializer defaultSerializer;

  /**
   * Need default serializer so that {@link FailureMode} which is not system wise, can be serialized
   * normally as object json. Otherwise, the serialization process ends up in {@link
   * StackOverflowError}.
   *
   * @param defaultSerializer The default serializer.
   */
  public FailureModeSerializer(JsonSerializer defaultSerializer) {
    this.defaultSerializer = checkNotNull(defaultSerializer);
  }

  @Override
  public void serialize(FailureMode failureMode, JsonGenerator jsonGenerator,
      SerializerProvider serializerProvider) throws IOException {
    if (failureMode.isSystemEntity()) {
      jsonGenerator.writeString(failureMode.getId().getId());
    } else {
      defaultSerializer.serialize(failureMode, jsonGenerator, serializerProvider);
    }
  }
}