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.nuxeo.connect.tools.report.viewer.Viewer.java

public static void main(String[] varargs) throws IOException, ParseException {

    class Arguments {
        Options options = new Options()
                .addOption(Option.builder("i").longOpt("input").hasArg().argName("file")
                        .desc("report input file").build())
                .addOption(Option.builder("o").longOpt("output").hasArg().argName("file")
                        .desc("thread dump output file").build());
        final CommandLine commandline = new DefaultParser().parse(options, varargs);

        Arguments() throws ParseException {

        }/*from  ww  w .  j  av  a2  s  .c o m*/

        InputStream input() throws IOException {
            if (!commandline.hasOption('i')) {
                return System.in;
            }
            return Files.newInputStream(Paths.get(commandline.getOptionValue('i')));
        }

        PrintStream output() throws IOException {
            if (!commandline.hasOption('o')) {
                return System.out;
            }
            return new PrintStream(commandline.getOptionValue('o'));
        }

    }

    Arguments arguments = new Arguments();
    final JsonFactory jsonFactory = new JsonFactory();
    PrintStream output = arguments.output();
    JsonParser parser = jsonFactory.createParser(arguments.input());
    ObjectMapper mapper = new ObjectMapper();
    while (!parser.isClosed() && parser.nextToken() != JsonToken.NOT_AVAILABLE) {
        String hostid = parser.nextFieldName();
        output.println(hostid);
        {
            parser.nextToken();
            while (parser.nextToken() == JsonToken.FIELD_NAME) {
                if ("mx-thread-dump".equals(parser.getCurrentName())) {
                    parser.nextToken(); // start mx-thread-dump report
                    while (parser.nextToken() == JsonToken.FIELD_NAME) {
                        if ("value".equals(parser.getCurrentName())) {
                            parser.nextToken();
                            printThreadDump(mapper.readTree(parser), output);
                        } else {
                            parser.nextToken();
                            parser.skipChildren();
                        }
                    }
                } else if ("mx-thread-deadlocked".equals(parser.getCurrentName())) {
                    parser.nextToken();
                    while (parser.nextToken() == JsonToken.FIELD_NAME) {
                        if ("value".equals(parser.getCurrentName())) {
                            if (parser.nextToken() == JsonToken.START_ARRAY) {
                                printThreadDeadlocked(mapper.readerFor(Long.class).readValue(parser), output);
                            }
                        } else {
                            parser.nextToken();
                            parser.skipChildren();
                        }
                    }
                } else if ("mx-thread-monitor-deadlocked".equals(parser.getCurrentName())) {
                    parser.nextToken();
                    while (parser.nextToken() == JsonToken.FIELD_NAME) {
                        if ("value".equals(parser.getCurrentName())) {
                            if (parser.nextToken() == JsonToken.START_ARRAY) {
                                printThreadMonitorDeadlocked(mapper.readerFor(Long.class).readValues(parser),
                                        output);
                            }
                        } else {
                            parser.nextToken();
                            parser.skipChildren();
                        }
                    }
                } else {
                    parser.nextToken();
                    parser.skipChildren();
                }
            }
        }
    }

}

From source file:Main.java

/**
 * JSON Deserialization of the given json string.
 * @param   jParser The json parser for reading json to deserialize
 * @param   <T>   The type of the object to deserialize into
 * @return   The deserialized object */
public static <T extends Object> T deserialize(JsonParser jParser, Class<T> typeReference) throws IOException {
    if ((null == jParser) || (jParser.isClosed()))
        return null;

    return mapper.readValue(jParser, typeReference);
}

From source file:ai.susi.geo.GeoJsonReader.java

private static Map<String, String> parseMap(JsonParser parser) throws JsonParseException, IOException {
    Map<String, String> map = new HashMap<>();
    JsonToken token = parser.nextToken();
    if (!JsonToken.START_OBJECT.equals(token))
        return map;

    while (!parser.isClosed() && (token = parser.nextToken()) != null && token != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        token = parser.nextToken();//w w  w . ja  va2 s.co m
        String value = parser.getText();
        map.put(name.toLowerCase(), value);
    }
    return map;
}

From source file:org.helm.notation2.wsadapter.MonomerWSSaver.java

/**
 * Adds or updates a single monomer to the monomer store using the URL configured in {@code MonomerStoreConfiguration}
 * .//  ww w  .ja  v  a2  s.  c om
 * 
 * @param monomer to save
 */
public String saveMonomerToStore(Monomer monomer) {
    String res = "";
    CloseableHttpResponse response = null;

    try {
        response = WSAdapterUtils.putResource(monomer.toJSON(),
                MonomerStoreConfiguration.getInstance().getWebserviceNucleotidesPutFullURL());
        LOG.debug(response.getStatusLine().toString());

        JsonFactory jsonf = new JsonFactory();
        InputStream instream = response.getEntity().getContent();

        JsonParser jsonParser = jsonf.createParser(instream);

        while (!jsonParser.isClosed()) {
            JsonToken jsonToken = jsonParser.nextToken();
            if (JsonToken.FIELD_NAME.equals(jsonToken)) {
                String fieldName = jsonParser.getCurrentName();
                LOG.debug("Field name: " + fieldName);
                jsonParser.nextToken();
                if (fieldName.equals("monomerShortName")) {
                    res = jsonParser.getValueAsString();
                    break;
                }
            }
        }

        EntityUtils.consume(response.getEntity());

    } catch (Exception e) {
        LOG.error("Saving monomer failed!", e);
        return "";
    } finally {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            LOG.debug("Closing resources failed.", e);
            return res;
        }
    }

    return res;
}

From source file:com.sdl.odata.unmarshaller.json.JsonLinkUnmarshaller.java

@Override
protected String getToEntityId(ODataRequestContext requestContext) throws ODataUnmarshallingException {
    // The body is expected to contain a single entity reference
    // See OData JSON specification chapter 13

    String bodyText;//from ww w . j  a  v a 2  s.c  o  m
    try {
        bodyText = requestContext.getRequest().getBodyText(StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
        throw new ODataSystemException("UTF-8 is not supported", e);
    }

    String idValue = null;
    try {
        JsonParser parser = new JsonFactory().createParser(bodyText);
        while (idValue == null && !parser.isClosed()) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                break;
            }

            if (token.equals(JsonToken.FIELD_NAME) && parser.getCurrentName().equals(JsonConstants.ID)) {
                token = parser.nextToken();
                if (token.equals(JsonToken.VALUE_STRING)) {
                    idValue = parser.getText();
                }
            }
        }
    } catch (IOException e) {
        throw new ODataUnmarshallingException("Error while parsing JSON data", e);
    }

    if (isNullOrEmpty(idValue)) {
        throw new ODataUnmarshallingException("The JSON object in the body has no '@odata.id' value,"
                + " or the value is empty. The JSON object in the body must have an '@odata.id' value"
                + " that refers to the entity to link to.");
    }

    return idValue;
}

From source file:com.world.watch.worldwatchcron.util.WWCron.java

public long extractLatestTimeStamp(String newsJson) {
    long maxTS = 0;
    JsonFactory factory = new JsonFactory();
    try {/*from   ww w  . j a v  a  2s.co m*/
        JsonParser parser = factory.createParser(newsJson);
        while (!parser.isClosed()) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                break;
            }
            String fieldName = parser.getCurrentName();
            if (fieldName != null && fieldName.equals("date")) {
                parser.nextToken();
                long date = Long.parseLong(parser.getText());
                if (maxTS < date) {
                    maxTS = date;
                }
            }
        }
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        //logger.error("File not found ", e);
    }
    return maxTS;
}

From source file:automenta.knowtention.model.JSONObjectMetrics.java

