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

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

Introduction

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

Prototype

public abstract String getCurrentName() throws IOException, JsonParseException;

Source Link

Document

Method that can be called to get the name associated with the current token: for JsonToken#FIELD_NAME s it will be the same as what #getText returns; for field values it will be preceding field name; and for others (array values, root-level values) null.

Usage

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

/**
 * /*www.j a  v a 2 s  .c om*/
 * @param key
 * @param clientId
 * @param response
 * @return
 * @throws HeaderParseException
 */
public static SyncResponse processServerResponse(SecretKey key, String clientId, final byte[] response)
        throws HeaderParseException {
    short version = SyncDataHelper.readShort(response, 0);
    SyncState syncState = null;

    Map<Long, String> newGroupIdMap = null;
    Map<Long, String> newContactIdMap = null;
    SyncAnchor newSyncAnchor = new SyncAnchor();
    int skippedRows = 0;
    List<RawContact> serverContactList = new ArrayList<RawContact>();
    List<ContactGroup> serverGroupList = new ArrayList<ContactGroup>();

    String newClientId = clientId;
    Restrictions restr = null;
    if (version == RequestGenerator.PROT_VERSION) {
        int headerLength = SyncDataHelper.readInt(response, 2);

        JsonParser jp = null;
        try {
            jp = getJsonFactory().createParser(response, HEADER_POS, headerLength);
            jp.nextToken();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldname = jp.getCurrentName();
                // move to value, or START_OBJECT/START_ARRAY
                if (jp.nextToken() == null) {
                    break;
                }
                if (CLIENT_FIELD_NAME.equals(fieldname)) {
                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                        String clientField = jp.getCurrentName();
                        if (jp.nextToken() == null) {
                            break;
                        }
                        if (PARAM_SYNC_ANCHOR.equals(clientField)) {
                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                String anchorType = jp.getCurrentName();
                                if (jp.nextToken() == null) {
                                    break;
                                }
                                long syncAnchor = jp.getLongValue();
                                if (anchorType != null && anchorType.length() > 0) {
                                    newSyncAnchor.setAnchor((byte) anchorType.charAt(0), syncAnchor);
                                }
                            }
                        } else if (PARAM_CLIENTID.equals(clientField)) {
                            newClientId = jp.getValueAsString();
                        } else if (TAG_GROUPIDS.equals(clientField)) {
                            newGroupIdMap = extractNewIdList(jp);
                        } else if (TAG_CONTACTIDS.equals(clientField)) {
                            newContactIdMap = extractNewIdList(jp);
                        } else {
                            LOG.warn("Unsupported Client-Header-Field: {}", clientField);
                        }
                    }
                } else if (SERVER_FIELD_NAME.equals(fieldname)) {
                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                        String serverField = jp.getCurrentName();
                        if (jp.nextToken() == null) {
                            break;
                        }
                        if (RequestGenerator.SYNCSTATE_FIELD_NAME.equals(serverField)) {
                            String syncStateStr = jp.getValueAsString();
                            if (syncStateStr != null && syncStateStr.length() > 0) {
                                syncState = SyncState.fromErrorVal(syncStateStr);
                            }
                        } else if (RequestGenerator.TAG_SERVER_CONFIG.equals(serverField)) {
                            restr = parseRestr(jp);
                        }
                    }

                }
            }

            final int respLen = response.length;

            if (respLen > headerLength + HEADER_POS) {
                skippedRows = getUpdatedRows(key, serverContactList, serverGroupList, response, headerLength,
                        respLen);
            }
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        } catch (JsonParseException ex) {
            throw new HeaderParseException(ex);
        } catch (IOException e) {
            throw new HeaderParseException(e);
        } finally {
            if (jp != null) {
                try {
                    jp.close();
                } catch (IOException ex) {
                    LOG.warn("Could not close JSONParser", ex);
                }
            }
        }
    }

    return new SyncResponse(syncState, serverContactList, serverGroupList, newSyncAnchor, newClientId,
            newGroupIdMap, newContactIdMap, skippedRows, restr);
}

From source file:org.talend.components.jira.datum.EntityParser.java

