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.github.fabioticconi.roguelite.systems.BootstrapSystem.java

public void loadBody(final String filename, final EntityEdit edit) throws IOException {
    final YAMLFactory factory = new YAMLFactory();
    final JsonParser parser = factory.createParser(new File(filename));

    int str = 0, agi = 0, con = 0, skin = 0, sight = 0;
    boolean herbivore = false, carnivore = false;

    parser.nextToken(); // START_OBJECT

    while (parser.nextToken() != null) {
        final String name = parser.getCurrentName();

        if (name == null)
            break;

        parser.nextToken(); // get in value

        System.out.println(name);

        if (name.equals("strength")) {
            str = parser.getIntValue();//  w w w.j a v a 2  s.  com
            edit.create(Strength.class).value = Util.ensureRange(str, -2, 2);
        } else if (name.equals("agility")) {
            agi = parser.getIntValue();
            edit.create(Agility.class).value = Util.ensureRange(agi, -2, 2);
        } else if (name.equals("constitution")) {
            con = parser.getIntValue();
            edit.create(Constitution.class).value = Util.ensureRange(con, -2, 2);
        } else if (name.equals("skin")) {
            skin = parser.getIntValue();
            edit.create(Skin.class).value = Util.ensureRange(skin, -2, 2);
        } else if (name.equals("sight")) {
            sight = parser.getIntValue();
            edit.create(Sight.class).value = Util.ensureRange(sight, 1, 18);
        } else if (name.equals("herbivore")) {
            herbivore = parser.getBooleanValue();

            if (herbivore)
                edit.create(Herbivore.class);
        } else if (name.equals("carnivore")) {
            carnivore = parser.getBooleanValue();

            if (carnivore)
                edit.create(Carnivore.class);
        }
    }

    // TODO check if neither herbivore nor carnivore? player is currently as such, for testing

    // Secondary Attributes
    final int size = Math.round((con - agi) / 2f);
    edit.create(Size.class).value = size;
    edit.create(Stamina.class).value = 5 + str + con;
    edit.create(Speed.class).value = (con - str - agi + 6) / 12f;

    // Tertiary Attributes
    edit.create(Hunger.class).value = (size / 2f) + 2f;
}

From source file:org.oscim.utils.overpass.OverpassAPIReader.java

private void parseNode(JsonParser jp) throws JsonParseException, IOException {

    long id = 0;//from  w w w  .j  av  a2s  . c  o  m
    double lat = 0, lon = 0;
    TagSet tags = null;

    while (jp.nextToken() != JsonToken.END_OBJECT) {

        String name = jp.getCurrentName();
        jp.nextToken();

        if ("id".equals(name))
            id = jp.getLongValue();

        else if ("lat".equals(name))
            lat = jp.getDoubleValue();

        else if ("lon".equals(name))
            lon = jp.getDoubleValue();

        else if ("tags".equals(name))
            tags = parseTags(jp);

    }

    // log("node: "+id + " "+ lat + " " + lon);
    OsmNode node = new OsmNode(lat, lon, tags, id);
    ownNodes.add(node);
    nodesById.put(Long.valueOf(id), node);
}

From source file:org.springframework.security.oauth2.common.OAuth2AccessTokenJackson2Deserializer.java

@Override
public OAuth2AccessToken deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    String tokenValue = null;/*from w  w w  . j a  v  a 2 s . c om*/
    String tokenType = null;
    String refreshToken = null;
    Long expiresIn = null;
    Set<String> scope = null;
    Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();

    // TODO What should occur if a parameter exists twice
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String name = jp.getCurrentName();
        jp.nextToken();
        if (OAuth2AccessToken.ACCESS_TOKEN.equals(name)) {
            tokenValue = jp.getText();
        } else if (OAuth2AccessToken.TOKEN_TYPE.equals(name)) {
            tokenType = jp.getText();
        } else if (OAuth2AccessToken.REFRESH_TOKEN.equals(name)) {
            refreshToken = jp.getText();
        } else if (OAuth2AccessToken.EXPIRES_IN.equals(name)) {
            try {
                expiresIn = jp.getLongValue();
            } catch (JsonParseException e) {
                expiresIn = Long.valueOf(jp.getText());
            }
        } else if (OAuth2AccessToken.SCOPE.equals(name)) {
            String text = jp.getText();
            scope = OAuth2Utils.parseParameterList(text);
        } else {
            additionalInformation.put(name, jp.readValueAs(Object.class));
        }
    }

    // TODO What should occur if a required parameter (tokenValue or tokenType) is missing?

    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(tokenValue);
    accessToken.setTokenType(tokenType);
    if (expiresIn != null) {
        accessToken.setExpiration(new Date(System.currentTimeMillis() + (expiresIn * 1000)));
    }
    if (refreshToken != null) {
        accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(refreshToken));
    }
    accessToken.setScope(scope);
    accessToken.setAdditionalInformation(additionalInformation);

    return accessToken;
}

From source file:data.DefaultExchanger.java

private void importSequence(String dbName, JsonParser parser, JdbcTemplate jdbcTemplate) throws IOException {
    if (hasSequence()) {
        JsonToken fieldNameToken = parser.nextToken();
        if (fieldNameToken == JsonToken.FIELD_NAME) {
            String fieldName = parser.getCurrentName();
            if (fieldName.equalsIgnoreCase(sequenceName())) {
                JsonToken current = parser.nextToken();
                if (current == JsonToken.VALUE_NUMBER_INT) {
                    long sequenceValue = parser.getNumberValue().longValue();
                    if (dbName.equals("MySQL")) {
                        jdbcTemplate/*from  w  ww.  jav  a 2  s .  c o  m*/
                                .execute("ALTER TABLE " + getTable() + " AUTO_INCREMENT = " + sequenceValue);
                    } else if (dbName.equals("H2")) {
                        jdbcTemplate
                                .execute("ALTER SEQUENCE " + sequenceName() + " RESTART WITH " + sequenceValue);
                    }
                }
            }
        }
        play.Logger.info("imported sequence {{}}", sequenceName());
    }
}

From source file:nl.talsmasoftware.enumerables.support.json.jackson2.EnumerableDeserializer.java

private Enumerable parseObject(JsonParser jp, Class<? extends Enumerable> type) throws IOException {
    Enumerable value = null;//from ww w  . j av a  2  s  . c om
    while (true) {
        final JsonToken nextToken = jp.nextToken();
        if (nextToken == null)
            throw new IllegalStateException("JSON stream ended while parsing an Enumerable object.");

        switch (nextToken) {
        case VALUE_NULL:
        case VALUE_STRING:
            if (value == null && "value".equals(jp.getCurrentName())) {
                value = Enumerable.parse(type, jp.getText());
            }
            break;
        case END_OBJECT:
            jp.clearCurrentToken();
            if (value == null)
                throw new IllegalStateException(
                        "Attribute \"value\" is required to parse an Enumerable JSON object.");
            return value;
        case START_ARRAY:
        case START_OBJECT:
            jp.skipChildren();
            jp.clearCurrentToken();
            break;
        }
    }
}

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  ww .j  ava  2s. c o  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:com.adobe.communities.ugc.migration.importer.ScoresImportServlet.java

private void importFile(final JsonParser jsonParser, final UserPropertiesManager userManager,
        final ResourceResolver resolver) throws ServletException, IOException, RepositoryException {
    Map<String, Boolean> scoreTypes = getScoreTypes(resolver);
    JsonToken token = jsonParser.nextToken();

    while (!token.equals(JsonToken.END_OBJECT)) {
        final String authId = jsonParser.getCurrentName();
        token = jsonParser.nextToken();/*from   ww  w.  j  a  v a 2 s .  c om*/
        if (!token.equals(JsonToken.START_OBJECT)) {
            throw new ServletException("Expected to see start object, got " + token);
        }
        final Map<String, Long> scores = new HashMap<String, Long>();
        token = jsonParser.nextToken();
        while (!token.equals(JsonToken.END_OBJECT)) {
            final String scoreName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            final Long scoreValue = jsonParser.getLongValue();
            scores.put(scoreName, scoreValue);
            if (!scoreTypes.containsKey(scoreName)) {
                LOG.warn(
                        "A score of type [{}] was imported for [{}], but that score type hasn't been configured "
                                + "on this server",
                        scoreName, authId);
            }
            token = jsonParser.nextToken();
        }
        updateProfileScore(authId, scores, userManager, resolver);
        token = jsonParser.nextToken();
    }
}

From source file:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java

@Override
public Dimension.Builder deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    // Get the name first.
    String name = parseName(p, ctxt);

    if (p.getCurrentToken() == JsonToken.START_OBJECT) {
        p.nextToken();//from  w ww .  ja  va  2  s .  c  om
    }

    Dimension.Builder dimension;
    dimension = Dimension.create(name);

    while (p.nextValue() != JsonToken.END_OBJECT) {
        switch (p.getCurrentName()) {
        case "category":
            parseCategory(dimension, p, ctxt);
            break;
        case "label":
            dimension.withLabel(parseLabel(p, ctxt));
            break;
        default:
            ctxt.handleUnknownProperty(p, this, Dimension.Builder.class, p.getCurrentName());
            break;
        }
    }

    return dimension;
}

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   w w w.  j  a  v a2s  . c om
        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:com.ryan.ryanreader.jsonwrap.JsonBufferedObject.java

@Override
protected void buildBuffered(final JsonParser jp) throws IOException {

    JsonToken jt;/*from  w  w w. ja  v  a  2 s  . co m*/

    while ((jt = jp.nextToken()) != JsonToken.END_OBJECT) {

        if (jt != JsonToken.FIELD_NAME)
            throw new JsonParseException("Expecting field name, got " + jt.name(), jp.getCurrentLocation());

        final String fieldName = jp.getCurrentName();
        final JsonValue value = new JsonValue(jp);

        synchronized (this) {
            properties.put(fieldName, value);
            notifyAll();
        }

        value.buildInThisThread();
    }
}