public JSONObjectMetrics(JsonNode n) {

    this.when = System.currentTimeMillis();
    this.channel = null;

    JsonParser parser = n.traverse();

    error = true;//w w  w . j a v  a  2 s  . c o m
    while (!parser.isClosed()) {

        try {

            JsonToken t = parser.nextToken();
            if (t == null)
                break;

            switch (t) {
            case VALUE_STRING:
                numValues++;
                numStrings++;
                String s = parser.getValueAsString();
                if (s != null)
                    stringLengthSum += s.length();
                break;
            case VALUE_EMBEDDED_OBJECT:
                numValues++;
                numEmbeddedObjects++;
                break;
            case VALUE_FALSE:
            case VALUE_TRUE:
                numValues++;
                numBooleans++;
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                numNumbers++;
                numValues++;
                break;
            case VALUE_NULL:
                numValues++;
                numNulls++;
                break;
            }

            if (t == null)
                break;
            numTokens++;

        } catch (IOException ex) {
            break;
        }

    }

    error = false;
}

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

@Override
public Set<Scope> 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<Scope> scopeSet = 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  w w w .j a  v  a  2 s  . c o  m
        if (JsonToken.FIELD_NAME.equals(token) && "scopes".equals(jp.getCurrentName())) {
            token = jp.nextToken();
            if (!JsonToken.START_ARRAY.equals(token)) {
                // bail out
                throw new ParseException("expected ARRAY after scopes");
            }

            while (true) {
                token = jp.nextToken();
                if (JsonToken.END_ARRAY.equals(token)) {
                    // bail out
                    break;
                }
                if (JsonToken.START_OBJECT.equals(token)) {
                    Scope scope = jsonToScope(jp);
                    scopeSet.add(scope);
                }
            }
        }
    }
    return scopeSet;
}

From source file:invar.lib.data.DataParserJson.java

private void parse(JsonParser parser, DataNode root) throws IOException {
    String fieldName = null;/*from  www.  j a va 2 s  .c o m*/
    DataNode parent = root;
    while (!parser.isClosed()) {
        JsonToken token = parser.nextToken();
        if (token == null) {
            continue;
        }
        switch (token) {
        case START_ARRAY:
            parent.addChild(parent = DataNode.createArray().setFieldName(fieldName));
            fieldName = null;
            break;
        case END_ARRAY:
            parent = parent.getParent();
            fieldName = null;
            break;
        case START_OBJECT:
            parent.addChild(parent = DataNode.createObject().setFieldName(fieldName));
            fieldName = null;
            break;
        case END_OBJECT:
            parent = parent.getParent();
            fieldName = null;
            break;
        case VALUE_TRUE:
            parent.addChild(DataNode.createBoolean().setValue(true).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_FALSE:
            parent.addChild(DataNode.createBoolean().setValue(false).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_NULL:
            parent.addChild(DataNode.createNull().setValue(null).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_STRING:
            parent.addChild(
                    DataNode.createString().setFieldName(fieldName).setValue(parser.getValueAsString()));
            fieldName = null;
            break;
        case VALUE_NUMBER_INT:
            try {
                Long v = parser.getValueAsLong();
                parent.addChild(DataNode.createLong().setFieldName(fieldName).setValue(v));
            } catch (JsonParseException e) {
                BigInteger v = parser.getBigIntegerValue();
                parent.addChild(DataNode.createBigInt().setFieldName(fieldName).setValue(v));
            }
            fieldName = null;
            break;
        case VALUE_NUMBER_FLOAT:
            parent.addChild(
                    DataNode.createDouble().setFieldName(fieldName).setValue(parser.getValueAsDouble()));
            fieldName = null;
            break;
        case FIELD_NAME:
            fieldName = parser.getValueAsString();
            break;
        default:
            fieldName = null;
            break;
        }
    }
}

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

@Override
public Set<RestAccess> 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<RestAccess> restAccessSet = 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;
        }/*  ww w.  ja v a 2 s.co  m*/

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

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

                if (JsonToken.START_OBJECT.equals(token)) {
                    RestAccess restAccess = jsonToRestAccess(jp);
                    restAccessSet.add(restAccess);
                }
            }
        }
    }
    return restAccessSet;
}