Example usage for com.fasterxml.jackson.core JsonFactory createParser

List of usage examples for com.fasterxml.jackson.core JsonFactory createParser

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory createParser.

Prototype

public JsonParser createParser(String content) throws IOException, JsonParseException 

Source Link

Document

Method for constructing parser for parsing contents of given String.

Usage

From source file:org.sead.repositories.reference.RefRepository.java

protected static void generateIndex(InputStream ro, File descFile, File indexFile)
        throws JsonParseException, IOException {

    log.debug("Generating desc and index files");
    JsonFactory f = new MappingJsonFactory(); // reading
    JsonParser jp = f.createParser(ro);

    JsonGenerator generator = new JsonFactory().createGenerator(descFile, JsonEncoding.UTF8);

    JsonToken current;//  ww w. ja va  2 s . co  m

    current = jp.nextToken();

    report(jp, current);
    while ((current = jp.nextToken()) != null) {
        if (current.equals(JsonToken.FIELD_NAME)) {
            String fName = jp.getText();
            if (fName.equals("describes")) {
                log.trace("describes");
                while (((current = jp.nextToken()) != null)) {
                    if (jp.isExpectedStartObjectToken()) {
                        generator.setCodec(new ObjectMapper());
                        generator.useDefaultPrettyPrinter();

                        generator.writeStartObject();

                        while (((current = jp.nextToken()) != JsonToken.END_OBJECT)) {
                            if (current != JsonToken.FIELD_NAME) {
                                log.warn("Unexpected Token!");
                                report(jp, current);

                            } else {
                                report(jp, current);
                                String name = jp.getText();
                                current = jp.nextToken(); // Get to start of
                                // value
                                if (!name.equals("aggregates")) {
                                    log.trace("Writing: " + name);
                                    generator.writeFieldName(name);
                                    generator.writeTree(jp.readValueAsTree());
                                } else {
                                    report(jp, current);
                                    log.trace("Skipping?");
                                    if (current.isStructStart()) {
                                        indexChildren(indexFile, jp);
                                        // jp.skipChildren();
                                    } else {
                                        log.warn("Was Not Struct start!");
                                    }
                                    log.trace("Hit aggregates");

                                }
                            }
                        }

                        generator.writeEndObject();

                        generator.close();
                    }
                }
            }
        }
    }
}

From source file:org.talend.components.localio.runtime.fixed.FixedDatasetRuntime.java

public List<IndexedRecord> getValues(int limit) {
    List<IndexedRecord> values = new ArrayList<>();
    switch (properties.format.getValue()) {
    case CSV://  w w w  . ja v  a  2  s  . c o m
        try {
            CsvRecordToIndexedRecordConverter converter = new CsvRecordToIndexedRecordConverter(getSchema());
            for (CSVRecord r : CSVFormat.RFC4180 //
                    .withDelimiter(properties.getFieldDelimiter().charAt(0)) //
                    .withRecordSeparator(properties.getRecordDelimiter())
                    .parse(new StringReader(properties.values.getValue())))
                values.add(converter.convertToAvro(r));
        } catch (IOException e) {
            throw LocalIOErrorCode.createCannotParseSchema(e, properties.values.getValue());
        }
        break;
    case JSON:
        ObjectMapper mapper = new ObjectMapper();
        JsonSchemaInferrer jsonSchemaInferrer = new JsonSchemaInferrer(mapper);
        JsonGenericRecordConverter converter = null;
        JsonFactory jsonFactory = new JsonFactory();
        try (StringReader r = new StringReader(properties.values.getValue())) {
            Iterator<JsonNode> value = mapper.readValues(jsonFactory.createParser(r), JsonNode.class);
            int count = 0;
            while (value.hasNext() && count++ < limit) {
                String json = value.next().toString();
                if (converter == null) {
                    Schema jsonSchema = jsonSchemaInferrer.inferSchema(json);
                    converter = new JsonGenericRecordConverter(jsonSchema);
                }
                values.add(converter.convertToAvro(json));
            }
        } catch (IOException e) {
            throw LocalIOErrorCode.createCannotParseJson(e, properties.schema.getValue(),
                    properties.values.getValue());
        }
        break;
    case AVRO:
        Schema schema = getSchema();
        if (isRandom()) {
            GeneratorFunction<IndexedRecord> gf = (GeneratorFunction<IndexedRecord>) GeneratorFunctions
                    .of(getSchema());
            GeneratorFunction.GeneratorContext ctx = GeneratorFunction.GeneratorContext.of(0, 0L);
            for (int i = 0; i < limit; i++) {
                ctx.setRowId(i);
                values.add(gf.apply(ctx));
            }
        } else {
            try (ByteArrayInputStream bais = new ByteArrayInputStream(
                    properties.values.getValue().trim().getBytes())) {
                JsonDecoder decoder = DecoderFactory.get().jsonDecoder(schema, bais);
                DatumReader<IndexedRecord> reader = new GenericDatumReader<>(schema);
                int count = 0;
                while (count++ < limit) {
                    values.add(reader.read(null, decoder));
                }
            } catch (EOFException e) {
                // Indicates the end of the values.
            } catch (IOException e) {
                throw LocalIOErrorCode.createCannotParseAvroJson(e, properties.schema.getValue(),
                        properties.values.getValue());
            }
        }
        break;
    }
    return values;
}