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

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

Introduction

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

Prototype

public int getValueAsInt() throws IOException, JsonParseException 

Source Link

Document

Method that will try to convert value of current token to a int.

Usage

From source file:io.seldon.spark.actions.JobUtils.java

public static ActionData getActionDataFromActionLogLine(String actionLogLine) {
    ActionData actionData = new ActionData();

    String[] parts = actionLogLine.split("\\s+", 3);
    String json = parts[2];// ww w  .  ja  v a 2s  . co  m
    actionData.timestamp_utc = parts[0];

    JsonFactory jsonF = new JsonFactory();
    try {
        JsonParser jp = jsonF.createParser(json);
        if (jp.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // Let's move to value
            jp.nextToken();
            if (fieldName.equals("client")) {
                actionData.client = jp.getText();
            } else if (fieldName.equals("client_userid")) {
                actionData.client_userid = jp.getText();
            } else if (fieldName.equals("userid")) {
                actionData.userid = jp.getValueAsInt();
            } else if (fieldName.equals("itemid")) {
                actionData.itemid = jp.getValueAsInt();
            } else if (fieldName.equals("client_itemid")) {
                actionData.client_itemid = jp.getText();
            } else if (fieldName.equals("rectag")) {
                actionData.rectag = jp.getText();
            } else if (fieldName.equals("type")) {
                actionData.type = jp.getValueAsInt();
            } else if (fieldName.equals("value")) {
                actionData.value = jp.getValueAsDouble();
            }
        }
        jp.close();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return actionData;
}

From source file:org.ohdsi.webapi.feasibility.TheraputicAreaDeserializer.java

@Override
public TheraputicArea deserialize(JsonParser jp, DeserializationContext dc)
        throws IOException, JsonProcessingException {

    TheraputicArea type = TheraputicArea.fromId(jp.getValueAsInt());
    if (type != null) {
        return type;
    }/* w  w  w  .j ava  2 s. c o m*/
    throw new JsonMappingException("invalid value for type, must be 'one' or 'two'");
}

From source file:com.xeiam.xchange.utils.jackson.EnumIntDeserializer.java

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

    E[] constants = enumClass.getEnumConstants();
    return constants[jp.getValueAsInt() + getIndexBase()];
}

From source file:ch.rasc.wampspring.message.WelcomeMessage.java

public WelcomeMessage(JsonParser jp) throws IOException {
    super(WampMessageType.WELCOME);

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }//w  ww  .j  ava  2s  .c o m
    this.sessionId = jp.getValueAsString();

    if (jp.nextToken() != JsonToken.VALUE_NUMBER_INT) {
        throw new IOException();
    }
    this.protocolVersion = jp.getValueAsInt();

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }
    this.serverIdent = jp.getValueAsString();

}

From source file:org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService.java

/**
 * Helper method to decode and assign a primitive.
 *
 * @param token the type of token./*from   ww w  . j  ava 2s . com*/
 * @param parser the parser with the content.
 *
 * @return the decoded primitve.
 *
 * @throws IOException
 */
private Object decodePrimitive(final JsonToken token, final JsonParser parser) throws IOException {
    switch (token) {
    case VALUE_TRUE:
    case VALUE_FALSE:
        return parser.getValueAsBoolean();
    case VALUE_STRING:
        return parser.getValueAsString();
    case VALUE_NUMBER_INT:
        try {
            return parser.getValueAsInt();
        } catch (final JsonParseException e) {
            return parser.getValueAsLong();
        }
    case VALUE_NUMBER_FLOAT:
        return parser.getValueAsDouble();
    case VALUE_NULL:
        return null;
    default:
        throw new MappingException("Could not decode primitve value " + token);
    }
}

From source file:com.cedarsoft.serialization.jackson.test.compatible.JacksonCompatibleTest.java

@Test
public void testReadCompatible() throws Exception {
    JsonParser parser = jsonFactory.createJsonParser(getClass().getResource("simple.json"));

    JacksonParserWrapper wrapper = new JacksonParserWrapper(parser);
    wrapper.startObject();//ww  w.  jav a 2  s  .  c o  m
    wrapper.nextField("street");

    assertThat(parser.getText()).isEqualTo("street");
    assertThat(parser.nextToken()).isEqualTo(JsonToken.VALUE_STRING);
    assertThat(parser.getText()).isEqualTo("Schlossalle");

    wrapper.nextField("number");

    assertThat(parser.nextToken()).isEqualTo(JsonToken.VALUE_NUMBER_INT);
    assertThat(parser.getValueAsInt()).isEqualTo(7);

    assertThat(parser.nextToken()).isEqualTo(JsonToken.END_OBJECT);

    wrapper.ensureObjectClosed();
}

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

private static RawOrganizationData readOrg(String rowId, JsonParser jp) throws IOException {
    String orgname = null;//from   www.ja v a2 s .c o  m
    OrganizationType orgtype = null;
    String orgLabel = null;
    String department = null;
    String jobTitle = null;
    String title = null;
    boolean isSuperPrimary = false;
    boolean isPrimary = false;

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String namefield = jp.getCurrentName();
        // move to value
        if (jp.nextToken() == null) {
            throw new JsonParseException("Invalid JSON-Structure. End of Object missing.",
                    jp.getCurrentLocation());
        }
        if (ContactConstants.DATA.equals(namefield)) {
            orgname = jp.getValueAsString();
        } else if (ContactConstants.TYPE.equals(namefield)) {
            orgtype = OrganizationType.fromVal(jp.getValueAsInt());
        } else if (ContactConstants.PRIMARY.equals(namefield)) {
            isPrimary = jp.getValueAsBoolean();
        } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) {
            isSuperPrimary = jp.getValueAsBoolean();
        } else if (ContactConstants.LABEL.equals(namefield)) {
            orgLabel = jp.getValueAsString();
        } else if (ContactConstants.ORGANIZATION_DEPARTMENT.equals(namefield)) {
            department = jp.getValueAsString();
        } else if (ContactConstants.ORGANIZATION_TITLE.equals(namefield)) {
            title = jp.getValueAsString();
        } else if (ContactConstants.ORGANIZATION_JOB.equals(namefield)) {
            jobTitle = jp.getValueAsString();
        } else {
            LOG.error("Unrecognized Organization-field for row with Id:" + rowId + " Fieldname:" + namefield);
        }
    }

    if (orgtype == null) {
        orgtype = OrganizationType.TYPE_OTHER;
    }

    return new RawOrganizationData(orgname, orgtype, orgLabel, isPrimary, isSuperPrimary, title, department,
            jobTitle);
}

From source file:org.apache.usergrid.chop.api.RunnerDeserializer.java

@Override
public Runner deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
    RunnerBuilder builder = new RunnerBuilder();

    String tmp = jp.getText();/*from  w  w  w.  j a va  2  s .  c o m*/
    validate(jp, tmp, "{");
    LOG.debug("First token is {}", tmp);

    jp.nextToken();
    tmp = jp.getText();
    LOG.debug("Second token is {}", tmp);

    while (jp.hasCurrentToken()) {

        tmp = jp.getText();
        LOG.debug("Current token text = {}", tmp);

        if (tmp.equals("ipv4Address")) {
            jp.nextToken();
            builder.setIpv4Address(jp.getText());
        } else if (tmp.equals("hostname")) {
            jp.nextToken();
            builder.setHostname(jp.getText());
        } else if (tmp.equals("url")) {
            jp.nextToken();
            builder.setUrl(jp.getText());
        } else if (tmp.equals("serverPort")) {
            jp.nextToken();
            builder.setServerPort(jp.getValueAsInt());
        } else if (tmp.equals("tempDir")) {
            jp.nextToken();
            builder.setTempDir(jp.getText());
        }

        jp.nextToken();

        if (jp.getText().equals("}")) {
            break;
        }
    }

    return builder.getRunner();
}

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

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T extends ListType> List<ListRawData<T>> readJsonList(String rowId,
        List<ListRawData<T>> listData, JsonParser jp, String fieldname, ListType defaultType,
        Class<T> typeClass) throws IOException {
    List<ListRawData<T>> newListData = listData;
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        String number = null;/*from   w  w  w.  j  a va2s . c  om*/
        ListType type = defaultType;
        String label = null;
        boolean isSuperPrimary = false;
        boolean isPrimary = false;
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String namefield = jp.getCurrentName();
            // move to value
            if (jp.nextToken() == null) {
                throw new JsonParseException("Invalid JSON-Structure. End of Array missing.",
                        jp.getCurrentLocation());
            }
            if (ContactConstants.DATA.equals(namefield)) {
                number = jp.getValueAsString();
            } else if (ContactConstants.TYPE.equals(namefield)) {
                type = ContactConstants.fromVal(typeClass, jp.getValueAsInt());
                if (type == null) {
                    type = defaultType;
                }
            } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) {
                isSuperPrimary = jp.getValueAsBoolean();
            } else if (ContactConstants.PRIMARY.equals(namefield)) {
                isPrimary = jp.getValueAsBoolean();
            } else if (ContactConstants.LABEL.equals(namefield)) {
                label = jp.getValueAsString();
            } else {
                LOG.error(JSON_FIELDNOTRECOGNIZED + rowId + " Fieldname:" + fieldname + " Unrecognized: "
                        + namefield);
                break;
            }
        }
        if (number != null) {
            if (newListData == null) {
                newListData = new ArrayList();
            }
            newListData.add(new ListRawData(number, type, label, isPrimary, isSuperPrimary));
        }
    }
    return newListData;
}

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

private static List<RawImData> readImList(String rowId, List<RawImData> imAddresses, JsonParser jp)
        throws IOException {
    List<RawImData> newImAddresses = imAddresses;
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        ImType type = null;//from  w  w  w .  j a  v a 2s .  c om
        ImProtocolType proType = null;
        String customProctocolName = null;
        String imAddress = null;
        String imTypeLabel = null;
        boolean isSuperPrimary = false;
        boolean isPrimary = false;
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String namefield = jp.getCurrentName();
            // move to value
            if (jp.nextToken() == null) {
                throw new JsonParseException("Invalid JSON-Structure. End of Object missing.",
                        jp.getCurrentLocation());
            }
            if (ContactConstants.DATA.equals(namefield)) {
                imAddress = jp.getValueAsString();
            } else if (ContactConstants.TYPE.equals(namefield)) {
                type = ImType.fromVal(jp.getValueAsInt());
            } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) {
                isSuperPrimary = jp.getValueAsBoolean();
            } else if (ContactConstants.PRIMARY.equals(namefield)) {
                isPrimary = jp.getValueAsBoolean();
            } else if (ContactConstants.LABEL.equals(namefield)) {
                imTypeLabel = jp.getValueAsString();
            } else if (ContactConstants.PROTOCOL_TYPE.equals(namefield)) {
                proType = ImProtocolType.fromVal(jp.getValueAsInt());
            } else if (ContactConstants.PROTOCOL_CUSTOM_PROT.equals(namefield)) {
                customProctocolName = jp.getValueAsString();
            } else {
                LOG.error(JSON_FIELDNOTRECOGNIZED + rowId + " Fieldname:" + namefield);
            }
        }
        if (newImAddresses == null) {
            newImAddresses = new ArrayList<RawImData>();
        }
        if (type == null) {
            type = ImType.TYPE_OTHER;
        }

        newImAddresses.add(new RawImData(imAddress, type, imTypeLabel, isPrimary, isSuperPrimary, proType,
                customProctocolName));
    }
    return newImAddresses;
}