Java Code Examples for com.fasterxml.jackson.core.JsonParser#hasCurrentToken()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#hasCurrentToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonExtract.java    From presto with Apache License 2.0 6 votes vote down vote up
public T processJsonObject(JsonParser jsonParser)
        throws IOException
{
    while (!jsonParser.nextFieldName(fieldName)) {
        if (!jsonParser.hasCurrentToken()) {
            throw new JsonParseException(jsonParser, "Unexpected end of object");
        }
        if (jsonParser.getCurrentToken() == END_OBJECT) {
            // Unable to find matching field
            return null;
        }
        jsonParser.skipChildren(); // Skip nested structure if currently at the start of one
    }

    jsonParser.nextToken(); // Shift to first token of the value

    return delegate.extract(jsonParser);
}
 
Example 2
Source File: KafkaConfiguration.java    From dropwizard-kafka-http with Apache License 2.0 6 votes vote down vote up
@Override
public Properties deserialize(JsonParser jp, DeserializationContext context) throws IOException {
    Properties props = new Properties();
    if (!jp.hasCurrentToken() || jp.getCurrentToken() != JsonToken.START_OBJECT)
        return props;

    String name = null;
    for (JsonToken token = jp.nextToken(); token != JsonToken.END_OBJECT; token = jp.nextToken()) {
        switch (token) {
            case FIELD_NAME: name = jp.getCurrentName(); break;
            case VALUE_STRING:
                String value = jp.getValueAsString();
                props.setProperty(name, value); break;
            default:
                throw new UnsupportedOperationException("Unsupported configuration item " + token + ". Only string values are supported");
        }
    }

    return props;
}
 
Example 3
Source File: LngLatAltDeserializer.java    From geojson-jackson with Apache License 2.0 6 votes vote down vote up
protected LngLatAlt deserializeArray(JsonParser jp, DeserializationContext ctxt) throws IOException {
    LngLatAlt node = new LngLatAlt();
    node.setLongitude(extractDouble(jp, ctxt, false));
    node.setLatitude(extractDouble(jp, ctxt, false));
    node.setAltitude(extractDouble(jp, ctxt, true));
    List<Double> additionalElementsList = new ArrayList<Double>();
    while (jp.hasCurrentToken() && jp.getCurrentToken() != JsonToken.END_ARRAY) {
        double element = extractDouble(jp, ctxt, true);
        if (!Double.isNaN(element)) {
            additionalElementsList.add(element);
        }
    }
    double[] additionalElements = new double[additionalElementsList.size()];
    for (int i = 0; i < additionalElements.length; i++) {
        additionalElements[i] = additionalElementsList.get(i);
    }
    node.setAdditionalElements(additionalElements);
    return node;
}
 
Example 4
Source File: JsonExtract.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public Slice extract(JsonParser jsonParser)
        throws IOException
{
    if (!jsonParser.hasCurrentToken()) {
        throw new JsonParseException(jsonParser, "Unexpected end of value");
    }

    DynamicSliceOutput dynamicSliceOutput = new DynamicSliceOutput(ESTIMATED_JSON_OUTPUT_SIZE);
    try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, dynamicSliceOutput)) {
        jsonGenerator.copyCurrentStructure(jsonParser);
    }
    return dynamicSliceOutput.slice();
}
 
Example 5
Source File: TableStorageErrorDeserializer.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the Extended Error information.
 * 
 * @return the Extended Error information.
 * 
 * @param reader
 *            the input stream to read error details from.
 * @param format
 *            The {@link TablePayloadFormat} to use for parsing
 * @throws IOException
 *             if an error occurs while accessing the stream with Json.
 */
public static StorageExtendedErrorInformation getExtendedErrorInformation(final Reader reader,
        final TablePayloadFormat format) throws JsonParseException, IOException {

    JsonFactory jsonFactory = new JsonFactory();
    JsonParser parser = jsonFactory.createParser(reader);
    try {
        final StorageExtendedErrorInformation errorInfo = new StorageExtendedErrorInformation();

        if (!parser.hasCurrentToken()) {
            parser.nextToken();
        }

        JsonUtilities.assertIsStartObjectJsonToken(parser);

        parser.nextToken();
        JsonUtilities.assertIsFieldNameJsonToken(parser);
        JsonUtilities.assertIsExpectedFieldName(parser, "odata.error");

        // start getting extended error information
        parser.nextToken();
        JsonUtilities.assertIsStartObjectJsonToken(parser);

        // get code
        parser.nextValue();
        JsonUtilities.assertIsExpectedFieldName(parser, TableConstants.ErrorConstants.ERROR_CODE);
        errorInfo.setErrorCode(parser.getValueAsString());

        // get message
        parser.nextToken();
        JsonUtilities.assertIsFieldNameJsonToken(parser);
        JsonUtilities.assertIsExpectedFieldName(parser, TableConstants.ErrorConstants.ERROR_MESSAGE);

        parser.nextToken();
        JsonUtilities.assertIsStartObjectJsonToken(parser);

        parser.nextValue();
        JsonUtilities.assertIsExpectedFieldName(parser, "lang");

        parser.nextValue();
        JsonUtilities.assertIsExpectedFieldName(parser, "value");
        errorInfo.setErrorMessage(parser.getValueAsString());

        parser.nextToken();
        JsonUtilities.assertIsEndObjectJsonToken(parser);

        parser.nextToken();

        // get innererror if it exists
        if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            JsonUtilities.assertIsExpectedFieldName(parser, TableConstants.ErrorConstants.INNER_ERROR);
            errorInfo.getAdditionalDetails().putAll(parseJsonErrorException(parser));
            parser.nextToken();
        }

        // end code object
        JsonUtilities.assertIsEndObjectJsonToken(parser);

        // end odata.error object
        parser.nextToken();
        JsonUtilities.assertIsEndObjectJsonToken(parser);

        return errorInfo;
    }
    finally {
        parser.close();
    }
}
 