/**
 * Rewinds {@link JsonParser} to the value of specified field
 * //  w w w  . j  a  v a  2s .co  m
 * @param parser JSON parser
 * @param fieldName name of field rewind to
 * @return true if field was found, false otherwise
 * @throws IOException in case of exception during JSON parsing
 */
private static boolean rewindToField(JsonParser parser, final String fieldName) throws IOException {

    JsonToken currentToken = parser.nextToken();
    /*
     * There is no special token, which denotes end of file, in Jackson.
     * This counter is used to define the end of file.
     * The counter counts '{' and '}'. It is increased, when meets '{' and
     * decreased, when meets '}'. When braceCounter == 0 it means the end
     * of file was met
     */
    int braceCounter = 0;
    String currentField = null;
    do {
        if (JsonToken.START_OBJECT == currentToken) {
            braceCounter++;
        }
        if (JsonToken.END_OBJECT == currentToken) {
            braceCounter--;
        }
        if (JsonToken.FIELD_NAME == currentToken) {
            currentField = parser.getCurrentName();
        }
        currentToken = parser.nextToken();
    } while (!fieldName.equals(currentField) && braceCounter != END_JSON);

    return braceCounter != END_JSON;
}

From source file:timezra.dropbox.maven.plugin.client.DbxClientWrapper.java

private static <C> WithChildrenC<C> readWithNulls(final JsonParser parser,
        final Collector<DbxEntry, ? extends C> collector) throws IOException, JsonReadException {
    final JsonLocation top = JsonReader.expectObjectStart(parser);

    String size = null;/*w  ww .j av  a 2 s. c o  m*/
    long bytes = -1;
    String path = null;
    Boolean is_dir = null;
    Boolean is_deleted = null;
    String rev = null;
    Boolean thumb_exists = null;
    String icon = null;
    Date modified = null;
    Date client_mtime = null;
    String hash = null;
    C contents = null;

    while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
        final String fieldName = parser.getCurrentName();
        JsonReader.nextToken(parser);

        final int fi = FM.get(fieldName);
        try {
            switch (fi) {
            case -1:
                JsonReader.skipValue(parser);
                break;
            case FM_size:
                size = JsonReader.StringReader.readField(parser, fieldName, size);
                break;
            case FM_bytes:
                bytes = JsonReader.readUnsignedLongField(parser, fieldName, bytes);
                break;
            case FM_path:
                path = JsonReader.StringReader.readField(parser, fieldName, path);
                break;
            case FM_is_dir:
                is_dir = JsonReader.BooleanReader.readField(parser, fieldName, is_dir);
                break;
            case FM_is_deleted:
                is_deleted = JsonReader.BooleanReader.readField(parser, fieldName, is_deleted);
                break;
            case FM_rev:
                rev = JsonReader.StringReader.readField(parser, fieldName, rev);
                break;
            case FM_thumb_exists:
                thumb_exists = JsonReader.BooleanReader.readField(parser, fieldName, thumb_exists);
                break;
            case FM_icon:
                icon = JsonReader.StringReader.readField(parser, fieldName, icon);
                break;
            case FM_modified:
                modified = JsonDateReader.Dropbox.readField(parser, fieldName, modified);
                break;
            case FM_client_mtime:
                client_mtime = JsonDateReader.Dropbox.readField(parser, fieldName, client_mtime);
                break;
            case FM_hash:
                if (collector == null) {
                    throw new JsonReadException(
                            "not expecting \"hash\" field, since we didn't ask for children",
                            parser.getCurrentLocation());
                }
                hash = JsonReader.StringReader.readField(parser, fieldName, hash);
                break;
            case FM_contents:
                if (collector == null) {
                    throw new JsonReadException(
                            "not expecting \"contents\" field, since we didn't ask for children",
                            parser.getCurrentLocation());
                }
                contents = JsonArrayReader.mk(Reader, collector).readField(parser, fieldName, contents);
                break;
            default:
                throw new AssertionError("bad index: " + fi + ", field = \"" + fieldName + "\"");
            }
        } catch (final JsonReadException ex) {
            throw ex.addFieldContext(fieldName);
        }
    }

    JsonReader.expectObjectEnd(parser);

    if (path == null) {
        throw new JsonReadException("missing field \"path\"", top);
    }
    if (icon == null) {
        throw new JsonReadException("missing field \"icon\"", top);
    }
    if (is_deleted == null) {
        is_deleted = Boolean.FALSE;
    }
    if (is_dir == null) {
        is_dir = Boolean.FALSE;
    }
    if (thumb_exists == null) {
        thumb_exists = Boolean.FALSE;
    }

    if (is_dir && (contents != null || hash != null)) {
        if (hash == null) {
            throw new JsonReadException("missing \"hash\", when we asked for children", top);
        }
        if (contents == null) {
            throw new JsonReadException("missing \"contents\", when we asked for children", top);
        }
    }

    DbxEntry e;
    if (is_dir) {
        e = new Folder(path, icon, thumb_exists);
    } else {
        // Normal File
        if (size == null) {
            throw new JsonReadException("missing \"size\" for a file entry", top);
        }
        if (bytes == -1) {
            throw new JsonReadException("missing \"bytes\" for a file entry", top);
        }
        if (modified == null) {
            throw new JsonReadException("missing \"modified\" for a file entry", top);
        }
        if (client_mtime == null) {
            throw new JsonReadException("missing \"client_mtime\" for a file entry", top);
        }
        if (rev == null) {
            throw new JsonReadException("missing \"rev\" for a file entry", top);
        }
        e = new File(path, icon, thumb_exists, bytes, size, modified, client_mtime, rev);
    }

    return new WithChildrenC<C>(e, hash, contents);
}

From source file:com.google.openrtb.json.OpenRtbJsonExtReader.java

protected final boolean filter(JsonParser par) throws IOException {
    return rootNameFilters.isEmpty() || rootNameFilters.contains(par.getCurrentName());
}

From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java

public static Map<String, Object> extractSubmap(final JsonParser jsonParser) throws IOException {
    jsonParser.nextToken(); // skip the START_OBJECT token
    final Map<String, Object> subMap = new HashMap<String, Object>();
    while (!jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) {
        final String label = jsonParser.getCurrentName(); // get the current label
        final JsonToken token = jsonParser.nextToken(); // get the current value
        if (!token.isScalarValue()) {
            if (token.equals(JsonToken.START_OBJECT)) {
                // if the next token starts a new object, recurse into it
                subMap.put(label, extractSubmap(jsonParser));
            } else if (token.equals(JsonToken.START_ARRAY)) {
                final List<String> subArray = new ArrayList<String>();
                jsonParser.nextToken(); // skip the START_ARRAY token
                while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) {
                    subArray.add(jsonParser.getValueAsString());
                    jsonParser.nextToken();
                }/*  w  w  w.ja  v a  2s .  c  o  m*/
                subMap.put(label, subArray);
                jsonParser.nextToken(); // skip the END_ARRAY token
            }
        } else {
            // either a string, boolean, or long value
            if (token.isNumeric()) {
                subMap.put(label, jsonParser.getValueAsLong());
            } else {
                final String value = jsonParser.getValueAsString();
                if (value.equals("true") || value.equals("false")) {
                    subMap.put(label, jsonParser.getValueAsBoolean());
                } else {
                    subMap.put(label, value);
                }
            }
        }
        jsonParser.nextToken(); // next token will either be an "END_OBJECT" or a new label
    }
    jsonParser.nextToken(); // skip the END_OBJECT token
    return subMap;
}

From source file:org.dbrain.data.jackson.serializers.JacksonSerializationUtils.java

