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

com.hubspot.singularity.data.transcoders.CompressingJsonTranscoder Maven / Gradle / Ivy

package com.hubspot.singularity.data.transcoders;

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.singularity.config.SingularityConfiguration;
import java.io.IOException;
import javax.annotation.Nullable;

public class CompressingJsonTranscoder extends CompressingTranscoder {
  private static final byte[] EMPTY_BYTES = new byte[0];

  private final ObjectMapper objectMapper;
  private final Class clazz;

  CompressingJsonTranscoder(
    final SingularityConfiguration configuration,
    final ObjectMapper objectMapper,
    final Class clazz
  ) {
    super(configuration);
    this.objectMapper = checkNotNull(objectMapper, "objectMapper is null");
    this.clazz = checkNotNull(clazz, "clazz is null");
  }

  @Override
  protected T actualFromBytes(@Nullable byte[] data)
    throws SingularityTranscoderException {
    if (data == null || data.length == 0) {
      return null;
    }

    try {
      return objectMapper.readValue(data, clazz);
    } catch (IOException e) {
      throw new SingularityTranscoderException(e);
    }
  }

  @Override
  protected byte[] actualToBytes(@Nullable T object)
    throws SingularityTranscoderException {
    try {
      return object == null ? EMPTY_BYTES : objectMapper.writeValueAsBytes(object);
    } catch (IOException e) {
      throw new SingularityTranscoderException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy