Search in sources :

Example 26 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project pinpoint by naver.

the class ObjectMapperJDK7IT method testReadValue.

@SuppressWarnings("deprecation")
@Test
public void testReadValue() throws Exception {
    String json_str = "{\"name\" : \"Jackson\"}";
    byte[] json_b = json_str.getBytes(UTF_8);
    mapper.readValue(json_str, __POJO.class);
    mapper.readValue(json_b, __POJO.class);
    ObjectReader reader = mapper.reader(__POJO.class);
    reader.readValue(json_str);
    reader.readValue(json_b);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    Method mapperReadValueString = ObjectMapper.class.getMethod("readValue", String.class, Class.class);
    Method mapperReadValueBytes = ObjectMapper.class.getMethod("readValue", byte[].class, Class.class);
    Method readerReadValueString = ObjectReader.class.getMethod("readValue", String.class);
    Method readerReadValueBytes = ObjectReader.class.getMethod("readValue", byte[].class);
    verifier.verifyTrace(event(SERVICE_TYPE, mapperReadValueString, annotation(ANNOTATION_KEY, json_str.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, mapperReadValueBytes, annotation(ANNOTATION_KEY, json_b.length)));
    verifier.verifyTrace(event(SERVICE_TYPE, readerReadValueString, annotation(ANNOTATION_KEY, json_str.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, readerReadValueBytes, annotation(ANNOTATION_KEY, json_b.length)));
    verifier.verifyTraceCount(0);
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 27 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project pinpoint by naver.

the class ObjectReaderIT method testReadValue.

@Test
public void testReadValue() throws Exception {
    String json_str = "{\"name\" : \"Jackson\"}";
    byte[] json_b = json_str.getBytes(UTF_8);
    ObjectReader reader = mapper.reader(__POJO.class);
    __POJO pojo = reader.readValue(json_str);
    pojo = reader.readValue(json_b);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    Method readval1 = ObjectReader.class.getMethod("readValue", String.class);
    Method readval2 = ObjectReader.class.getMethod("readValue", byte[].class);
    verifier.verifyTrace(event("JACKSON", readval1, Expectations.annotation("jackson.json.length", json_str.length())));
    verifier.verifyTrace(event("JACKSON", readval2, Expectations.annotation("jackson.json.length", json_b.length)));
    verifier.verifyTraceCount(0);
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 28 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project hazelcast by hazelcast.

the class CsvInputFormat method createRecordReader.

@Override
public RecordReader<NullWritable, Object> createRecordReader(InputSplit split, TaskAttemptContext context) {
    return new RecordReader<NullWritable, Object>() {

        private Object current;

        private MappingIterator<Object> iterator;

        private Function<Object, Object> projection = identity();

        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public void initialize(InputSplit split, TaskAttemptContext context) throws IOException {
            FileSplit fileSplit = (FileSplit) split;
            Configuration conf = context.getConfiguration();
            Configuration configuration = context.getConfiguration();
            String className = configuration.get(CSV_INPUT_FORMAT_BEAN_CLASS);
            Class<?> formatClazz = className == null ? null : ReflectionUtils.loadClass(className);
            Path file = fileSplit.getPath();
            FileSystem fs = file.getFileSystem(conf);
            FSDataInputStream in = fs.open(file);
            if (formatClazz == String[].class) {
                ObjectReader reader = new CsvMapper().enable(Feature.WRAP_AS_ARRAY).readerFor(String[].class).with(CsvSchema.emptySchema().withSkipFirstDataRow(false));
                iterator = reader.readValues((InputStream) in);
                if (!iterator.hasNext()) {
                    throw new JetException("Header row missing in " + split);
                }
                String[] header = (String[]) iterator.next();
                List<String> fieldNames = new ArrayList<>();
                String field;
                for (int i = 0; (field = configuration.get(CSV_INPUT_FORMAT_FIELD_LIST_PREFIX + i)) != null; i++) {
                    fieldNames.add(field);
                }
                projection = (Function) createFieldProjection(header, fieldNames);
            } else {
                iterator = new CsvMapper().readerFor(formatClazz).withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).with(CsvSchema.emptySchema().withHeader()).readValues((InputStream) in);
            }
        }

        @Override
        public boolean nextKeyValue() {
            if (!iterator.hasNext()) {
                return false;
            }
            current = projection.apply(iterator.next());
            return true;
        }

        @Override
        public NullWritable getCurrentKey() {
            return NullWritable.get();
        }

        @Override
        public Object getCurrentValue() {
            return current;
        }

        @Override
        public float getProgress() {
            return 0;
        }

        @Override
        public void close() throws IOException {
            iterator.close();
        }
    };
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) RecordReader(org.apache.hadoop.mapreduce.RecordReader) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) TaskAttemptContext(org.apache.hadoop.mapreduce.TaskAttemptContext) JetException(com.hazelcast.jet.JetException) FileSplit(org.apache.hadoop.mapreduce.lib.input.FileSplit) Function(java.util.function.Function) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) FileSystem(org.apache.hadoop.fs.FileSystem) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) InputSplit(org.apache.hadoop.mapreduce.InputSplit)

Example 29 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project hazelcast by hazelcast.

the class CsvReadFileFnProvider method createReadFileFn.

@SuppressWarnings("unchecked")
@Nonnull
@Override
public <T> FunctionEx<Path, Stream<T>> createReadFileFn(@Nonnull FileFormat<T> format) {
    CsvFileFormat<T> csvFileFormat = (CsvFileFormat<T>) format;
    // Format is not Serializable
    Class<?> formatClazz = csvFileFormat.clazz();
    return path -> {
        FileInputStream fis = new FileInputStream(path.toFile());
        MappingIterator<T> iterator;
        Function<T, T> projection = identity();
        if (formatClazz == String[].class) {
            ObjectReader reader = new CsvMapper().enable(Feature.WRAP_AS_ARRAY).readerFor(String[].class).with(CsvSchema.emptySchema().withSkipFirstDataRow(false));
            iterator = reader.readValues(fis);
            if (!iterator.hasNext()) {
                throw new JetException("Header row missing in " + path);
            }
            String[] header = (String[]) iterator.next();
            List<String> fieldNames = csvFileFormat.fieldNames();
            if (fieldNames != null) {
                projection = (Function<T, T>) createFieldProjection(header, fieldNames);
            }
        } else {
            iterator = new CsvMapper().readerFor(formatClazz).withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).with(CsvSchema.emptySchema().withHeader()).readValues(fis);
        }
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, ORDERED), false).map(projection).onClose(() -> uncheckRun(fis::close));
    };
}
Also used : FunctionEx(com.hazelcast.function.FunctionEx) Util.uncheckRun(com.hazelcast.jet.impl.util.Util.uncheckRun) Spliterators(java.util.Spliterators) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) ORDERED(java.util.Spliterator.ORDERED) FileInputStream(java.io.FileInputStream) Function(java.util.function.Function) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JetException(com.hazelcast.jet.JetException) CsvFileFormat(com.hazelcast.jet.pipeline.file.CsvFileFormat) Feature(com.fasterxml.jackson.dataformat.csv.CsvParser.Feature) FileFormat(com.hazelcast.jet.pipeline.file.FileFormat) List(java.util.List) Stream(java.util.stream.Stream) Util.createFieldProjection(com.hazelcast.jet.impl.util.Util.createFieldProjection) ReadFileFnProvider(com.hazelcast.jet.pipeline.file.impl.ReadFileFnProvider) Function.identity(java.util.function.Function.identity) StreamSupport(java.util.stream.StreamSupport) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) JetException(com.hazelcast.jet.JetException) FileInputStream(java.io.FileInputStream) Function(java.util.function.Function) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) List(java.util.List) CsvFileFormat(com.hazelcast.jet.pipeline.file.CsvFileFormat) Nonnull(javax.annotation.Nonnull)

Example 30 with ObjectReader

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project native-navigation by airbnb.

the class ConversionUtil method toType.

/**
 * Converts the provided {@code readableMap} into an object of the provided {@code targetType}
 */
static <T> T toType(ObjectMapper objectMapper, ReadableMap readableMap, Class<T> targetType) {
    ObjectNode jsonNode = toJsonObject(readableMap);
    ObjectReader objectReader = JacksonUtils.readerForType(objectMapper, targetType);
    // noinspection OverlyBroadCatchBlock
    try {
        return objectReader.readValue(jsonNode);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException)

Aggregations

ObjectReader (com.fasterxml.jackson.databind.ObjectReader)83 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)33 IOException (java.io.IOException)32 Test (org.junit.Test)23 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 ArrayList (java.util.ArrayList)8 JavaType (com.fasterxml.jackson.databind.JavaType)7 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)4 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)4 Method (java.lang.reflect.Method)4 Collectors (java.util.stream.Collectors)4 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)3 JSONLayoutPage (org.knime.js.core.layout.bs.JSONLayoutPage)3