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

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

Introduction

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

Prototype

public double getValueAsDouble() throws IOException, JsonParseException 

Source Link

Document

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

Usage

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

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

    return new Date(Math.round(jp.getValueAsDouble() * 1000));
}

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];//from   ww  w . j  a v  a 2s  .c o 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.tanrabad.survey.service.json.MultiPolygonTypeConverter.java

private Location getLocation(JsonParser jsonParser) throws IOException {
    Location value3;/*from   w w w  .j  a va  2 s  . c om*/
    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
        ArrayList<Double> point = new ArrayList<>();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            Double coordinate;
            coordinate = jsonParser.getCurrentToken() == JsonToken.VALUE_NULL ? null
                    : jsonParser.getValueAsDouble();
            point.add(coordinate);
        }
        value3 = new Location(point.get(1), point.get(0));
    } else {
        value3 = null;
    }
    return value3;
}

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./*w  w w  .j  a  va 2 s  .co m*/
 * @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:org.quantumbadger.redreader.jsonwrap.JsonValue.java

protected JsonValue(final JsonParser jp, final JsonToken firstToken) throws IOException {

    switch (firstToken) {

    case START_OBJECT:
        type = TYPE_OBJECT;// w  w  w .  j a  va2  s.c o  m
        value = new JsonBufferedObject();
        this.jp = jp;
        break;

    case START_ARRAY:
        type = TYPE_ARRAY;
        value = new JsonBufferedArray();
        this.jp = jp;
        break;

    case VALUE_FALSE:
        type = TYPE_BOOLEAN;
        value = false;
        break;

    case VALUE_TRUE:
        type = TYPE_BOOLEAN;
        value = true;
        break;

    case VALUE_NULL:
        type = TYPE_NULL;
        value = null;
        break;

    case VALUE_STRING:
        type = TYPE_STRING;
        value = jp.getValueAsString();
        break;

    case VALUE_NUMBER_FLOAT:

        //noinspection FloatingPointEquality,UnnecessaryExplicitNumericCast
        if (jp.getValueAsDouble() == (double) jp.getValueAsLong()) {
            type = TYPE_INTEGER;
            value = jp.getValueAsLong();
        } else {
            type = TYPE_FLOAT;
            value = jp.getValueAsDouble();
        }

        break;

    case VALUE_NUMBER_INT:
        type = TYPE_INTEGER;
        value = jp.getValueAsLong();
        break;

    default:
        throw new JsonParseException("Expecting an object, literal, or array", jp.getCurrentLocation());
    }
}

From source file:com.ryan.ryanreader.jsonwrap.JsonValue.java

protected JsonValue(final JsonParser jp, final JsonToken firstToken) throws IOException {

    switch (firstToken) {

    case START_OBJECT:
        type = Type.OBJECT;//  w  w  w .  j a  va  2 s  .c  om
        value = new JsonBufferedObject();
        this.jp = jp;
        break;

    case START_ARRAY:
        type = Type.ARRAY;
        value = new JsonBufferedArray();
        this.jp = jp;
        break;

    case VALUE_FALSE:
        type = Type.BOOLEAN;
        value = false;
        break;

    case VALUE_TRUE:
        type = Type.BOOLEAN;
        value = true;
        break;

    case VALUE_NULL:
        type = Type.NULL;
        value = null;
        break;

    case VALUE_STRING:
        type = Type.STRING;
        value = jp.getValueAsString();
        break;

    case VALUE_NUMBER_FLOAT:

        //noinspection FloatingPointEquality,UnnecessaryExplicitNumericCast
        if (jp.getValueAsDouble() == (double) jp.getValueAsLong()) {
            type = Type.INTEGER;
            value = jp.getValueAsLong();
        } else {
            type = Type.FLOAT;
            value = jp.getValueAsDouble();
        }

        break;

    case VALUE_NUMBER_INT:
        type = Type.INTEGER;
        value = jp.getValueAsLong();
        break;

    default:
        throw new JsonParseException("Expecting an object, literal, or array", jp.getCurrentLocation());
    }
}

From source file:org.h2gis.drivers.geojson.GeoJsonReaderDriver.java

/**
 * Parses the properties of a feature//from  w  w  w .  ja va2  s . c  om
 *
 * Syntax:
 *
 * "properties": {"prop0": "value0"}
 *
 * @param jsParser
 */
private void parseProperties(JsonParser jp, int fieldIndex) throws IOException, SQLException {
    jp.nextToken();//START_OBJECT {
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        JsonToken value = jp.nextToken();
        if (value == JsonToken.VALUE_STRING) {
            getPreparedStatement().setObject(fieldIndex, jp.getText());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_TRUE) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsBoolean());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_FALSE) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsBoolean());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NUMBER_FLOAT) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsDouble());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NUMBER_INT) {
            getPreparedStatement().setObject(fieldIndex, jp.getValueAsInt());
            fieldIndex++;
        } else if (value == JsonToken.VALUE_NULL) {
            getPreparedStatement().setObject(fieldIndex, null);
            fieldIndex++;
        } else {
            //ignore other value
        }
    }

}

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

