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

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

Introduction

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

Prototype

public abstract JsonToken nextToken() throws IOException, JsonParseException;

Source Link

Document

Main iteration method, which will advance stream enough to determine type of the next token, if any.

Usage

From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java

/** Parses the fields, which should look like {field1:
 *  ..., field2: ..., ...} *///from ww  w . j a v a 2s.c  om
public static void parseFields(IndexState state, Document doc, JsonParser p) throws IOException {
    JsonToken token = p.nextToken();
    if (token != JsonToken.START_OBJECT) {
        throw new IllegalArgumentException("fields should be an object");
    }
    while (true) {
        token = p.nextToken();
        if (token == JsonToken.END_OBJECT) {
            break;
        }
        assert token == JsonToken.FIELD_NAME;
        parseOneField(p, state, doc, p.getText());
    }
}

From source file:com.ntsync.shared.RequestGenerator.java

/**
 * Parse a JSON-Object with "Server/Config/*" which the Restrictions
 * /* ww w . j  a va 2 s. c om*/
 * @param is
 * @return
 * @throws IOException
 */
public static Restrictions parseRestr(InputStream is) throws IOException {
    JsonParser jp = getJsonFactory().createParser(is);
    jp.nextToken();
    Restrictions restr = null;
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        // move to value, or START_OBJECT/START_ARRAY
        if (jp.nextToken() == null) {
            break;
        }
        if (SERVER_FIELD_NAME.equals(fieldname)) {
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String serverField = jp.getCurrentName();
                if (jp.nextToken() == null) {
                    break;
                }
                if (RequestGenerator.TAG_SERVER_CONFIG.equals(serverField)) {
                    restr = parseRestr(jp);
                }
            }
        }
    }
    return restr;
}

From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java

/** Parse a Document using Jackson's streaming parser
 *  API.  The document should look like {indexName: 'foo',
 *  fields: {..., ...}} *///from w w w .j  a va 2  s .c  om
public static Document parseDocument(IndexState state, JsonParser p) throws IOException {
    JsonToken token = p.nextToken();
    if (token == JsonToken.END_ARRAY) {
        // nocommit hackish.. caller should tell us this means "end"?
        return null;
    } else if (token != JsonToken.START_OBJECT) {
        throw new IllegalArgumentException("expected JSON Object");
    }

    final Document doc = new Document();
    while (true) {
        token = p.nextToken();
        if (token == JsonToken.END_OBJECT) {
            break;
        }
        assert token == JsonToken.FIELD_NAME : token;

        String fieldName = p.getText();
        if (fieldName.equals("fields")) {
            parseFields(state, doc, p);
        } else {
            // Let a plugin handle it:
            boolean handled = false;
            for (PostHandle postHandle : postHandlers) {
                if (postHandle.invoke(state, fieldName, p, doc)) {
                    handled = true;
                    break;
                }
            }

            if (!handled) {
                throw new IllegalArgumentException("unrecognized field " + p.getText());
            }
        }

        // nocommit need test that same field name can't
        // appear more than once?  app must put all values for
        // a given field into an array (for a multi-valued
        // field) 
    }

    return doc;
}

From source file:org.metis.utils.Utils.java

/**
 * Recursively steps through JSON object and arrays of objects. We support
 * only a single object or an array of objects, where each object represents
 * an entity (e.g., a student, a customer, an account, etc.).
 * // ww w . jav a2  s  .c  om
 * All objects must have the same identical set of keys.
 * 
 * @param jp
 * @param params
 * @throws Exception
 */
