Search in sources :

Example 51 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project curiostack by curioswitch.

the class ParseSupport method parseObjectStart.

/**
 * Checks the current token is '{' and advances past it. For parsing the beginning of JSON objects
 * ({@link Message}s and {@link java.util.Map}s).
 */
public static void parseObjectStart(JsonParser parser) throws IOException {
    JsonToken json = parser.currentToken();
    if (json != JsonToken.START_OBJECT) {
        throw new InvalidProtocolBufferException("Expected an object but found: " + json);
    }
    parser.nextToken();
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 52 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project curiostack by curioswitch.

the class ParseSupport method parseString.

/**
 * Parsers a string value out of the input.
 */
public static String parseString(JsonParser parser) throws IOException {
    JsonToken json = parser.currentToken();
    String result = null;
    try {
        result = parser.getValueAsString();
    } catch (IOException e) {
    // Fall through
    }
    if (result == null) {
        throw new InvalidProtocolBufferException("Not a string value: " + json);
    }
    return result;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonToken(com.fasterxml.jackson.core.JsonToken) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 53 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project cytoscape-impl by cytoscape.

the class CyTransformerReaderImpl method readCompositeFilter.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void readCompositeFilter(JsonParser parser, CompositeFilter composite) throws IOException {
    JsonToken firstToken = parser.nextToken();
    if (firstToken == JsonToken.END_OBJECT) {
        return;
    }
    if (firstToken != JsonToken.FIELD_NAME) {
        throw new IOException("Expected: " + JsonToken.FIELD_NAME + ". Got: " + firstToken);
    }
    assertEquals(FilterIO.TRANSFORMERS_FIELD, parser.getCurrentName());
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    while (true) {
        Filter filter = (Filter) readTransformer(parser);
        if (filter == null) {
            break;
        }
        composite.append(filter);
    }
    assertEquals(JsonToken.END_ARRAY, parser.getCurrentToken());
    assertNextToken(parser, JsonToken.END_OBJECT);
}
Also used : Filter(org.cytoscape.filter.model.Filter) CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException)

Example 54 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project cytoscape-impl by cytoscape.

the class CyTransformerReaderImpl method readTransformer.

private Transformer<?, ?> readTransformer(JsonParser parser) throws IOException {
    JsonToken firstToken = parser.nextToken();
    if (firstToken == JsonToken.END_ARRAY) {
        return null;
    }
    if (firstToken != JsonToken.START_OBJECT) {
        throw new IOException("Expected: " + JsonToken.START_OBJECT + ". Got: " + firstToken);
    }
    assertField(parser, FilterIO.ID_FIELD);
    String id = parser.nextTextValue();
    assertField(parser, FilterIO.PARAMETERS_FIELD);
    assertNextToken(parser, JsonToken.START_OBJECT);
    Transformer<?, ?> transformer = transformerManager.createTransformer(id);
    if (transformer == null && !id.contains(".")) {
        transformer = transformerManager.createTransformer("org.cytoscape." + id);
    }
    if (transformer == null) {
        throw new IOException("Unrecognized id: '" + id + "'");
    }
    Map<String, Object> parameters = readParameters(parser);
    assertEquals(JsonToken.END_OBJECT, parser.getCurrentToken());
    FilterIO.applyParameters(parameters, transformer);
    if (transformer instanceof CompositeFilter) {
        readCompositeFilter(parser, (CompositeFilter<?, ?>) transformer);
    } else if (transformer instanceof SubFilterTransformer) {
        SubFilterTransformer<?, ?> sft = (SubFilterTransformer<?, ?>) transformer;
        readCompositeFilter(parser, sft.getCompositeFilter());
    } else {
        assertNextToken(parser, JsonToken.END_OBJECT);
    }
    return transformer;
}
Also used : CompositeFilter(org.cytoscape.filter.model.CompositeFilter) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) SubFilterTransformer(org.cytoscape.filter.model.SubFilterTransformer)

Example 55 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project components by Talend.

the class DatasetWritePayload method createStreamToReadData.

private static Iterator<IndexedRecord> createStreamToReadData(JsonParser parser, ObjectMapper mapper, Schema schema, InputStream input) throws IOException {
    JsonToken dataFieldToken = parser.nextToken();
    isTrue(FIELD_NAME == dataFieldToken, invalidInputMessage(FIELD_NAME, dataFieldToken));
    isTrue(Objects.equals(DATA_FIELD_NAME, parser.getText()), invalidInputMessage(DATA_FIELD_NAME, parser.getText()));
    JsonToken dataArrayStartToken = parser.nextToken();
    isTrue(START_ARRAY == dataArrayStartToken, invalidInputMessage(START_ARRAY, dataArrayStartToken));
    JsonToken firstDataObjectStartToken = parser.nextToken();
    isTrue(START_OBJECT == firstDataObjectStartToken, invalidInputMessage(START_OBJECT, firstDataObjectStartToken));
    Iterator<ObjectNode> objectNodeIterator = parser.readValuesAs(ObjectNode.class);
    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    return new Iterator<IndexedRecord>() {

        @Override
        public boolean hasNext() {
            return objectNodeIterator.hasNext();
        }

        @Override
        public IndexedRecord next() {
            try {
                ObjectNode next = objectNodeIterator.next();
                return reader.read(null, DecoderFactory.get().jsonDecoder(schema, mapper.writeValueAsString(next)));
            } catch (IOException e) {
                throw new RuntimeException("Error while reading avro data.", e);
            }
        }
    };
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Iterator(java.util.Iterator) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)251 IOException (java.io.IOException)65 JsonParser (com.fasterxml.jackson.core.JsonParser)44 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 JsonFactory (com.fasterxml.jackson.core.JsonFactory)18 JsonParseException (com.fasterxml.jackson.core.JsonParseException)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 SqlNullable (com.facebook.presto.spi.function.SqlNullable)6 SqlType (com.facebook.presto.spi.function.SqlType)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 InputStream (java.io.InputStream)5 LinkedHashMap (java.util.LinkedHashMap)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ByteString (com.google.protobuf.ByteString)4 HashSet (java.util.HashSet)4 MappingJsonFactory (com.fasterxml.jackson.databind.MappingJsonFactory)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3