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

com.algolia.model.search.SnippetResult Maven / Gradle / Ivy

The newest version!
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost
// - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.

package com.algolia.model.search;

import com.algolia.exceptions.AlgoliaRuntimeException;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/** SnippetResult */
@JsonDeserialize(using = SnippetResult.Deserializer.class)
public interface SnippetResult {
  // SnippetResult as Map wrapper.
  static SnippetResult of(Map value) {
    return new MapOfStringSnippetResultWrapper(value);
  }

  // SnippetResult as List wrapper.
  static SnippetResult of(List value) {
    return new ListOfSnippetResultWrapper(value);
  }

  // SnippetResult as Map wrapper.
  @JsonSerialize(using = MapOfStringSnippetResultWrapper.Serializer.class)
  class MapOfStringSnippetResultWrapper implements SnippetResult {

    private final Map value;

    MapOfStringSnippetResultWrapper(Map value) {
      this.value = value;
    }

    public Map getValue() {
      return value;
    }

    static class Serializer extends JsonSerializer {

      @Override
      public void serialize(MapOfStringSnippetResultWrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeObject(value.getValue());
      }
    }
  }

  // SnippetResult as List wrapper.
  @JsonSerialize(using = ListOfSnippetResultWrapper.Serializer.class)
  class ListOfSnippetResultWrapper implements SnippetResult {

    private final List value;

    ListOfSnippetResultWrapper(List value) {
      this.value = value;
    }

    public List getValue() {
      return value;
    }

    static class Serializer extends JsonSerializer {

      @Override
      public void serialize(ListOfSnippetResultWrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeObject(value.getValue());
      }
    }
  }

  class Deserializer extends JsonDeserializer {

    private static final Logger LOGGER = Logger.getLogger(Deserializer.class.getName());

    @Override
    public SnippetResult deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
      JsonNode tree = jp.readValueAsTree();
      // deserialize SnippetResultOption
      if (tree.isObject() && tree.has("matchLevel")) {
        try (JsonParser parser = tree.traverse(jp.getCodec())) {
          return parser.readValueAs(SnippetResultOption.class);
        } catch (Exception e) {
          // deserialization failed, continue
          LOGGER.finest("Failed to deserialize oneOf SnippetResultOption (error: " + e.getMessage() + ") (type: SnippetResultOption)");
        }
      }
      // deserialize Map
      if (tree.isObject()) {
        try (JsonParser parser = tree.traverse(jp.getCodec())) {
          Map value = parser.readValueAs(new TypeReference>() {});
          return new SnippetResult.MapOfStringSnippetResultWrapper(value);
        } catch (Exception e) {
          // deserialization failed, continue
          LOGGER.finest(
            "Failed to deserialize oneOf Map (error: " + e.getMessage() + ") (type: Map)"
          );
        }
      }
      // deserialize List
      if (tree.isArray()) {
        try (JsonParser parser = tree.traverse(jp.getCodec())) {
          List value = parser.readValueAs(new TypeReference>() {});
          return new SnippetResult.ListOfSnippetResultWrapper(value);
        } catch (Exception e) {
          // deserialization failed, continue
          LOGGER.finest("Failed to deserialize oneOf List (error: " + e.getMessage() + ") (type: List)");
        }
      }
      throw new AlgoliaRuntimeException(String.format("Failed to deserialize json element: %s", tree));
    }

    /** Handle deserialization of the 'null' value. */
    @Override
    public SnippetResult getNullValue(DeserializationContext ctxt) throws JsonMappingException {
      throw new JsonMappingException(ctxt.getParser(), "SnippetResult cannot be null");
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy