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

io.github.emm035.openapi.immutables.v3.examples.ExampleDeserializer Maven / Gradle / Ivy

package io.github.emm035.openapi.immutables.v3.examples;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;

import java.io.IOException;
import java.util.Map;

class ExampleDeserializer extends JsonDeserializer {

  private static final Map> MAPPINGS = ImmutableMap.of(
    "value", ValueExample.class,
    "externalValue", ExternalExample.class
  );

  @Override
  public Example deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) p.getCodec();
    JsonNode node = mapper.readTree(p);

    if (node.has("value")) {
      return mapper.treeToValue(node, ValueExample.class);
    }
    if (node.has("externalValue")) {
      return mapper.treeToValue(node, ExternalExample.class);
    }
    throw ctxt.instantiationException(Example.class, "Unable to instantiate any example type from " + node.toString());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy