Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
*/
package com.airbyte.api.utils;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.airbyte.api.utils.Utils.TypeReferenceWithShape;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class OneOfDeserializer extends StdDeserializer {
private static final long serialVersionUID = -1;
private final List typeReferences; // oneOf subschemas
private final Class cls;
private final ObjectMapper mapper;
protected OneOfDeserializer(Class cls, TypeReferenceWithShape... typeReferences) {
super(cls);
this.typeReferences= Arrays.asList(typeReferences);
this.cls = cls;
this.mapper = JSON.getMapper();
}
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return deserializeOneOf(mapper, p, ctxt, typeReferences, cls);
}
private static T deserializeOneOf(ObjectMapper mapper, JsonParser p, DeserializationContext ctxt,
List typeReferences, Class cls) throws IOException {
TreeNode tree = p.getCodec().readTree(p);
// TODO don't have to generate json because can use tree.traverse to get a
// parser to read value, perf advantage and can stop plugging in ObjectMapper
String json = mapper.writeValueAsString(tree);
return deserializeOneOf(mapper, json, typeReferences, cls, ctxt);
}
private static T deserializeOneOf(ObjectMapper mapper, String json, List typeReferences, Class cls,
DeserializationContext ctxt) throws JsonProcessingException {
List> matches = new ArrayList<>();
for (TypeReferenceWithShape c : typeReferences) {
// try to deserialize with each of the member classes
// @formatter:off
try {
JavaType jt = Utils.convertToShape(mapper.getTypeFactory(), c.typeReference(), c.shape());
// Jackson very permissive with readValue so we will tighten things up a bit
if (matchPossible(jt, json)) {
Object o = mapper.readValue(json, jt);
o = Utils.convertToShapeInverse(o, c.shape(), jt);
@SuppressWarnings("unchecked")
TypedObject typed = TypedObject.of(o, c.shape(), (TypeReference