Search in sources :

Example 21 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project snow-owl by b2ihealthcare.

the class ConceptPropertyDeserializer method deserialize.

@Override
public ConceptProperty<?> deserialize(JsonParser parser, DeserializationContext arg1) throws IOException, JsonProcessingException {
    TreeNode node = parser.readValueAsTree();
    ObjectCodec objectCodec = parser.getCodec();
    Iterator<String> fieldNames = node.fieldNames();
    PropertyType[] propertyTypes = PropertyType.values();
    PropertyType propertyType = null;
    while (fieldNames.hasNext()) {
        String fieldName = (String) fieldNames.next();
        if (fieldName.startsWith(VALUE_PREFIX)) {
            String type = fieldName.replace(VALUE_PREFIX, "");
            propertyType = Sets.newHashSet(propertyTypes).stream().filter(t -> t.getDisplayName().equalsIgnoreCase(type)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown property type '" + fieldName + "'."));
            break;
        }
    }
    if (propertyType == null) {
        throw new IllegalArgumentException("Invalid property type with null value.");
    }
    switch(propertyType) {
        case CODING:
            return objectCodec.treeToValue(node, CodingConceptProperty.class);
        case CODE:
            return objectCodec.treeToValue(node, CodeConceptProperty.class);
        case DATETIME:
            return objectCodec.treeToValue(node, DateTimeConceptProperty.class);
        case STRING:
            return objectCodec.treeToValue(node, StringConceptProperty.class);
        case BOOLEAN:
            return objectCodec.treeToValue(node, BooleanConceptProperty.class);
        case DECIMAL:
            return objectCodec.treeToValue(node, DecimalConceptProperty.class);
        case INTEGER:
            return objectCodec.treeToValue(node, IntegerConceptProperty.class);
        default:
            throw new IllegalArgumentException("Unsupported property type '" + propertyType + "'.");
    }
}
Also used : StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeNode(com.fasterxml.jackson.core.TreeNode) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Iterator(java.util.Iterator) JsonParser(com.fasterxml.jackson.core.JsonParser) PropertyType(com.b2international.snowowl.fhir.core.codesystems.PropertyType) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) Sets(com.google.common.collect.Sets) TreeNode(com.fasterxml.jackson.core.TreeNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) PropertyType(com.b2international.snowowl.fhir.core.codesystems.PropertyType)

Example 22 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project snow-owl by b2ihealthcare.

the class ExtensionDeserializer method deserialize.

@Override
public Extension<?> deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException {
    TreeNode node = parser.readValueAsTree();
    ObjectCodec objectCodec = parser.getCodec();
    Iterator<String> fieldNames = node.fieldNames();
    ExtensionType[] extensionTypes = ExtensionType.values();
    ExtensionType extensionType = null;
    while (fieldNames.hasNext()) {
        String fieldName = (String) fieldNames.next();
        if (fieldName.startsWith(VALUE_PREFIX)) {
            String type = fieldName.replace(VALUE_PREFIX, "");
            extensionType = Sets.newHashSet(extensionTypes).stream().filter(t -> t.getDisplayName().equalsIgnoreCase(type)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown extension type '" + fieldName + "'."));
            break;
        }
    }
    if (extensionType == null) {
        throw new IllegalArgumentException("Invalid extension with null value type.");
    }
    switch(extensionType) {
        case INTEGER:
            return objectCodec.treeToValue(node, IntegerExtension.class);
        case STRING:
            return objectCodec.treeToValue(node, StringExtension.class);
        default:
            throw new IllegalArgumentException("Unsupported extension type '" + extensionType + "'.");
    }
}
Also used : ExtensionType(com.b2international.snowowl.fhir.core.codesystems.ExtensionType) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeNode(com.fasterxml.jackson.core.TreeNode) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Iterator(java.util.Iterator) JsonParser(com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) Sets(com.google.common.collect.Sets) TreeNode(com.fasterxml.jackson.core.TreeNode) ExtensionType(com.b2international.snowowl.fhir.core.codesystems.ExtensionType) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec)

Example 23 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project cyclops by aol.

the class IterableXDeserializer method deserialize.