private static void parseJson(JsonParser jp, List<Map<String, String>> rows) throws Exception {

    // get the next json token
    JsonToken current = jp.nextToken();

    // base case: return if we've reached end of json stream
    if (current == null) {
        return;
    }

    // all rows must have the identical set of keys!
    Map<String, String> firstRow = null;
    if (!rows.isEmpty()) {
        firstRow = rows.get(0);
    }

    // we only accept objects or arrays of objects
    switch (current) {
    case START_OBJECT:
        HashMap<String, String> row = new HashMap<String, String>();
        while (jp.nextToken() != END_OBJECT) {
            // parser should be on 'key' token
            String key = jp.getCurrentName().toLowerCase();
            // ensure all rows have the identical set of keys!
            if (firstRow != null && firstRow.get(key) == null) {
                String eStr = "parseJson: given list of json objects do " + "not have identical set of keys";
                LOG.error(eStr);
                throw new Exception(eStr);
            }
            // now advance to 'value' token
            jp.nextToken();
            String value = jp.getText();
            row.put(key, value);
        }
        // if row is not null, add it to the rows list
        if (!row.isEmpty()) {
            // ensure all rows have the identical set of keys!
            if (firstRow != null && firstRow.size() != row.size()) {
                String eStr = "parseJson: given list of json objects do "
                        + "not have identical set of keys; number of " + "keys vary";
                LOG.error(eStr);
                throw new Exception(eStr);
            }
            rows.add(row);
        }
        break;
    case START_ARRAY:
    case END_ARRAY:
        break;
    default:
        LOG.error("parseJson: ERROR, json token is neither object nor array");
        throw new Exception("parseJson: ERROR, json token is neither object nor array");
    }

    // go on to the next start-of-array, end-of-array, start-of-object, or
    // end of stream
    parseJson(jp, rows);
}

From source file:info.bonjean.quinkana.Main.java

