content: /*
   * Transfer webhooks
   *
   * The version of the OpenAPI document: 4
   *
   *
   * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
   * https://openapi-generator.tech
   * Do not edit the class manually.
   */
  
  package com.adyen.model.transferwebhooks;
  
  import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.core.JsonParser;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.core.JsonToken;
  import com.fasterxml.jackson.databind.DeserializationContext;
  import com.fasterxml.jackson.databind.JsonMappingException;
  import com.fasterxml.jackson.databind.JsonNode;
  import com.fasterxml.jackson.databind.MapperFeature;
  import com.fasterxml.jackson.databind.SerializerProvider;
  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
  import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  import jakarta.ws.rs.core.GenericType;
  import java.io.IOException;
  import java.util.*;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  @JsonDeserialize(
      using = TransferEventEventsDataInner.TransferEventEventsDataInnerDeserializer.class)
  @JsonSerialize(using = TransferEventEventsDataInner.TransferEventEventsDataInnerSerializer.class)
  public class TransferEventEventsDataInner extends AbstractOpenApiSchema {
    private static final Logger log = Logger.getLogger(TransferEventEventsDataInner.class.getName());
  
    public static class TransferEventEventsDataInnerSerializer
        extends StdSerializer<TransferEventEventsDataInner> {
      public TransferEventEventsDataInnerSerializer(Class<TransferEventEventsDataInner> t) {
        super(t);
      }
  
      public TransferEventEventsDataInnerSerializer() {
        this(null);
      }
  
      @Override
      public void serialize(
          TransferEventEventsDataInner value, JsonGenerator jgen, SerializerProvider provider)
          throws IOException, JsonProcessingException {
        jgen.writeObject(value.getActualInstance());
      }
    }
  
    public static class TransferEventEventsDataInnerDeserializer
        extends StdDeserializer<TransferEventEventsDataInner> {
      public TransferEventEventsDataInnerDeserializer() {
        this(TransferEventEventsDataInner.class);
      }
  
      public TransferEventEventsDataInnerDeserializer(Class<?> vc) {
        super(vc);
      }
  
      @Override
      public TransferEventEventsDataInner deserialize(JsonParser jp, DeserializationContext ctxt)
          throws IOException, JsonProcessingException {
        JsonNode tree = jp.readValueAsTree();
        Object deserialized = null;
        boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS);
        int match = 0;
        JsonToken token = tree.traverse(jp.getCodec()).nextToken();
        // deserialize IssuingTransactionData
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(IssuingTransactionData.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(IssuingTransactionData.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'IssuingTransactionData'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'IssuingTransactionData'", e);
        }
  
        // deserialize MerchantPurchaseData
        try {
          boolean attemptParsing = true;
          if (attemptParsing) {
            // Checks if the unique type of the oneOf json matches any of the object TypeEnum values
            boolean typeMatch = false;
            if (tree.findValue("type") != null) {
              typeMatch =
                  Arrays.stream(MerchantPurchaseData.TypeEnum.values())
                      .anyMatch((t) -> t.getValue().equals(tree.findValue("type").asText()));
            }
  
            if (typeMatch) {
              deserialized = tree.traverse(jp.getCodec()).readValueAs(MerchantPurchaseData.class);
              // TODO: there is no validation against JSON schema constraints
              // (min, max, enum, pattern...), this does not perform a strict JSON
              // validation, which means the 'match' count may be higher than it should be.
              match++;
              log.log(Level.FINER, "Input data matches schema 'MerchantPurchaseData'");
            }
          }
        } catch (Exception e) {
          // deserialization failed, continue
          log.log(Level.FINER, "Input data does not match schema 'MerchantPurchaseData'", e);
        }
  
        if (match == 1) {
          TransferEventEventsDataInner ret = new TransferEventEventsDataInner();
          ret.setActualInstance(deserialized);
          return ret;
        }
        throw new IOException(
            String.format(
                "Failed deserialization for TransferEventEventsDataInner: %d classes match result, expected 1",
                match));
      }
  
      /** Handle deserialization of the 'null' value. */
      @Override
      public TransferEventEventsDataInner getNullValue(DeserializationContext ctxt)
          throws JsonMappingException {
        throw new JsonMappingException(
            ctxt.getParser(), "TransferEventEventsDataInner cannot be null");
      }
    }
  
    // store a list of schema names defined in oneOf
    public static final Map<String, GenericType<?>> schemas = new HashMap<>();
  
    public TransferEventEventsDataInner() {
      super("oneOf", Boolean.FALSE);
    }
  
    public TransferEventEventsDataInner(IssuingTransactionData o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    public TransferEventEventsDataInner(MerchantPurchaseData o) {
      super("oneOf", Boolean.FALSE);
      setActualInstance(o);
    }
  
    static {
      schemas.put("IssuingTransactionData", new GenericType<IssuingTransactionData>() {});
      schemas.put("MerchantPurchaseData", new GenericType<MerchantPurchaseData>() {});
      JSON.registerDescendants(
          TransferEventEventsDataInner.class, Collections.unmodifiableMap(schemas));
    }
  
    @Override
    public Map<String, GenericType<?>> getSchemas() {
      return TransferEventEventsDataInner.schemas;
    }
  
    /**
     * Set the instance that matches the oneOf child schema, check the instance parameter is valid
     * against the oneOf child schemas: IssuingTransactionData, MerchantPurchaseData
     *
     * <p>It could be an instance of the 'oneOf' schemas. The oneOf child schemas may themselves be a
     * composed schema (allOf, anyOf, oneOf).
     */
    @Override
    public void setActualInstance(Object instance) {
      if (JSON.isInstanceOf(IssuingTransactionData.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      if (JSON.isInstanceOf(MerchantPurchaseData.class, instance, new HashSet<>())) {
        super.setActualInstance(instance);
        return;
      }
  
      throw new RuntimeException(
          "Invalid instance type. Must be IssuingTransactionData, MerchantPurchaseData");
    }
  
    /**
     * Get the actual instance, which can be the following: IssuingTransactionData,
     * MerchantPurchaseData
     *
     * @return The actual instance (IssuingTransactionData, MerchantPurchaseData)
     */
    @Override
    public Object getActualInstance() {
      return super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `IssuingTransactionData`. If the actual instance is not
     * `IssuingTransactionData`, the ClassCastException will be thrown.
     *
     * @return The actual instance of `IssuingTransactionData`
     * @throws ClassCastException if the instance is not `IssuingTransactionData`
     */
    public IssuingTransactionData getIssuingTransactionData() throws ClassCastException {
      return (IssuingTransactionData) super.getActualInstance();
    }
  
    /**
     * Get the actual instance of `MerchantPurchaseData`. If the actual instance is not
     * `MerchantPurchaseData`, the ClassCastException will be thrown.
     *
     * @return The actual instance of `MerchantPurchaseData`
     * @throws ClassCastException if the instance is not `MerchantPurchaseData`
     */
    public MerchantPurchaseData getMerchantPurchaseData() throws ClassCastException {
      return (MerchantPurchaseData) super.getActualInstance();
    }
  
    /**
     * Create an instance of TransferEventEventsDataInner given an JSON string
     *
     * @param jsonString JSON string
     * @return An instance of TransferEventEventsDataInner
     * @throws IOException if the JSON string is invalid with respect to TransferEventEventsDataInner
     */
    public static TransferEventEventsDataInner fromJson(String jsonString) throws IOException {
      return JSON.getMapper().readValue(jsonString, TransferEventEventsDataInner.class);
    }
  
    /**
     * Convert an instance of TransferEventEventsDataInner to an JSON string
     *
     * @return JSON string
     */
    public String toJson() throws JsonProcessingException {
      return JSON.getMapper().writeValueAsString(this);
    }
  }
  