protected void readGeoField(JsonParser par, Geo.Builder geo, String fieldName) throws IOException {
    switch (fieldName) {
    case "lat":
        geo.setLat(par.getValueAsDouble());
        break;//from   w  w  w.  jav  a 2s .com
    case "lon":
        geo.setLon(par.getValueAsDouble());
        break;
    case "type": {
        LocationType value = LocationType.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            geo.setType(value);
        }
    }
        break;
    case "country":
        geo.setCountry(par.getText());
        break;
    case "region":
        geo.setRegion(par.getText());
        break;
    case "regionfips104":
        geo.setRegionfips104(par.getText());
        break;
    case "metro":
        geo.setMetro(par.getText());
        break;
    case "city":
        geo.setCity(par.getText());
        break;
    case "zip":
        geo.setZip(par.getText());
        break;
    case "utcoffset":
        geo.setUtcoffset(par.getIntValue());
        break;
    case "accuracy":
        geo.setAccuracy(par.getIntValue());
        break;
    case "lastfix":
        geo.setLastfix(par.getIntValue());
        break;
    case "ipservice": {
        LocationService value = LocationService.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            geo.setIpservice(value);
        }
    }
        break;
    default:
        readOther(geo, par, fieldName);
    }
}

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

protected void readDealField(JsonParser par, Deal.Builder deal, String fieldName) throws IOException {
    switch (fieldName) {
    case "id":
        deal.setId(par.getText());/* w  w  w  .j  a  va  2s .  c o m*/
        break;
    case "bidfloor":
        deal.setBidfloor(par.getValueAsDouble());
        break;
    case "bidfloorcur":
        deal.setBidfloorcur(par.getText());
        break;
    case "wseat":
        for (startArray(par); endArray(par); par.nextToken()) {
            deal.addWseat(par.getText());
        }
        break;
    case "wadomain":
        for (startArray(par); endArray(par); par.nextToken()) {
            deal.addWadomain(par.getText());
        }
        break;
    case "at": {
        AuctionType value = AuctionType.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            deal.setAt(value);
        }
    }
        break;
    default:
        readOther(deal, par, fieldName);
    }
}

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

protected void readBidField(JsonParser par, Bid.Builder bid, String fieldName) throws IOException {
    switch (fieldName) {
    case "id":
        bid.setId(par.getText());/*from ww  w.  jav a2 s. c  om*/
        break;
    case "impid":
        bid.setImpid(par.getText());
        break;
    case "price":
        bid.setPrice(par.getValueAsDouble());
        break;
    case "adid":
        bid.setAdid(par.getText());
        break;
    case "nurl":
        bid.setNurl(par.getText());
        break;
    case "adm":
        if (par.getCurrentToken() == JsonToken.VALUE_STRING) {
            String valueString = par.getText();
            if (valueString.startsWith("{")) {
                bid.setAdmNative(factory().newNativeReader().readNativeResponse(valueString));
            } else {
                bid.setAdm(valueString);
            }
        } else { // Object
            bid.setAdmNative(factory().newNativeReader().readNativeResponse(par));
        }
        break;
    case "adomain":
        for (startArray(par); endArray(par); par.nextToken()) {
            bid.addAdomain(par.getText());
        }
        break;
    case "bundle":
        bid.setBundle(par.getText());
        break;
    case "iurl":
        bid.setIurl(par.getText());
        break;
    case "cid":
        bid.setCid(par.getText());
        break;
    case "crid":
        bid.setCrid(par.getText());
        break;
    case "cat":
        for (startArray(par); endArray(par); par.nextToken()) {
            String cat = par.getText();
            if (checkContentCategory(cat)) {
                bid.addCat(cat);
            }
        }
        break;
    case "attr":
        for (startArray(par); endArray(par); par.nextToken()) {
            CreativeAttribute value = CreativeAttribute.valueOf(par.getIntValue());
            if (checkEnum(value)) {
                bid.addAttr(value);
            }
        }
        break;
    case "dealid":
        bid.setDealid(par.getText());
        break;
    case "w":
        bid.setW(par.getIntValue());
        break;
    case "h":
        bid.setH(par.getIntValue());
        break;
    case "api": {
        APIFramework value = APIFramework.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            bid.setApi(value);
        }
    }
        break;
    case "protocol": {
        Protocol value = Protocol.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            bid.setProtocol(value);
        }
    }
        break;
    case "qagmediarating": {
        QAGMediaRating value = QAGMediaRating.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            bid.setQagmediarating(value);
        }
    }
        break;
    case "exp":
        bid.setExp(par.getIntValue());
        break;
    default:
        readOther(bid, par, fieldName);
    }
}