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

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

Introduction

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

Prototype

public boolean getValueAsBoolean() throws IOException, JsonParseException 

Source Link

Document

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

Usage

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 ww  .j  ava  2 s  . 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:ch.rasc.wampspring.message.PublishMessage.java

public PublishMessage(JsonParser jp, WampSession wampSession) throws IOException {
    super(WampMessageType.PUBLISH);

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }//from   w  w w  .  j ava  2 s  .  c  om
    setTopicURI(replacePrefix(jp.getValueAsString(), wampSession));

    jp.nextToken();
    this.event = jp.readValueAs(Object.class);

    if (jp.nextToken() != JsonToken.END_ARRAY) {
        if (jp.getCurrentToken() == JsonToken.VALUE_TRUE || jp.getCurrentToken() == JsonToken.VALUE_FALSE) {
            this.excludeMe = jp.getValueAsBoolean();

            this.exclude = null;

            this.eligible = null;

            if (jp.nextToken() != JsonToken.END_ARRAY) {
                // Wrong message format, excludeMe should not be followed by
                // any value
                throw new IOException();
            }
        } else {
            this.excludeMe = null;

            TypeReference<Set<String>> typRef = new TypeReference<Set<String>>() {
                // nothing here
            };

            if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
                throw new IOException();
            }
            this.exclude = jp.readValueAs(typRef);

            if (jp.nextToken() == JsonToken.START_ARRAY) {
                this.eligible = jp.readValueAs(typRef);
            } else {
                this.eligible = null;
            }
        }
    } else {
        this.excludeMe = null;
        this.exclude = null;
        this.eligible = null;
    }

}

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

protected void readReqAssetField(JsonParser par, NativeRequest.Asset.Builder asset, String fieldName)
        throws IOException {
    switch (fieldName) {
    case "id":
        asset.setId(par.getIntValue());/*ww  w.  jav  a 2  s.  c  o  m*/
        break;
    case "required":
        asset.setRequired(par.getValueAsBoolean());
        break;
    case "title":
        asset.setTitle(readReqTitle(par));
        break;
    case "img":
        asset.setImg(readReqImage(par));
        break;
    case "video":
        asset.setVideo(coreReader().readVideo(par));
        break;
    case "data":
        asset.setData(readReqData(par));
        break;
    default:
        readOther(asset, par, fieldName);
    }
}

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

protected void readRespAssetField(JsonParser par, NativeResponse.Asset.Builder asset, String fieldName)
        throws IOException {
    switch (fieldName) {
    case "id":
        asset.setId(par.getIntValue());/*from w w  w .j ava 2s .  co  m*/
        break;
    case "required":
        asset.setRequired(par.getValueAsBoolean());
        break;
    case "title":
        asset.setTitle(readRespTitle(par));
        break;
    case "img":
        asset.setImg(readRespImage(par));
        break;
    case "video":
        asset.setVideo(readRespVideo(par));
        break;
    case "data":
        asset.setData(readRespData(par));
        break;
    case "link":
        asset.setLink(readRespLink(par));
        break;
    default:
        readOther(asset, par, fieldName);
    }
}

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

private static RawOrganizationData readOrg(String rowId, JsonParser jp) throws IOException {
    String orgname = null;//from ww  w  . j a  v  a 2  s.  com
    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: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;/*  w w w .  j  a  va 2  s.  c o m*/
        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;
}

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  va2 s.  co m*/
        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<RawAddressData> readAddressList(String rowId, List<RawAddressData> addresses, JsonParser jp)
        throws IOException {
    List<RawAddressData> newAddresses = addresses;
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        AddressType type = null;/* w  w w. j  a  va  2s  .  c om*/
        String label = null;
        String street = null;
        String city = null;
        String postcode = null;
        String country = null;
        String region = null;
        String pobox = null;
        String neighborhood = 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.NEIGHBORHOOD.equals(namefield)) {
                neighborhood = jp.getValueAsString();
            } else if (ContactConstants.TYPE.equals(namefield)) {
                type = AddressType.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)) {
                label = jp.getValueAsString();
            } else if (ContactConstants.STREET.equals(namefield)) {
                street = jp.getValueAsString();
            } else if (ContactConstants.REGION.equals(namefield)) {
                region = jp.getValueAsString();
            } else if (ContactConstants.CITY.equals(namefield)) {
                city = jp.getValueAsString();
            } else if (ContactConstants.POSTCODE.equals(namefield)) {
                postcode = jp.getValueAsString();
            } else if (ContactConstants.COUNTRY.equals(namefield)) {
                country = jp.getValueAsString();
            } else if (ContactConstants.REGION.equals(namefield)) {
                region = jp.getValueAsString();
            } else if (ContactConstants.POBOX.equals(namefield)) {
                pobox = jp.getValueAsString();
            } else {
                LOG.error(JSON_FIELDNOTRECOGNIZED + rowId + " Fieldname:" + namefield);
            }
        }
        if (newAddresses == null) {
            newAddresses = new ArrayList<RawAddressData>();
        }
        if (type == null) {
            type = AddressType.TYPE_OTHER;
        }

        newAddresses.add(new RawAddressData(type, label, isPrimary, isSuperPrimary, street, pobox, neighborhood,
                city, region, postcode, country));
    }
    return newAddresses;
}

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

/**
 * Parses the properties of a feature//from  ww w  .  ja v a  2 s. c  o  m
 *
 * 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.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();
                }//from   w w  w  .ja  v a2 s .  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;
}