Example usage for com.fasterxml.jackson.core JsonParser isClosed

List of usage examples for com.fasterxml.jackson.core JsonParser isClosed

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser isClosed.

Prototype

public abstract boolean isClosed();

Source Link

Document

Method that can be called to determine whether this parser is closed or not.

Usage

From source file:org.onosproject.north.aaa.api.parser.impl.ApplicationParser.java

@Override
public Set<Application> parseJson(InputStream stream) throws IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<Application> applicationSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }/*from   ww w .  j  a  v  a2  s  .  co m*/

        if (JsonToken.FIELD_NAME.equals(token) && "applications".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after applications");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }

                if (JsonToken.START_OBJECT.equals(token)) {
                    Application app = jsonToApplication(jp);
                    applicationSet.add(app);
                }
            }
        }
    }
    return applicationSet;
}

From source file:org.onosproject.north.aaa.api.parser.impl.ClientParser.java

@Override
public Set<ClientCredential> parseJson(InputStream stream) throws IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<ClientCredential> clientSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }//w  w w .  ja  v  a 2s  . co  m

        if (JsonToken.FIELD_NAME.equals(token) && "clients".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after clients");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }

                if (JsonToken.START_OBJECT.equals(token)) {
                    ClientCredential client = jsonToClientCredential(jp);
                    clientSet.add(client);
                }
            }
        }
    }
    return clientSet;
}

From source file:org.onosproject.north.aaa.api.parser.impl.UserParser.java

@Override
public Set<User> parseJson(InputStream stream) throws IOException, ParseException {
    // begin parsing JSON to Application class
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(stream);
    JsonParser jp = jsonNode.traverse();
    Set<User> userSet = new HashSet<>();

    // continue parsing the token till the end of input is reached
    while (!jp.isClosed()) {
        // get the token
        JsonToken token = jp.nextToken();
        // if its the last token then we are done
        if (token == null) {
            break;
        }/*from   ww w.j  a  va  2s  . c o m*/
        if (JsonToken.FIELD_NAME.equals(token) && "users".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after users");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }
                if (JsonToken.START_OBJECT.equals(token)) {
                    User user = jsonToUser(jp);
                    userSet.add(user);
                }
            }
        }
    }
    return userSet;
}

From source file:org.wso2.extension.siddhi.map.json.sourcemapper.JsonSourceMapper.java

