Java Code Examples for org.codehaus.jackson.JsonParser#getCurrentToken()

The following examples show how to use org.codehaus.jackson.JsonParser#getCurrentToken() . 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
@Nonnull
private static Object parseValue(@Nonnull final JsonParser p)
        throws JsonParseException, IOException {
    final JsonToken t = p.getCurrentToken();
    switch (t) {
        case VALUE_FALSE:
            return Boolean.FALSE;
        case VALUE_TRUE:
            return Boolean.TRUE;
        case VALUE_NULL:
            return null;
        case VALUE_STRING:
            return p.getText();
        case VALUE_NUMBER_FLOAT:
            return p.getDoubleValue();
        case VALUE_NUMBER_INT:
            return p.getIntValue();
        default:
            throw new IOException("Unexpected token: " + t);
    }
}
 
Example 2
private static void skipValue(JsonParser parser) throws JsonParseException, IOException {
    int array = 0;
    int object = 0;
    do {
        JsonToken currentToken = parser.getCurrentToken();
        if (currentToken == JsonToken.START_ARRAY) {
            array++;
        }
        if (currentToken == JsonToken.END_ARRAY) {
            array--;
        }
        if (currentToken == JsonToken.START_OBJECT) {
            object++;
        }
        if (currentToken == JsonToken.END_OBJECT) {
            object--;
        }

        parser.nextToken();

    } while (array > 0 || object > 0);
}
 
Example 3
private Object parsePrimitive(JsonParser parser, PrimitiveObjectInspector oi)
        throws SerDeException, IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == null) {
        return null;
    }
    try {
        switch (parser.getCurrentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE:
            case VALUE_NUMBER_INT:
            case VALUE_NUMBER_FLOAT:
            case VALUE_STRING:
                return getObjectOfCorrespondingPrimitiveType(parser.getText(), oi);
            case VALUE_NULL:
                return null;
            default:
                throw new SerDeException("unexpected token type: " + currentToken);
        }
    } finally {
        parser.nextToken();
    }
}
 
Example 4
private Object parseMapKey(JsonParser parser, PrimitiveObjectInspector oi)
        throws SerDeException, IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == null) {
        return null;
    }
    try {
        switch (parser.getCurrentToken()) {
            case FIELD_NAME:
                return getObjectOfCorrespondingPrimitiveType(parser.getText(), oi);
            case VALUE_NULL:
                return null;
            default:
                throw new SerDeException("unexpected token type: " + currentToken);
        }
    } finally {
        parser.nextToken();
    }
}
 
Example 5
@SuppressWarnings("unchecked")
protected BackportedJacksonMappingIterator(JavaType type, JsonParser jp, DeserializationContext ctxt, JsonDeserializer<?> deser) {
    _type = type;
    _parser = jp;
    _context = ctxt;
    _deserializer = (JsonDeserializer<T>) deser;

    /* One more thing: if we are at START_ARRAY (but NOT root-level
     * one!), advance to next token (to allow matching END_ARRAY)
     */
    if (jp != null && jp.getCurrentToken() == JsonToken.START_ARRAY) {
        JsonStreamContext sc = jp.getParsingContext();
        // safest way to skip current token is to clear it (so we'll advance soon)
        if (!sc.inRoot()) {
            jp.clearCurrentToken();
        }
    }
}
 
Example 6
private Object parseMap(JsonParser parser, MapObjectInspector oi)
        throws IOException, SerDeException {
    if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
        parser.nextToken();
        return null;
    }

    Map<Object, Object> ret = new LinkedHashMap<>();

    if (parser.getCurrentToken() != JsonToken.START_OBJECT) {
        throw new SerDeException("struct expected");
    }

    if (!(oi.getMapKeyObjectInspector() instanceof PrimitiveObjectInspector)) {
        throw new SerDeException("map key must be a primitive");
    }
    PrimitiveObjectInspector keyOI = (PrimitiveObjectInspector) oi.getMapKeyObjectInspector();
    ObjectInspector valOI = oi.getMapValueObjectInspector();

    JsonToken currentToken = parser.nextToken();
    while (currentToken != null && currentToken != JsonToken.END_OBJECT) {

        if (currentToken != JsonToken.FIELD_NAME) {
            throw new SerDeException("unexpected token: " + currentToken);
        }

        Object key = parseMapKey(parser, keyOI);
        Object val = parseDispatcher(parser, valOI);
        ret.put(key, val);

        currentToken = parser.getCurrentToken();
    }
    if (currentToken != null) {
        parser.nextToken();
    }
    return ret;
}
 
Example 7
private Object parseList(JsonParser parser, ListObjectInspector oi)
        throws JsonParseException, IOException, SerDeException {
    List<Object> ret = new ArrayList<>();

    if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
        parser.nextToken();
        return null;
    }

    if (parser.getCurrentToken() != JsonToken.START_ARRAY) {
        throw new SerDeException("array expected");
    }
    ObjectInspector eOI = oi.getListElementObjectInspector();
    JsonToken currentToken = parser.nextToken();
    try {
        while (currentToken != null && currentToken != JsonToken.END_ARRAY) {
            ret.add(parseDispatcher(parser, eOI));
            currentToken = parser.getCurrentToken();
        }
    } catch (Exception e) {
        throw new SerDeException("array: " + e.getMessage(), e);
    }

    currentToken = parser.nextToken();

    return ret;
}
 
Example 8
Source Project: stratio-cassandra   File: SSTableImport.java    License: Apache License 2.0 4 votes vote down vote up
private int importSorted(String jsonFile, ColumnFamily columnFamily, String ssTablePath,
        IPartitioner partitioner) throws IOException
{
    int importedKeys = 0; // already imported keys count
    long start = System.nanoTime();

    JsonParser parser = getParser(jsonFile);

    if (keyCountToImport == null)
    {
        keyCountToImport = 0;
        System.out.println("Counting keys to import, please wait... (NOTE: to skip this use -n <num_keys>)");

        parser.nextToken(); // START_ARRAY
        while (parser.nextToken() != null)
        {
            parser.skipChildren();
            if (parser.getCurrentToken() == JsonToken.END_ARRAY)
                break;

            keyCountToImport++;
        }
    }

    System.out.printf("Importing %s keys...%n", keyCountToImport);

    parser = getParser(jsonFile); // renewing parser
    SSTableWriter writer = new SSTableWriter(ssTablePath, keyCountToImport, ActiveRepairService.UNREPAIRED_SSTABLE);

    int lineNumber = 1;
    DecoratedKey prevStoredKey = null;

    parser.nextToken(); // START_ARRAY
    while (parser.nextToken() != null)
    {
        String key = parser.getCurrentName();
        Map<?, ?> row = parser.readValueAs(new TypeReference<Map<?, ?>>(){});
        DecoratedKey currentKey = partitioner.decorateKey(getKeyValidator(columnFamily).fromString((String) row.get("key")));

        if (row.containsKey("metadata"))
            parseMeta((Map<?, ?>) row.get("metadata"), columnFamily, null);

        addColumnsToCF((List<?>) row.get("cells"), columnFamily);

        if (prevStoredKey != null && prevStoredKey.compareTo(currentKey) != -1)
        {
            System.err
                    .printf("Line %d: Key %s is greater than previous, collection is not sorted properly. Aborting import. You might need to delete SSTables manually.%n",
                            lineNumber, key);
            return -1;
        }

        // saving decorated key
        writer.append(currentKey, columnFamily);
        columnFamily.clear();

        prevStoredKey = currentKey;
        importedKeys++;
        lineNumber++;

        long current = System.nanoTime();

        if (TimeUnit.NANOSECONDS.toSeconds(current - start) >= 5) // 5 secs.
        {
            System.out.printf("Currently imported %d keys.%n", importedKeys);
            start = current;
        }

        if (keyCountToImport == importedKeys)
            break;

    }

    writer.closeAndOpenReader();

    return importedKeys;
}