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:invar.lib.data.DataParserJson.java

private void parse(JsonParser parser, DataNode root) throws IOException {
    String fieldName = null;/*w ww  .  j a v a2  s .  c o  m*/
    DataNode parent = root;
    while (!parser.isClosed()) {
        JsonToken token = parser.nextToken();
        if (token == null) {
            continue;
        }
        switch (token) {
        case START_ARRAY:
            parent.addChild(parent = DataNode.createArray().setFieldName(fieldName));
            fieldName = null;
            break;
        case END_ARRAY:
            parent = parent.getParent();
            fieldName = null;
            break;
        case START_OBJECT:
            parent.addChild(parent = DataNode.createObject().setFieldName(fieldName));
            fieldName = null;
            break;
        case END_OBJECT:
            parent = parent.getParent();
            fieldName = null;
            break;
        case VALUE_TRUE:
            parent.addChild(DataNode.createBoolean().setValue(true).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_FALSE:
            parent.addChild(DataNode.createBoolean().setValue(false).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_NULL:
            parent.addChild(DataNode.createNull().setValue(null).setFieldName(fieldName));
            fieldName = null;
            break;
        case VALUE_STRING:
            parent.addChild(
                    DataNode.createString().setFieldName(fieldName).setValue(parser.getValueAsString()));
            fieldName = null;
            break;
        case VALUE_NUMBER_INT:
            try {
                Long v = parser.getValueAsLong();
                parent.addChild(DataNode.createLong().setFieldName(fieldName).setValue(v));
            } catch (JsonParseException e) {
                BigInteger v = parser.getBigIntegerValue();
                parent.addChild(DataNode.createBigInt().setFieldName(fieldName).setValue(v));
            }
            fieldName = null;
            break;
        case VALUE_NUMBER_FLOAT:
            parent.addChild(
                    DataNode.createDouble().setFieldName(fieldName).setValue(parser.getValueAsDouble()));
            fieldName = null;
            break;
        case FIELD_NAME:
            fieldName = parser.getValueAsString();
            break;
        default:
            fieldName = null;
            break;
        }
    }
}

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

protected void readImpField(JsonParser par, Imp.Builder imp, String fieldName) throws IOException {
    switch (fieldName) {
    case "id":
        imp.setId(par.getText());//w  w w  .  ja v  a  2 s .  co m
        break;
    case "banner":
        imp.setBanner(readBanner(par));
        break;
    case "video":
        imp.setVideo(readVideo(par));
        break;
    case "audio":
        imp.setAudio(readAudio(par));
        break;
    case "native":
        imp.setNative(readNative(par));
        break;
    case "displaymanager":
        imp.setDisplaymanager(par.getText());
        break;
    case "displaymanagerver":
        imp.setDisplaymanagerver(par.getText());
        break;
    case "instl":
        imp.setInstl(par.getValueAsBoolean());
        break;
    case "tagid":
        imp.setTagid(par.getText());
        break;
    case "bidfloor":
        imp.setBidfloor(par.getValueAsDouble());
        break;
    case "bidfloorcur":
        imp.setBidfloorcur(par.getText());
        break;
    case "secure":
        imp.setSecure(par.getValueAsBoolean());
        break;
    case "iframebuster":
        for (startArray(par); endArray(par); par.nextToken()) {
            imp.addIframebuster(par.getText());
        }
        break;
    case "pmp":
        imp.setPmp(readPmp(par));
        break;
    case "clickbrowser":
        imp.setClickbrowser(par.getValueAsBoolean());
        break;
    case "exp":
        imp.setExp(par.getIntValue());
        break;
    default:
        readOther(imp, par, fieldName);
    }
}

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

protected void readDeviceField(JsonParser par, Device.Builder device, String fieldName) throws IOException {
    switch (fieldName) {
    case "ua":
        device.setUa(par.getText());//w ww . j a  v a  2 s.  c om
        break;
    case "geo":
        device.setGeo(readGeo(par));
        break;
    case "dnt":
        device.setDnt(par.getValueAsBoolean());
        break;
    case "lmt":
        device.setLmt(par.getValueAsBoolean());
        break;
    case "ip":
        device.setIp(par.getText());
        break;
    case "ipv6":
        device.setIpv6(par.getText());
        break;
    case "devicetype": {
        DeviceType value = DeviceType.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            device.setDevicetype(value);
        }
    }
        break;
    case "make":
        device.setMake(par.getText());
        break;
    case "model":
        device.setModel(par.getText());
        break;
    case "os":
        device.setOs(par.getText());
        break;
    case "osv":
        device.setOsv(par.getText());
        break;
    case "hwv":
        device.setHwv(par.getText());
        break;
    case "w":
        device.setW(par.getIntValue());
        break;
    case "h":
        device.setH(par.getIntValue());
        break;
    case "ppi":
        device.setPpi(par.getIntValue());
        break;
    case "pxratio":
        device.setPxratio(par.getValueAsDouble());
        break;
    case "js":
        device.setJs(par.getValueAsBoolean());
        break;
    case "flashver":
        device.setFlashver(par.getText());
        break;
    case "language":
        device.setLanguage(par.getText());
        break;
    case "carrier":
        device.setCarrier(par.getText());
        break;
    case "connectiontype": {
        ConnectionType value = ConnectionType.valueOf(par.getIntValue());
        if (checkEnum(value)) {
            device.setConnectiontype(value);
        }
    }
        break;
    case "ifa":
        device.setIfa(par.getText());
        break;
    case "didsha1":
        device.setDidsha1(par.getText());
        break;
    case "didmd5":
        device.setDidmd5(par.getText());
        break;
    case "dpidsha1":
        device.setDpidsha1(par.getText());
        break;
    case "dpidmd5":
        device.setDpidmd5(par.getText());
        break;
    case "macsha1":
        device.setMacsha1(par.getText());
        break;
    case "macmd5":
        device.setMacmd5(par.getText());
        break;
    case "geofetch":
        device.setGeofetch(par.getValueAsBoolean());
        break;
    default:
        readOther(device, par, fieldName);
    }
}

From source file:org.wso2.extension.siddhi.map.json.sourcemapper.JsonSourceMapper.java

private Event convertToSingleEventForDefaultMapping(Object eventObject) throws IOException {
    Event event = new Event(attributesSize);
    Object[] data = event.getData();
    JsonParser parser;
    int numberOfProvidedAttributes = 0;
    try {//from   w w w. ja  v  a 2  s.c  o m
        parser = factory.createParser(eventObject.toString());
    } catch (IOException e) {
        throw new SiddhiAppRuntimeException(
                "Initializing a parser failed for the event string." + eventObject.toString());
    }
    int position;
    while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();
        if (JsonToken.START_OBJECT.equals(jsonToken)) {
            parser.nextToken();
            if (DEFAULT_JSON_EVENT_IDENTIFIER.equalsIgnoreCase(parser.getText())) {
                parser.nextToken();
            } else {
                log.error("Default json message " + eventObject
                        + " contains an invalid event identifier. Required \"event\", " + "but found \""
                        + parser.getText() + "\". Hence dropping the message.");
                return null;
            }
        } else if (JsonToken.FIELD_NAME.equals(jsonToken)) {
            String key = parser.getCurrentName();
            numberOfProvidedAttributes++;
            position = findDefaultMappingPosition(key);
            if (position == -1) {
                log.error("Stream \"" + streamDefinition.getId() + "\" does not have an attribute named \""
                        + key + "\", but the received event " + eventObject.toString()
                        + " does. Hence dropping the message.");
                return null;
            }
            jsonToken = parser.nextToken();
            Attribute.Type type = streamAttributes.get(position).getType();

            if (JsonToken.VALUE_NULL.equals(jsonToken)) {
                data[position] = null;
            } else {
                switch (type) {
                case BOOL:
                    if (JsonToken.VALUE_TRUE.equals(jsonToken) || JsonToken.VALUE_FALSE.equals(jsonToken)) {
                        data[position] = parser.getValueAsBoolean();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type BOOL. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case INT:
                    if (JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) {
                        data[position] = parser.getValueAsInt();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type INT. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case DOUBLE:
                    if (JsonToken.VALUE_NUMBER_FLOAT.equals(jsonToken)) {
                        data[position] = parser.getValueAsDouble();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type DOUBLE. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case STRING:
                    if (JsonToken.VALUE_STRING.equals(jsonToken)) {
                        data[position] = parser.getValueAsString();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type STRING. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case FLOAT:
                    if (JsonToken.VALUE_NUMBER_FLOAT.equals(jsonToken)
                            || JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) {
                        data[position] = attributeConverter.getPropertyValue(parser.getValueAsString(),
                                Attribute.Type.FLOAT);
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type FLOAT. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                case LONG:
                    if (JsonToken.VALUE_NUMBER_INT.equals(jsonToken)) {
                        data[position] = parser.getValueAsLong();
                    } else {
                        log.error("Json message " + eventObject.toString()
                                + " contains incompatible attribute types and values. Value " + parser.getText()
                                + " is not compatible with type LONG. " + "Hence dropping the message.");
                        return null;
                    }
                    break;
                default:
                    return null;
                }
            }
        }
    }

    if (failOnMissingAttribute && (numberOfProvidedAttributes != attributesSize)) {
        log.error("Json message " + eventObject.toString()
                + " contains missing attributes. Hence dropping the message.");
        return null;
    }
    return event;
}