private Event convertToSingleEventForDefaultMapping(Object eventObject) throws IOException {
    Event event = new Event(attributesSize);
    Object[] data = event.getData();
    JsonParser parser;
    int numberOfProvidedAttributes = 0;
    try {//  ww  w . ja  v a2  s. c o  m
        parser = factory.createParser(eventObject.toString());
    } catch (IOException e) {
        throw new SiddhiAppRuntimeException(
                "Initializing a parser failed for the event string." + eventObject.toString());
    }
    int position;
    while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();
        if (JsonToken.START_OBJECT.equals(jsonToken)) {
            parser.nextToken();
            if (DEFAULT_JSON_EVENT_IDENTIFIER.equalsIgnoreCase(parser.getText())) {
                parser.nextToken();
            } else {
                log.error("Default json message " + eventObject
                        + " contains an invalid event identifier. Required \"event\", " + "but found \""
                        + parser.getText() + "\". Hence dropping the message.");
                return null;
            }
        } else if (JsonToken.FIELD_NAME.equals(jsonToken)) {
            String key = parser.getCurrentName();
            numberOfProvidedAttributes++;
            position = findDefaultMappingPosition(key);
            if (position == -1) {
                log.error("Stream \"" + streamDefinition.getId() + "\" does not have an attribute named \""
                        + key + "\", but the received event " + eventObject.toString()
                        + " does. Hence dropping the message.");
                return null;
            }
            jsonToken = parser.nextToken();
            Attribute.Type type = streamAttributes.get(position).getType();

            if (JsonToken.VALUE_NULL.equals(jsonToken)) {
                data[position] = null;
            } else {
                switch (type) {
                case BOOL:
                    if (JsonToken.VALUE_TRUE.equals(jsonToken) || JsonToken.VALUE_FALSE.equals(jsonToken)) {
                        data[position] = parser.getValueAsBoolean();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type BOOL. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case INT:
                    if (JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) {
                        data[position] = parser.getValueAsInt();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type INT. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case DOUBLE:
                    if (JsonToken.VALUE_NUMBER_FLOAT.equals(jsonToken)) {
                        data[position] = parser.getValueAsDouble();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type DOUBLE. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case STRING:
                    if (JsonToken.VALUE_STRING.equals(jsonToken)) {
                        data[position] = parser.getValueAsString();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type STRING. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case FLOAT:
                    if (JsonToken.VALUE_NUMBER_FLOAT.equals(jsonToken)
                            || JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) {
                        data[position] = attributeConverter.getPropertyValue(parser.getValueAsString(),
                                Attribute.Type.FLOAT);
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type FLOAT. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case LONG:
                    if (JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) {
                        data[position] = parser.getValueAsLong();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type LONG. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                default:
                    return null;
                }
            }
        }
    }

    if (failOnMissingAttribute && (numberOfProvidedAttributes != attributesSize)) {
        log.error("Json message " + eventObject.toString()
                + " contains missing attributes. Hence dropping the message.");
        return null;
    }
    return event;
}

From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java

/**
 * Reads token of the specified JsonParser and returns a list of column metadata.
 *
 * @param jsonParser the jsonParser whose next tokens are supposed to represent a list of column metadata
 * @return The column metadata parsed from JSON parser.
 * @throws IOException In case of JSON exception related error.
 */// www.  ja  v  a2 s.co m
private static List<ColumnMetadata> parseAnArrayOfColumnMetadata(JsonParser jsonParser) throws IOException {
    try {
        List<ColumnMetadata> columns = new ArrayList<>();
        // skip the array beginning [
        jsonParser.nextToken();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY && !jsonParser.isClosed()) {
            ColumnMetadata columnMetadata = jsonParser.readValueAs(ColumnMetadata.class);
            columns.add(columnMetadata);
        }
        if (columns.isEmpty()) {
            throw new IllegalArgumentException(
                    "No column metadata has been retrieved when trying to parse the retrieved data set.");
        }
        return columns;
    } catch (IOException e) {
        throw new IOExceptionWithCause("Unable to parse and retrieve the list of column metadata", e);
    }
}

From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java

private static RowMetadata parseDataSetMetadataAndReturnRowMetadata(JsonParser jsonParser) throws IOException {
    try {//from w ww.j  a  va 2 s  .  c om
        RowMetadata rowMetadata = null;
        while (jsonParser.nextToken() != JsonToken.END_OBJECT && !jsonParser.isClosed()) {
            String currentField = jsonParser.getCurrentName();
            if (StringUtils.equalsIgnoreCase("columns", currentField)) {
                rowMetadata = new RowMetadata(parseAnArrayOfColumnMetadata(jsonParser));
            }
        }
        LOGGER.debug("Skipping data to go back to the outer json object");
        while (jsonParser.getParsingContext().getParent().getCurrentName() != null && !jsonParser.isClosed()) {
            jsonParser.nextToken();
        }
        return rowMetadata;
    } catch (IOException e) {
        throw new IOExceptionWithCause("Unable to parse and retrieve the row metadata", e);
    }
}

From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java

private static LightweightExportableDataSet parseRecords(JsonParser jsonParser, RowMetadata rowMetadata,
        String joinOnColumn) throws IOException {
    try {// w  w  w.j a  v  a 2  s .  co  m
        LightweightExportableDataSet lookupDataset = new LightweightExportableDataSet();
        lookupDataset.setMetadata(rowMetadata);
        jsonParser.nextToken();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY && !jsonParser.isClosed()) {
            Map<String, String> values = jsonParser.readValueAs(Map.class);
            lookupDataset.addRecord(values.get(joinOnColumn), values);
        }
        if (lookupDataset.isEmpty()) {
            throw new IllegalArgumentException(
                    "No lookup record has been retrieved when trying to parse the retrieved data set.");
        }
        return lookupDataset;
    } catch (IOException e) {
        throw new IOExceptionWithCause("Unable to parse and retrieve the records of the data set", e);
    }
}