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

com.cedarsoft.serialization.jackson.CollectionSerializer Maven / Gradle / Ivy

There is a newer version: 8.9.2
Show newest version
package com.cedarsoft.serialization.jackson;

import java.io.IOException;
import java.util.List;

import javax.annotation.Nonnull;

import com.cedarsoft.version.Version;
import com.cedarsoft.version.VersionException;
import com.cedarsoft.version.VersionRange;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * @author Johannes Schneider ([email protected])
 */
public class CollectionSerializer extends AbstractJacksonSerializer> {
  @Nonnull
  private final Class listType;

  public CollectionSerializer(@Nonnull Class listType, @Nonnull AbstractJacksonSerializer serializer) {
    this(listType, serializer, serializer.getType() + "s", serializer.getFormatVersionRange());
  }

  public CollectionSerializer(@Nonnull Class listType, @Nonnull AbstractJacksonSerializer serializer, @Nonnull String type, @Nonnull VersionRange formatVersionRange) {
    super(type, formatVersionRange);
    this.listType = listType;

    add(serializer).responsibleFor(listType).map(formatVersionRange).toDelegateVersion(serializer.getFormatVersion());
    assert getDelegatesMappings().verify();
  }

  @Override
  public void serialize(@Nonnull JsonGenerator serializeTo, @Nonnull List object, @Nonnull Version formatVersion) throws IOException, VersionException, JsonProcessingException {
    verifyVersionWritable(formatVersion);
    serializeArray(object, listType, getType(), serializeTo, formatVersion);
  }

  @Nonnull
  @Override
  public List deserialize(@Nonnull JsonParser deserializeFrom, @Nonnull Version formatVersion) throws IOException, VersionException, JsonProcessingException {
    verifyVersionReadable(formatVersion);

    List elements = deserializeArray(listType, getType(), deserializeFrom, formatVersion);

    JacksonParserWrapper parser = new JacksonParserWrapper(deserializeFrom);
    parser.closeObject();

    return elements;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy