Java Code Examples for org.codehaus.jackson.JsonToken#END_ARRAY

The following examples show how to use org.codehaus.jackson.JsonToken#END_ARRAY . 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: JsonSerdeUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static List<Object> parseArray(@Nonnull final JsonParser p,
        @CheckForNull final List<TypeInfo> columnTypes)
        throws HCatException, IOException, SerDeException {
    Preconditions.checkNotNull(columnTypes, "columnTypes MUST NOT be null",
        SerDeException.class);
    if (columnTypes.size() != 1) {
        throw new IOException("Expected a single array but go " + columnTypes);
    }

    TypeInfo elemType = columnTypes.get(0);
    HCatSchema schema = HCatSchemaUtils.getHCatSchema(elemType);

    HCatFieldSchema listSchema = schema.get(0);
    HCatFieldSchema elemSchema = listSchema.getArrayElementSchema().get(0);

    final List<Object> arr = new ArrayList<Object>();
    while (p.nextToken() != JsonToken.END_ARRAY) {
        arr.add(extractCurrentField(p, elemSchema, true));
    }
    return arr;
}
 
Example 2
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
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
Source File: BackportedJacksonMappingIterator.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Equivalent of {@link #next} but one that may throw checked
 * exceptions from Jackson due to invalid input.
 */
public boolean hasNextValue() throws IOException {
    if (_parser == null) {
        return false;
    }
    JsonToken t = _parser.getCurrentToken();
    if (t == null) { // un-initialized or cleared; find next
        t = _parser.nextToken();
        // If EOF, no more
        if (t == null) {
            _parser.close();
            return false;
        }
        // And similarly if we hit END_ARRAY; except that we won't close parser
        if (t == JsonToken.END_ARRAY) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: HiveJsonStructReader.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
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 5
Source File: BarFileReadRunner.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 10_relations.json, 20_roles.json, 30_extroles.json, 70_$links.json, 10_odatarelations.jsonのバリデートチェック.
 * @param jp Jsonパース
 * @param mapper ObjectMapper
 * @param jsonName ファイル名
 * @throws IOException IOException
 */
protected void registJsonEntityData(JsonParser jp, ObjectMapper mapper, String jsonName) throws IOException {
    JsonToken token;
    token = jp.nextToken();

    // Relations,Roles,ExtRoles,$linksのチェック
    checkMatchFieldName(jp, jsonName);

    token = jp.nextToken();
    // 配列でなければエラー
    if (token != JsonToken.START_ARRAY) {
        throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
    }
    token = jp.nextToken();

    while (jp.hasCurrentToken()) {
        if (token == JsonToken.END_ARRAY) {
            break;
        } else if (token != JsonToken.START_OBJECT) {
            throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
        }

        // 1件登録処理
        JSONMappedObject mappedObject = barFileJsonValidate(jp, mapper, jsonName);
        if (jsonName.equals(RELATION_JSON)) {
            createRelation(mappedObject.getJson());
        } else if (jsonName.equals(ROLE_JSON)) {
            createRole(mappedObject.getJson());
        } else if (jsonName.equals(EXTROLE_JSON)) {
            createExtRole(mappedObject.getJson());
        } else if (jsonName.equals(LINKS_JSON)) {
            createLinks(mappedObject, odataProducer);
        }

        token = jp.nextToken();
    }
}
 
Example 6
Source File: PNetGenerationCommand.java    From workcraft with MIT License 4 votes vote down vote up
public static void initParse(String args) throws IOException {
    JsonFactory f = new MappingJsonFactory();
    JsonParser jp = f.createJsonParser(new File(args));

    JsonToken current;

    current = jp.nextToken();
    if (current != JsonToken.START_OBJECT) {
        LogUtils.logError("Root should be object: quiting.");
        return;
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        // move from field name to field value
        current = jp.nextToken();

        if ("NETWORK".equals(fieldName)) {
            if (current == JsonToken.START_ARRAY) {
                // For each of the records in the array
                System.out.println("Generate CPNs");
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    JsonNode node = jp.readValueAsTree();
                    String idName = node.get("id").toString();
                    String idName1 = "";
                    String idName2 = "";
                    String idNamep = "";
                    String idNamep1 = "";
                    String idNamep2 = "";
                    String typeName = node.get("type").toString();
                    //System.out.println("id: " + idName + "type: " + typeName);
                    lst2.add(new Ids(idName, typeName));
                    JsonNode y = node.get("outs");
                    if (y != null) {
                        for (int i = 0; y.has(i); i++) {
                            if (y.get(i).has("id")) {
                                if (i == 0) {
                                    idName1 = y.get(i).get("id").toString();
                                    idNamep1 = y.get(i).get("in_port").toString();
                                    if ("xfork".equals(typeName)) {
                                        lst.add(new Info(idName1, idName, "b", idNamep1));
                                    } else if ("xswitch".equals(typeName)) {
                                        lst.add(new Info(idName1, idName, "a", idNamep1));
                                    } else {
                                        lst.add(new Info(idName1, idName, "", idNamep1));
                                    }
                                    if (idName1.contains("Sync")) {
                                        //System.out.println("id: " + idName + "sync: " + idName1);
                                        slsti.add(new Info(idName, idName1, "", idNamep1));   //swapped order slsti slsto
                                    }
                                    //add o based on order of i or reverse?
                                    if (idName.contains("Sync")) {
                                        slsto2.add(new Info(idName1, idName, "", idNamep1));
                                    }
                                } else if (i == 1) {
                                    idName2 = y.get(i).get("id").toString();
                                    idNamep2 = y.get(i).get("in_port").toString();
                                    if ("xfork".equals(typeName)) {
                                        lst.add(new Info(idName2, idName, "a", idNamep2));
                                    } else if ("xswitch".equals(typeName)) {
                                        lst.add(new Info(idName2, idName, "b", idNamep2));
                                    } else {
                                        lst.add(new Info(idName2, idName, "", idNamep2));
                                    }
                                    if (idName2.contains("Sync")) slsti.add(new Info(idName, idName2, "", idNamep2));
                                    if (idName.contains("Sync")) {
                                        slsto2.add(new Info(idName2, idName, "", idNamep2));
                                    }
                                } else {
                                    idName1 = y.get(i).get("id").toString();
                                    idNamep = y.get(i).get("in_port").toString();
                                    if (idName.contains("Sync")) {
                                        slsto2.add(new Info(idName, idName1, "", idNamep));
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                LogUtils.logError("Records should be an array: skipping.");
                jp.skipChildren();
            }
        } else {
            //System.out.println("Unprocessed property: " + fieldName);
            jp.skipChildren();
        }
    }
}
 
Example 7
Source File: SSTableImport.java    From stratio-cassandra with 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;
}
 
Example 8
Source File: BackendResponse.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
private static BackendResponse parseBackendResponse(JsonParser parser) throws IOException {
    try {
        // Sanity check: verify that we got "Json Object":
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        long responseKey = 0;
        int delay = 0;
        int numItems = 0;
        int itemSize = 0;
        String[] items = null;
        JsonToken current;

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = parser.getCurrentName();
            // advance
            current = parser.nextToken();
            if (fieldName.equals("responseKey")) {
                responseKey = parser.getLongValue();
            } else if (fieldName.equals("delay")) {
                delay = parser.getIntValue();
            } else if (fieldName.equals("itemSize")) {
                itemSize = parser.getIntValue();
            } else if (fieldName.equals("numItems")) {
                numItems = parser.getIntValue();
            } else if (fieldName.equals("items")) {
                // expect numItems to be populated before hitting this
                if (numItems == 0) {
                    throw new IllegalStateException("Expected numItems > 0");
                }
                items = new String[numItems];
                if (current == JsonToken.START_ARRAY) {
                    int j = 0;
                    // For each of the records in the array
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        items[j++] = parser.getText();
                    }
                } else {
                    //                            System.out.println("Error: items should be an array: skipping.");
                    parser.skipChildren();
                }

            }
        }
        return new BackendResponse(responseKey, delay, numItems, itemSize, items);
    } finally {
        parser.close();
    }
}
 
Example 9
Source File: ImportAdmins.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Imports admin users.
 *
 * @param fileName Name of admin user data file.
 */
private void importAdminUsers(final String fileName,
                              final int writeThreadCount,
                              final int auditThreadCount) throws Exception {

    int count = 0;

    File adminUsersFile = new File(importDir, fileName);

    logger.info("----- Loading file: " + adminUsersFile.getAbsolutePath());
    JsonParser jp = getJsonParserForFile(adminUsersFile);

    int loopCounter = 0;

    BlockingQueue<Map<String, Object>> workQueue = new LinkedBlockingQueue<Map<String, Object>>();
    BlockingQueue<Map<String, Object>> auditQueue = new LinkedBlockingQueue<Map<String, Object>>();

    startAdminWorkers(workQueue, auditQueue, writeThreadCount);
    startAdminAuditors(auditQueue, auditThreadCount);

    JsonToken token = jp.nextToken();
    validateStartArray(token);

    while (jp.nextValue() != JsonToken.END_ARRAY) {
        loopCounter += 1;

        @SuppressWarnings("unchecked")
        Map<String, Object> entityProps = jp.readValueAs(HashMap.class);
        if (loopCounter % 1000 == 0) {
            logger.debug( "Publishing to queue... counter=" + loopCounter );
        }

        workQueue.add( entityProps );
    }

    waitForQueueAndMeasure(workQueue, writeEmptyCount, adminWriteThreads, "Admin Write");
    waitForQueueAndMeasure(auditQueue, auditEmptyCount, adminAuditThreads, "Admin Audit");

    logger.info("----- End: Imported {} admin users from file {}",
            count, adminUsersFile.getAbsolutePath());

    jp.close();
}
 
Example 10
Source File: BackendResponse.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
public static BackendResponse parseBackendResponse(JsonParser parser) throws IOException {
    try {
        // Sanity check: verify that we got "Json Object":
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        long responseKey = 0;
        int delay = 0;
        int numItems = 0;
        int itemSize = 0;
        String[] items = null;
        JsonToken current;

        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = parser.getCurrentName();
            // advance
            current = parser.nextToken();
            if (fieldName.equals("responseKey")) {
                responseKey = parser.getLongValue();
            } else if (fieldName.equals("delay")) {
                delay = parser.getIntValue();
            } else if (fieldName.equals("itemSize")) {
                itemSize = parser.getIntValue();
            } else if (fieldName.equals("numItems")) {
                numItems = parser.getIntValue();
            } else if (fieldName.equals("items")) {
                // expect numItems to be populated before hitting this
                if (numItems == 0) {
                    throw new IllegalStateException("Expected numItems > 0");
                }
                items = new String[numItems];
                if (current == JsonToken.START_ARRAY) {
                    int j = 0;
                    // For each of the records in the array
                    while (parser.nextToken() != JsonToken.END_ARRAY) {
                        items[j++] = parser.getText();
                    }
                } else {
                    //                            System.out.println("Error: items should be an array: skipping.");
                    parser.skipChildren();
                }

            }
        }
        
        return new BackendResponse(responseKey, delay, numItems, itemSize, items);
    } finally {
        parser.close();
    }
}