Search in sources :

Example 1 with CollectionType

use of com.fasterxml.jackson.databind.type.CollectionType in project camel by apache.

the class JacksonXMLDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
    // is there a header with the unmarshal type?
    Class<?> clazz = unmarshalType;
    String type = null;
    if (allowUnmarshallType) {
        type = exchange.getIn().getHeader(JacksonXMLConstants.UNMARSHAL_TYPE, String.class);
    }
    if (type == null && isAllowJmsType()) {
        type = exchange.getIn().getHeader("JMSType", String.class);
    }
    if (type != null) {
        clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type);
    }
    if (collectionType != null) {
        CollectionType collType = xmlMapper.getTypeFactory().constructCollectionType(collectionType, clazz);
        return this.xmlMapper.readValue(stream, collType);
    } else {
        return this.xmlMapper.readValue(stream, clazz);
    }
}
Also used : CollectionType(com.fasterxml.jackson.databind.type.CollectionType)

Example 2 with CollectionType

use of com.fasterxml.jackson.databind.type.CollectionType in project camel by apache.

the class JacksonDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
    // is there a header with the unmarshal type?
    Class<?> clazz = unmarshalType;
    String type = null;
    if (allowUnmarshallType) {
        type = exchange.getIn().getHeader(JacksonConstants.UNMARSHAL_TYPE, String.class);
    }
    if (type == null && isAllowJmsType()) {
        type = exchange.getIn().getHeader("JMSType", String.class);
    }
    if (type != null) {
        clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type);
    }
    if (collectionType != null) {
        CollectionType collType = objectMapper.getTypeFactory().constructCollectionType(collectionType, clazz);
        return this.objectMapper.readValue(stream, collType);
    } else {
        return this.objectMapper.readValue(stream, clazz);
    }
}
Also used : CollectionType(com.fasterxml.jackson.databind.type.CollectionType)

Example 3 with CollectionType

use of com.fasterxml.jackson.databind.type.CollectionType in project hadoop by apache.

the class SwiftNativeFileSystemStore method listDirectory.

/**
   * List a directory.
   * This is O(n) for the number of objects in this path.
   *
   *
   *
   * @param path working path
   * @param listDeep ask for all the data
   * @param newest ask for the newest data
   * @return Collection of file statuses
   * @throws IOException IO problems
   * @throws FileNotFoundException if the path does not exist
   */
private List<FileStatus> listDirectory(SwiftObjectPath path, boolean listDeep, boolean newest) throws IOException {
    final byte[] bytes;
    final ArrayList<FileStatus> files = new ArrayList<FileStatus>();
    final Path correctSwiftPath = getCorrectSwiftPath(path);
    try {
        bytes = swiftRestClient.listDeepObjectsInDirectory(path, listDeep);
    } catch (FileNotFoundException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("" + "File/Directory not found " + path);
        }
        if (SwiftUtils.isRootDir(path)) {
            return Collections.emptyList();
        } else {
            throw e;
        }
    } catch (SwiftInvalidResponseException e) {
        //bad HTTP error code
        if (e.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
            //this can come back on a root list if the container is empty
            if (SwiftUtils.isRootDir(path)) {
                return Collections.emptyList();
            } else {
                //NO_CONTENT returned on something other than the root directory;
                //see if it is there, and convert to empty list or not found
                //depending on whether the entry exists.
                FileStatus stat = getObjectMetadata(correctSwiftPath, newest);
                if (stat.isDirectory()) {
                    //it's an empty directory. state that
                    return Collections.emptyList();
                } else {
                    //it's a file -return that as the status
                    files.add(stat);
                    return files;
                }
            }
        } else {
            //a different status code: rethrow immediately
            throw e;
        }
    }
    final CollectionType collectionType = JSONUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, SwiftObjectFileStatus.class);
    final List<SwiftObjectFileStatus> fileStatusList = JSONUtil.toObject(new String(bytes, Charset.forName("UTF-8")), collectionType);
    //in this case swift will return empty array
    if (fileStatusList.isEmpty()) {
        SwiftFileStatus objectMetadata = getObjectMetadata(correctSwiftPath, newest);
        if (objectMetadata.isFile()) {
            files.add(objectMetadata);
        }
        return files;
    }
    for (SwiftObjectFileStatus status : fileStatusList) {
        if (status.getName() != null) {
            files.add(new SwiftFileStatus(status.getBytes(), status.getBytes() == 0, 1, getBlocksize(), status.getLast_modified().getTime(), getCorrectSwiftPath(new Path(status.getName()))));
        }
    }
    return files;
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) FileStatus(org.apache.hadoop.fs.FileStatus) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) SwiftInvalidResponseException(org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException)

Example 4 with CollectionType

use of com.fasterxml.jackson.databind.type.CollectionType in project moco by dreamhead.

the class CollectionReader method read.

public <T> ImmutableList<T> read(final InputStream is, final Class<T> elementClass) {
    try {
        CollectionType type = factory.constructCollectionType(List.class, elementClass);
        List<T> sessionSettings = mapper.readValue(is, type);
        return copyOf(sessionSettings);
    } catch (UnrecognizedPropertyException e) {
        logger.info("Unrecognized field: {}", e.getMessage());
        throw new RuntimeException(format("Unrecognized field [ %s ], please check!", e.getPropertyName()));
    } catch (JsonMappingException e) {
        logger.info("{} {}", e.getMessage(), e.getPathReference());
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(is);
    }
}
Also used : CollectionType(com.fasterxml.jackson.databind.type.CollectionType) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) IOException(java.io.IOException)

Example 5 with CollectionType

use of com.fasterxml.jackson.databind.type.CollectionType in project jackson-databind by FasterXML.

the class TestTypeResolution method testListViaTypeRef.

public void testListViaTypeRef() {
    TypeFactory tf = TypeFactory.defaultInstance();
    JavaType t = tf.constructType(new TypeReference<MyLongList<Integer>>() {
    });
    CollectionType type = (CollectionType) t;
    assertSame(MyLongList.class, type.getRawClass());
    assertEquals(tf.constructType(Long.class), type.getContentType());
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Aggregations

CollectionType (com.fasterxml.jackson.databind.type.CollectionType)7 JavaType (com.fasterxml.jackson.databind.JavaType)3 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)3 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 ArrayType (com.fasterxml.jackson.databind.type.ArrayType)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 Path (org.apache.hadoop.fs.Path)1 SwiftInvalidResponseException (org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException)1 SwiftObjectPath (org.apache.hadoop.fs.swift.util.SwiftObjectPath)1