Example 6
Source File: ProjectDeserializer.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public Project deserialize( final JsonParser jp, final DeserializationContext ctxt ) throws IOException {
    ProjectBuilder builder = new ProjectBuilder();

    String tmp = jp.getText();
    validate( jp, tmp, "{" );
    LOG.debug( "First token is {}", tmp );

    jp.nextToken();
    tmp = jp.getText();
    LOG.debug( "Second token is {}", tmp );

    while( jp.hasCurrentToken() ) {

        tmp = jp.getText();
        LOG.debug( "Current token text = {}", tmp );

        if ( tmp.equals( "testPackageBase" ) ) {
            jp.nextToken();
            builder.setTestPackageBase( jp.getText() );
        }
        else if ( tmp.equals( "chopVersion" ) ) {
            jp.nextToken();
            builder.setChopVersion( jp.getText() );
        }
        else if ( tmp.equals( "createTimestamp" ) ) {
            jp.nextToken();
            builder.setCreateTimestamp( jp.getText() );
        }
        else if ( tmp.equals( "vcsVersion" ) ) {
            jp.nextToken();
            builder.setVcsVersion( jp.getText() );
        }
        else if ( tmp.equals( "vcsRepoUrl" ) ) {
            jp.nextToken();
            builder.setVcsRepoUrl( jp.getText() );
        }
        else if ( tmp.equals( "groupId" ) ) {
            jp.nextToken();
            builder.setGroupId( jp.getText() );
        }
        else if ( tmp.equals( "artifactId" ) ) {
            jp.nextToken();
            builder.setArtifactId( jp.getText() );
        }
        else if ( tmp.equals( "projectVersion" ) ) {
            jp.nextToken();
            builder.setProjectVersion( jp.getText() );
        }
        else if ( tmp.equals( "md5" ) ) {
            jp.nextToken();
            builder.setMd5( jp.getText() );
        }
        else if ( tmp.equals( "loadKey" ) ) {
            jp.nextToken();
            builder.setLoadKey( jp.getText() );
        }
        else if ( tmp.equals( "loadTime" ) ) {
            jp.nextToken();
            builder.setLoadTime( jp.getText() );
        }

        jp.nextToken();

        if ( jp.getText().equals( "}" ) ) {
            break;
        }
    }

    return builder.getProject();
}
 
Example 7
Source File: RunnerDeserializer.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public Runner deserialize( final JsonParser jp, final DeserializationContext ctxt ) throws IOException {
    RunnerBuilder builder = new RunnerBuilder();

    String tmp = jp.getText();
    validate( jp, tmp, "{" );
    LOG.debug( "First token is {}", tmp );

    jp.nextToken();
    tmp = jp.getText();
    LOG.debug( "Second token is {}", tmp );

    while( jp.hasCurrentToken() ) {

        tmp = jp.getText();
        LOG.debug( "Current token text = {}", tmp );

        if ( tmp.equals( "ipv4Address" ) ) {
            jp.nextToken();
            builder.setIpv4Address( jp.getText() );
        }
        else if ( tmp.equals( "hostname" ) ) {
            jp.nextToken();
            builder.setHostname( jp.getText() );
        }
        else if ( tmp.equals( "url" ) ) {
            jp.nextToken();
            builder.setUrl( jp.getText() );
        }
        else if ( tmp.equals( "serverPort" ) ) {
            jp.nextToken();
            builder.setServerPort( jp.getValueAsInt() );
        }
        else if ( tmp.equals( "tempDir" ) ) {
            jp.nextToken();
            builder.setTempDir( jp.getText() );
        }

        jp.nextToken();

        if ( jp.getText().equals( "}" ) ) {
            break;
        }
    }

    return builder.getRunner();
}
 
Example 8
Source File: CoordinatedClusterDeserializer.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public ICoordinatedCluster deserialize( final JsonParser jp, final DeserializationContext ctxt )
        throws IOException {

    String tmp = jp.getText();
    validate( jp, tmp, "{" );

    jp.nextToken();
    tmp = jp.getText();

    BasicCluster delegate = new BasicCluster();
    CoordinatedCluster cluster = new CoordinatedCluster( delegate );

    while( jp.hasCurrentToken() ) {

        tmp = jp.getText();

        if ( tmp.equals( CoordinatedClusterSerializer.NAME ) ) {
            jp.nextToken();
            delegate.setName( jp.getText() );
        }
        else if ( tmp.equals( CoordinatedClusterSerializer.SIZE ) ) {
            jp.nextToken();
            delegate.setSize( jp.getIntValue() );
        }
        else if ( tmp.equals( CoordinatedClusterSerializer.INSTANCE_SPEC ) ) {
            jp.nextToken();
            delegate.setInstanceSpec( jp.readValuesAs( InstanceSpec.class ).next() );
        }
        else if ( tmp.equals( CoordinatedClusterSerializer.INSTANCES ) ) {
            jp.nextToken();
            jp.nextToken();
            Iterator<Instance> iterator = jp.readValuesAs( Instance.class );
            while ( iterator.hasNext() ) {
                cluster.add( iterator.next() );
            }
        }

        jp.nextToken();

        if ( jp.getText().equals( "}" ) ) {
            break;
        }
    }

    return cluster;
}