public static Value parseValue(JsonParser parser, DeserializationContext ctxt) throws IOException {
    JsonToken token = getToken(parser);/*  w w  w.  j  a  v a2s.  c o  m*/
    if (token != null) {
        Value result;
        switch (token) {
        case VALUE_STRING:
            result = Value.of(parser.getValueAsString());
            break;
        case VALUE_NUMBER_FLOAT:
            result = Value.of(parser.getDoubleValue());
            break;
        case VALUE_NUMBER_INT:
            result = Value.of(parser.getBigIntegerValue());
            break;
        case VALUE_NULL:
            result = NullValueImpl.NULL;
            break;
        case VALUE_TRUE:
            result = Value.of(Boolean.TRUE);
            break;
        case VALUE_FALSE:
            result = Value.of(Boolean.FALSE);
            break;
        case START_OBJECT: {
            ValueMap values = ValueMap.newInstance();
            while (parser.nextToken() == JsonToken.FIELD_NAME) {
                String key = parser.getCurrentName();
                parser.nextToken();
                Value v = parseValue(parser, ctxt);
                if (v == null) {
                    throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
                }
                values.put(key, v);
            }
            if (getToken(parser) == JsonToken.END_OBJECT) {
                parser.clearCurrentToken();
            } else {
                throw ctxt.wrongTokenException(parser, JsonToken.END_OBJECT, null);
            }
            result = values;
        }
            break;
        case START_ARRAY: {
            ValueList values = ValueList.newInstance();
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                Value v = parseValue(parser, ctxt);
                if (v == null) {
                    throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
                }
                values.add(v);
            }
            if (getToken(parser) == JsonToken.END_ARRAY) {
                parser.clearCurrentToken();
            } else {
                throw ctxt.wrongTokenException(parser, JsonToken.END_ARRAY, null);
            }
            result = values;
        }
            break;
        default:
            throw ctxt.wrongTokenException(parser, JsonToken.START_OBJECT, "Expected Value");
        }
        return result;
    } else {
        return null;
    }
}

From source file:monasca.log.api.app.validation.LogApplicationTypeValidationTest.java

private String getMessage(String json) throws JsonParseException, IOException {
    JsonFactory factory = new JsonFactory();
    JsonParser jp = factory.createParser(json);
    jp.nextToken();// ww w  .j a va 2  s  .com
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        jp.nextToken();
        if ("message".equals(fieldname)) {

            return jp.getText();
        }
    }
    jp.close();
    return null;
}

From source file:com.netflix.discovery.converters.jackson.serializer.PortWrapperXmlDeserializer.java

@Override
public InstanceInfo.PortWrapper deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    boolean enabled = false;
    int port = 0;
    while (jp.nextToken() == JsonToken.FIELD_NAME) {
        String fieldName = jp.getCurrentName();
        jp.nextToken(); // to point to value
        if ("enabled".equals(fieldName)) {
            enabled = Boolean.valueOf(jp.getValueAsString());
        } else if (fieldName == null || "".equals(fieldName)) {
            String value = jp.getValueAsString();
            port = value == null ? 0 : Integer.parseInt(value);
        } else {//from  ww  w . j a  v a  2  s  . c o m
            throw new JsonMappingException("Unexpected field " + fieldName, jp.getCurrentLocation());
        }
    }
    return new InstanceInfo.PortWrapper(enabled, port);
}

From source file:GetAppConfig.java

private void getConf() {
    String line;/*from w  w w . j  a v a2 s .c  o  m*/
    String json = "";
    try {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(this.jsonfile), "UTF-8"));
        while ((line = reader.readLine()) != null) {
            json += line;
        }
        reader.close();
    } catch (Exception e) {
        System.out.println("Error: readconf(): " + e.getMessage());
        return;
    }

    JsonFactory factory = new JsonFactory();
    try {
        JsonParser parser = factory.createParser(json);
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            if (name == null)
                continue;
            parser.nextToken();
            if (name.equals("userid")) {
                this.setUserID(parser.getText());
            } else if (name.equals("passwd")) {
                this.setPassword(parser.getText());
            } else if (name.equals("deviceid")) {
                this.setDeviceID(parser.getText());
            } else if (name.equals("pkgdir")) {
                this.setPackageDir(parser.getText());
            } else {
                parser.skipChildren();
            }
        } // while
    } catch (Exception e) {
        System.out.println("Error: parseconf(): " + e.getMessage());
    }
}

From source file:ijfx.core.metadata.MetaDataDeserializer.java

@Override
public MetaDataSet deserialize(JsonParser jp, DeserializationContext dc)
        throws IOException, JsonProcessingException {
    MetaDataSet m = new MetaDataSet();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fileName = jp.getCurrentName();
        jp.nextToken();//from   w  w w  .  ja  v a2  s.c o  m
        String value = jp.getText();

        m.putGeneric(fileName, value);

    }
    return m;
}