@Override
public IterableX<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    if (!p.isExpectedStartArrayToken()) {
        return (IterableX) ctxt.handleUnexpectedToken(handledType(), p);
    }
    List multi = new ArrayList();
    JsonToken t;
    while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
        Object value;
        if (t == JsonToken.VALUE_NULL) {
            value = null;
        } else if (typeDeserializerForValue == null) {
            value = valueDeserializer.deserialize(p, ctxt);
        } else {
            value = valueDeserializer.deserializeWithType(p, ctxt, typeDeserializerForValue);
        }
        multi.add(value);
    }
    if (Vector.class.isAssignableFrom(elementType))
        return Vector.fromIterable(multi);
    if (Seq.class.isAssignableFrom(elementType))
        return Seq.fromIterable(multi);
    if (LazySeq.class.isAssignableFrom(elementType))
        return LazySeq.fromIterable(multi);
    if (LazyString.class.isAssignableFrom(elementType))
        return LazyString.fromLazySeq((LazySeq) LazySeq.fromIterable(multi));
    if (IntMap.class.isAssignableFrom(elementType))
        return IntMap.fromIterable(multi);
    if (ReactiveSeq.class.isAssignableFrom(elementType))
        return ReactiveSeq.fromIterable(multi);
    if (Streamable.class.isAssignableFrom(elementType))
        return Streamable.fromIterable(multi);
    if (BankersQueue.class.isAssignableFrom(elementType))
        return BankersQueue.fromIterable(multi);
    if (Bag.class.isAssignableFrom(elementType))
        return Bag.fromIterable(multi);
    if (cyclops.data.HashSet.class.isAssignableFrom(elementType))
        return HashSet.fromIterable(multi);
    if (cyclops.data.TrieSet.class.isAssignableFrom(elementType))
        return TrieSet.fromIterable(multi);
    if (cyclops.data.TreeSet.class.isAssignableFrom(elementType))
        return TreeSet.fromIterable(multi, (Comparator) Comparator.naturalOrder());
    Optional<Method> m = streamMethod.computeIfAbsent(elementType, c -> Stream.of(c.getMethods()).filter(method -> "fromIterable".equals(method.getName())).filter(method -> method.getParameterCount() == 1).findFirst().map(m2 -> {
        m2.setAccessible(true);
        return m2;
    }));
    IterableX x = m.map(mt -> (IterableX) new Invoker().executeMethod(multi, mt, itX)).orElse(null);
    return x;
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) ArrayList(java.util.ArrayList) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) Map(java.util.Map) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Method(java.lang.reflect.Method) ConstantCallSite(java.lang.invoke.ConstantCallSite) CollectionLikeType(com.fasterxml.jackson.databind.type.CollectionLikeType) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) ContextualDeserializer(com.fasterxml.jackson.databind.deser.ContextualDeserializer) JsonParser(com.fasterxml.jackson.core.JsonParser) AsArrayTypeDeserializer(com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer) cyclops.data(cyclops.data) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) MethodHandles(java.lang.invoke.MethodHandles) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Stream(java.util.stream.Stream) CallSite(java.lang.invoke.CallSite) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) Optional(java.util.Optional) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectArrayDeserializer(com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer) Comparator(java.util.Comparator) Streamable(cyclops.companion.Streamable) cyclops.data(cyclops.data) IterableX(com.oath.cyclops.types.traversable.IterableX) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JsonToken(com.fasterxml.jackson.core.JsonToken) Method(java.lang.reflect.Method) Comparator(java.util.Comparator)

Example 24 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project cyclops by aol.

the class PersistentMapDeserializer method deserialize.

@Override
public PersistentMap<?, ?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonDeserializer deser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructRawMapType(Map.class));
    Object o = deser.deserialize(p, ctxt);
    if (HashMap.class.isAssignableFrom(mapType))
        return HashMap.fromMap((Map) o);
    if (TreeMap.class.isAssignableFrom(mapType))
        return TreeMap.fromMap((Comparator) Comparator.naturalOrder(), (Map) o);
    /**
     * if(TrieMap.class.isAssignableFrom(mapType))
     *      return TrieMap.fromMap((Map)o);
     *    if(LinkedMap.class.isAssignableFrom(mapType))
     *      return LinkedMap.fromMap((Map)o);
     */
    Optional<Method> m = streamMethod.computeIfAbsent(mapType, c -> Stream.of(c.getMethods()).filter(method -> "fromMap".equals(method.getName())).filter(method -> method.getParameterCount() == 1).filter(method -> method.getParameterTypes()[0].isAssignableFrom(Map.class)).findFirst().map(m2 -> {
        m2.setAccessible(true);
        return m2;
    }));
    PersistentMap<?, ?> x = m.map(mt -> (PersistentMap) new Invoker().executeMethod(o, mt, mapType)).orElse(null);
    return x;
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) JsonParser(com.fasterxml.jackson.core.JsonParser) HashMap(cyclops.data.HashMap) MethodHandles(java.lang.invoke.MethodHandles) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeMap(cyclops.data.TreeMap) Stream(java.util.stream.Stream) CallSite(java.lang.invoke.CallSite) Map(java.util.Map) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) Optional(java.util.Optional) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Comparator(java.util.Comparator) Method(java.lang.reflect.Method) ConstantCallSite(java.lang.invoke.ConstantCallSite) PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) Method(java.lang.reflect.Method) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) PersistentMap(com.oath.cyclops.types.persistent.PersistentMap) HashMap(cyclops.data.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(cyclops.data.TreeMap) Map(java.util.Map) Comparator(java.util.Comparator)

Example 25 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project incubator-servicecomb-java-chassis by apache.

the class ArgWrapperJavaType method readValue.

public Map<String, Object> readValue(ObjectMapper mapper, String json) throws IOException {
    Map<String, Object> args = new LinkedHashMap<>();
    JsonParser jp = mapper.getFactory().createParser(json);
    DeserializationContext deserializationContext = ObjectMapperUtils.createDeserializationContext(mapper, jp);
    jp.nextToken();
    for (String fieldName = jp.nextFieldName(); fieldName != null; fieldName = jp.nextFieldName()) {
        jp.nextToken();
        ArgInfo argInfo = argInfos.get(fieldName);
        if (argInfo == null) {
            continue;
        }
        if (argInfo.deserializer == null) {
            argInfo.deserializer = deserializationContext.findRootValueDeserializer(argInfo.javaType);
        }
        args.put(fieldName, argInfo.deserializer.deserialize(jp, deserializationContext));
    }
    return args;
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) LinkedHashMap(java.util.LinkedHashMap) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)28 JsonParser (com.fasterxml.jackson.core.JsonParser)21 IOException (java.io.IOException)17 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)7 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)7 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)6 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)6 Map (java.util.Map)6 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)5 List (java.util.List)5 ObjectNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Iterator (java.util.Iterator)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3