private static void run(String[] args) throws QuinkanaException {
    Properties properties = new Properties();
    InputStream inputstream = Main.class.getResourceAsStream("/config.properties");

    try {//from   ww  w.  j ava 2 s  . co  m
        properties.load(inputstream);
        inputstream.close();
    } catch (IOException e) {
        throw new QuinkanaException("cannot load internal properties", e);
    }

    String name = (String) properties.get("name");
    String description = (String) properties.get("description");
    String url = (String) properties.get("url");
    String version = (String) properties.get("version");
    String usage = (String) properties.get("help.usage");
    String defaultHost = (String) properties.get("default.host");
    int defaultPort = Integer.valueOf(properties.getProperty("default.port"));

    ArgumentParser parser = ArgumentParsers.newArgumentParser(name).description(description)
            .epilog("For more information, go to " + url).version("${prog} " + version).usage(usage);
    parser.addArgument("ACTION").type(Action.class).choices(Action.tail, Action.list).dest("action");
    parser.addArgument("-H", "--host").setDefault(defaultHost)
            .help(String.format("logstash host (default: %s)", defaultHost));
    parser.addArgument("-P", "--port").type(Integer.class).setDefault(defaultPort)
            .help(String.format("logstash TCP port (default: %d)", defaultPort));
    parser.addArgument("-f", "--fields").nargs("+").help("fields to display");
    parser.addArgument("-i", "--include").nargs("+").help("include filter (OR), example host=example.com")
            .metavar("FILTER").type(Filter.class);
    parser.addArgument("-x", "--exclude").nargs("+")
            .help("exclude filter (OR, applied after include), example: severity=debug").metavar("FILTER")
            .type(Filter.class);
    parser.addArgument("-s", "--single").action(Arguments.storeTrue()).help("display single result and exit");
    parser.addArgument("--version").action(Arguments.version()).help("output version information and exit");

    Namespace ns = parser.parseArgsOrFail(args);

    Action action = ns.get("action");
    List<String> fields = ns.getList("fields");
    List<Filter> includes = ns.getList("include");
    List<Filter> excludes = ns.getList("exclude");
    boolean single = ns.getBoolean("single");
    String host = ns.getString("host");
    int port = ns.getInt("port");

    final Socket clientSocket;
    final InputStream is;
    try {
        clientSocket = new Socket(host, port);
        is = clientSocket.getInputStream();
    } catch (IOException e) {
        throw new QuinkanaException("cannot connect to the server " + host + ":" + port, e);
    }

    // add a hook to ensure we clean the resources when leaving
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    // prepare JSON parser
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory jsonFactory = mapper.getFactory();
    JsonParser jp;
    try {
        jp = jsonFactory.createParser(is);
    } catch (IOException e) {
        throw new QuinkanaException("error during JSON parser creation", e);
    }

    JsonToken token;

    // action=list
    if (action.equals(Action.list)) {
        try {
            // do this in a separate loop to not pollute the main loop
            while ((token = jp.nextToken()) != null) {
                if (token != JsonToken.START_OBJECT)
                    continue;

                // parse object
                JsonNode node = jp.readValueAsTree();

                // print fields
                Iterator<String> fieldNames = node.fieldNames();
                while (fieldNames.hasNext())
                    System.out.println(fieldNames.next());

                System.exit(0);
            }
        } catch (IOException e) {
            throw new QuinkanaException("error during JSON parsing", e);
        }
    }

    // action=tail
    try {
        while ((token = jp.nextToken()) != null) {
            if (token != JsonToken.START_OBJECT)
                continue;

            // parse object
            JsonNode node = jp.readValueAsTree();

            // filtering (includes)
            if (includes != null) {
                boolean skip = true;
                for (Filter include : includes) {
                    if (include.match(node)) {
                        skip = false;
                        break;
                    }
                }
                if (skip)
                    continue;
            }

            // filtering (excludes)
            if (excludes != null) {
                boolean skip = false;
                for (Filter exclude : excludes) {
                    if (exclude.match(node)) {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                    continue;
            }

            // if no field specified, print raw output (JSON)
            if (fields == null) {
                System.out.println(node.toString());
                if (single)
                    break;
                continue;
            }

            // formatted output, build and print the string
            StringBuilder sb = new StringBuilder(128);
            for (String field : fields) {
                if (sb.length() > 0)
                    sb.append(" ");

                if (node.get(field) != null && node.get(field).textValue() != null)
                    sb.append(node.get(field).textValue());
            }
            System.out.println(sb.toString());

            if (single)
                break;
        }
    } catch (IOException e) {
        throw new QuinkanaException("error during JSON parsing", e);
    }
}

From source file:org.immutables.mongo.repository.internal.BsonEncoding.java

public static DBObject unwrapJsonable(String json) {
    try {//from   www .jav a  2s . c om
        JsonParser parser = JSON_FACTORY.createParser(json);
        parser.nextToken();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BsonGenerator generator = BSON_FACTORY.createGenerator(outputStream);
        generator.copyCurrentStructure(parser);
        generator.close();
        parser.close();
        byte[] data = outputStream.toByteArray();
        return (DBObject) new LazyDBCallback(null).createObject(data, 0);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:com.ntsync.shared.RequestGenerator.java

private static Map<Long, String> extractNewIdList(JsonParser jp) throws IOException {
    Map<Long, String> newIdMap = new HashMap<Long, String>();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String clientIdStr = jp.getCurrentName();
        if (jp.nextToken() == null) {
            break;
        }/*from  w  w w .j a  v a 2  s .  c o  m*/
        String serverRowId = jp.getValueAsString();
        try {
            Long clientRowId = Long.valueOf(clientIdStr);
            newIdMap.put(clientRowId, serverRowId);
        } catch (NumberFormatException ex) {
            LOG.warn("Invalid ID from server. Id:" + clientIdStr + " ServerId:" + serverRowId, ex);
        }

    }
    return newIdMap;
}

From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java

/** Parse one value for a field, which is either an
 *  object matching the type of the field, or a {boost:
 *  ..., value: ...}. *//*from   ww  w.  j  ava2s .co  m*/
private static boolean parseOneValue(FieldDef fd, JsonParser p, Document doc) throws IOException {

    Object o = null;
    float boost = 1.0f;

    JsonToken token = p.nextToken();
    if (token == JsonToken.START_ARRAY) {
        if ("hierarchy".equals(fd.faceted) || fd.valueType == FieldDef.FieldValueType.LAT_LON) {
            o = getNativeValue(fd, token, p);
        } else {
            if (fd.multiValued == false) {
                fail(fd.name, "expected single value, not array, since this field is not multiValued");
            }
            while (true) {
                if (!parseOneValue(fd, p, doc)) {
                    break;
                }
            }
            return true;
        }
    } else {

        if (token == JsonToken.END_ARRAY) {
            assert fd.multiValued;
            return false;
        }

        if (fd.fieldType.indexOptions() != IndexOptions.NONE && token == JsonToken.START_OBJECT) {
            // Parse a {boost: X, value: Y}
            while (true) {
                token = p.nextToken();
                if (token == JsonToken.END_OBJECT) {
                    break;
                }
                assert token == JsonToken.FIELD_NAME;
                String key = p.getText();
                if (key.equals("boost")) {
                    token = p.nextToken();
                    if (token == JsonToken.VALUE_NUMBER_INT || token == JsonToken.VALUE_NUMBER_FLOAT) {
                        boost = p.getFloatValue();
                    } else {
                        fail(fd.name, "boost in inner object field value must have float or int value; got: "
                                + token);
                    }
                } else if (key.equals("value")) {
                    o = getNativeValue(fd, p.nextToken(), p);
                } else {
                    fail(fd.name, "unrecognized json key \"" + key
                            + "\" in inner object field value; must be boost or value");
                }
            }
            if (o == null) {
                fail(fd.name, "missing 'value' key");
            }
        } else {
            // Parse a native value:
            o = getNativeValue(fd, token, p);
        }
    }

    parseOneNativeValue(fd, doc, o, boost);
    return true;
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Creates a json pipe from a {@link JsonParser}.
 *//*from  ww w.j a  v a2 s.  co m*/
public static Pipe newPipe(final JsonParser parser, boolean numeric) throws IOException {
    final JsonInput jsonInput = new JsonInput(parser, numeric);
    return new Pipe() {
        @Override
        protected Input begin(Pipe.Schema<?> pipeSchema) throws IOException {
            if (parser.nextToken() != JsonToken.START_OBJECT) {
                throw new JsonInputException("Expected token: { but was " + parser.getCurrentToken()
                        + " on message " + pipeSchema.wrappedSchema.messageFullName());
            }

            return jsonInput;
        }

        @Override
        protected void end(Pipe.Schema<?> pipeSchema, Input input, boolean cleanupOnly) throws IOException {
            if (cleanupOnly) {
                parser.close();
                return;
            }

            assert input == jsonInput;
            final JsonToken token = parser.getCurrentToken();

            parser.close();

            if (token != JsonToken.END_OBJECT) {
                throw new JsonInputException("Expected token: } but was " + token + " on message "
                        + pipeSchema.wrappedSchema.messageFullName());
            }
        }
    };
}

From source file:com.ntsync.shared.RequestGenerator.java

private static Restrictions parseRestr(JsonParser jp) throws IOException {
    int maxContacts = Integer.MAX_VALUE;
    int maxGroups = Integer.MAX_VALUE;
    boolean photoSyncSupported = false;
    Date validUntil = null;/*  ww  w.ja  v a2  s .  co m*/

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String configName = jp.getCurrentName();
        if (jp.nextToken() == null) {
            break;
        }
        if (PARAM_IS_PHOTO_SYNC_ENABLED.equals(configName)) {
            photoSyncSupported = jp.getBooleanValue();
        } else if (PARAM_MAX_CONTACTS.equals(configName)) {
            maxContacts = jp.getIntValue();
        } else if (PARAM_MAX_GROUPS.equals(configName)) {
            maxGroups = jp.getIntValue();
        } else if (PARAM_VALID_UNTIL.equals(configName)) {
            validUntil = new Date(jp.getLongValue());
        }
    }
    return new Restrictions(maxContacts, maxGroups, photoSyncSupported, validUntil);
}