Example usage for com.google.gson JsonElement getAsBoolean

List of usage examples for com.google.gson JsonElement getAsBoolean

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsBoolean.

Prototype

public boolean getAsBoolean() 

Source Link

Document

convenience method to get this element as a boolean value.

Usage

From source file:org.apache.lens.driver.es.client.jest.JestResultSetTransformer.java

License:Apache License

protected Object getTypedValue(int colPosition, JsonElement jsonObjectValue) {
    final Type type = getDataType(colPosition, jsonObjectValue);
    switch (type) {
    case NULL_TYPE:
        return null;
    case DOUBLE_TYPE:
        return jsonObjectValue.getAsDouble();
    case BOOLEAN_TYPE:
        return jsonObjectValue.getAsBoolean();
    default://from w  ww . ja  v  a 2  s .  co m
        return jsonObjectValue.getAsString();
    }
}

From source file:org.apache.metamodel.elasticsearch.elastic1.rest.JestElasticSearchUtils.java

License:Apache License

private static Object getDataFromColumnType(JsonElement field, ColumnType type) {
    if (field == null || field.isJsonNull()) {
        return null;
    }//from ww  w  . j  av a  2  s .c  o m

    if (field.isJsonObject()) {
        return new Gson().fromJson(field, Map.class);
    }
    if (field.isJsonArray()) {
        return new Gson().fromJson(field, List.class);
    }

    if (type.isNumber()) {
        // Pretty terrible workaround to avoid LazilyParsedNumber
        // (which is happily output, but not recognized by Jest/GSON).
        return NumberComparator.toNumber(field.getAsString());
    } else if (type.isTimeBased()) {
        final Date valueToDate = ElasticSearchDateConverter.tryToConvert(field.getAsString());
        if (valueToDate == null) {
            return field.getAsString();
        } else {
            return valueToDate;
        }
    } else if (type.isBoolean()) {
        return field.getAsBoolean();
    } else {
        return field.getAsString();
    }
}

From source file:org.broad.igv.feature.genome.GenomeManager.java

License:Open Source License

private Genome loadJsonFile(String genomePath) throws IOException {

    Genome newGenome = null;/*  ww w .  j av  a  2s  .co  m*/

    BufferedReader reader = ParsingUtils.openBufferedReader(genomePath);
    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(reader).getAsJsonObject();

    String id = json.get("id").getAsString();
    String name = json.get("name").getAsString();
    String fastaPath = json.get("fastaURL").getAsString();
    JsonElement indexPathObject = json.get("indexURL");
    String indexPath = indexPathObject == null ? null : indexPathObject.getAsString();

    FastaIndexedSequence sequence = fastaPath.endsWith(".gz")
            ? new FastaBlockCompressedSequence(fastaPath, indexPath)
            : new FastaIndexedSequence(fastaPath, indexPath);

    ArrayList<ResourceLocator> tracks = new ArrayList<>();
    JsonArray annotations = json.getAsJsonArray("annotations");
    if (annotations != null) {
        annotations.forEach((JsonElement jsonElement) -> {
            JsonObject obj = jsonElement.getAsJsonObject();
            String trackPath = obj.get("url").getAsString();
            JsonElement trackName = obj.get("name");
            JsonElement trackIndexPath = obj.get("indexURL");
            JsonElement indexed = obj.get("indexed");
            JsonElement aliasURL = obj.get("aliasURL");

            ResourceLocator res = new ResourceLocator(trackPath);
            if (trackName != null)
                res.setName(trackName.getAsString());
            if (trackIndexPath != null)
                res.setIndexPath(trackIndexPath.getAsString());
            if (indexed != null)
                res.setIndexed(indexed.getAsBoolean());
            tracks.add(res);
        });
    }

    newGenome = new Genome(id, name, sequence, true);

    newGenome.setAnnotationResources(tracks);

    // TODO -- set aliases

    return newGenome;

}

From source file:org.commonjava.couch.change.CouchDocChangeDeserializer.java

License:Apache License

@Override
public CouchDocChange deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject record = json.getAsJsonObject();
    final int seq = record.get(SEQ).getAsInt();
    final String id = record.get(ID).getAsString();

    final JsonElement element = record.get(DELETED);
    final boolean deleted = element == null ? false : element.getAsBoolean();

    final JsonArray changesArray = record.getAsJsonArray(CHANGES_ARRAY);
    final List<String> revs = new ArrayList<String>(changesArray.size());
    for (final JsonElement revRecord : changesArray) {
        revs.add(revRecord.getAsJsonObject().get(REV).getAsString());
    }//from  w  w w  .j av  a2 s. com

    return new CouchDocChange(seq, id, revs, deleted);
}

From source file:org.dartlang.vm.service.element.Instance.java

License:Open Source License

/**
 * The valueAsString for String references may be truncated. If so, this property is added with
 * the value 'true'.//from w w  w . j a v  a2s . co m
 */
public boolean getValueAsStringIsTruncated() {
    JsonElement elem = json.get("valueAsStringIsTruncated");
    return elem != null ? elem.getAsBoolean() : false;
}

From source file:org.displaytag.model.CustomColumnData.java

License:Artistic License

private Boolean getBooleanValue(JsonObject jsonObj, String name) {
    if (jsonObj.has(name)) {
        JsonElement e = jsonObj.get(name);
        return Boolean.TRUE.equals(e.getAsBoolean());
    }/*from   w  ww .  ja v a  2s .c  o m*/
    return Boolean.FALSE;
}

From source file:org.eclipse.che.api.core.jsonrpc.JsonRpcUtils.java

License:Open Source License

static <T> T getAs(JsonElement element, Class<T> type) {
    if (type.equals(String.class)) {
        return cast(element.getAsString());
    } else if (type.equals(Double.class)) {
        return cast(element.getAsDouble());
    } else if (type.equals(Boolean.class)) {
        return cast(element.getAsBoolean());
    } else if (type.equals(Void.class)) {
        return null;
    } else {//  w  ww.  ja  v  a2s . c  o m
        return DtoFactory.getInstance().createDtoFromJson(element.toString(), type);
    }
}

From source file:org.eclipse.dirigible.repository.ext.db.model.TableModel.java

License:Open Source License

private void fillColumns(JsonObject definitionObject) throws EDataStructureModelFormatException {
    // columns/*from www  .j  ava  2 s .  co m*/
    JsonElement columnsElement = definitionObject.get(COLUMNS);
    if (columnsElement == null) {
        throw new EDataStructureModelFormatException(String
                .format(DataStructureModel.ELEMENT_S_DOES_NOT_EXIST_IN_THIS_MODEL_S, COLUMNS, this.getName()));
    }
    if (!columnsElement.isJsonArray()) {
        throw new EDataStructureModelFormatException(String
                .format(DataStructureModel.ELEMENT_S_MUST_BE_ARRAY_IN_THE_MODEL_S, COLUMNS, this.getName()));
    }
    JsonArray columnsArray = columnsElement.getAsJsonArray();
    for (JsonElement jsonElement : columnsArray) {
        if (jsonElement instanceof JsonObject) {
            JsonObject jsonObject = (JsonObject) jsonElement;

            // column's name
            JsonElement columnNameElement = jsonObject.get(COLUMN_NAME);
            if (columnNameElement == null) {
                throw new EDataStructureModelFormatException(
                        String.format(ELEMENT_S_DOES_NOT_EXIST_IN_THIS_COLUMNS_ARRAY_IN_THE_TABLE_MODEL_S,
                                COLUMN_NAME, this.getName()));
            }
            if (columnNameElement instanceof JsonArray) {
                throw new EDataStructureModelFormatException(String.format(
                        DataStructureModel.ELEMENT_S_MUST_BE_A_SINGLE_ELEMENT_NOT_AN_ARRAY_IN_THIS_MODEL_S,
                        COLUMN_NAME, this.getName()));
            }
            String columnName = columnNameElement.getAsString();

            // column's type
            JsonElement columnTypeElement = jsonObject.get(COLUMN_TYPE);
            if (columnTypeElement == null) {
                throw new EDataStructureModelFormatException(
                        String.format(ELEMENT_S_DOES_NOT_EXIST_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                                COLUMN_TYPE, columnName, this.getName()));
            }
            if (columnTypeElement instanceof JsonArray) {
                throw new EDataStructureModelFormatException(String.format(
                        ELEMENT_S_MUST_BE_A_SINGLE_ELEMENT_NOT_AN_ARRAY_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                        COLUMN_TYPE, columnName, this.getName()));
            }
            String columnType = columnTypeElement.getAsString();

            // column's length
            JsonElement columnLengthElement = jsonObject.get(COLUMN_LENGTH);
            if (columnLengthElement == null) {
                throw new EDataStructureModelFormatException(
                        String.format(ELEMENT_S_DOES_NOT_EXIST_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                                COLUMN_LENGTH, columnName, this.getName()));
            }
            if (columnLengthElement instanceof JsonArray) {
                throw new EDataStructureModelFormatException(String.format(
                        ELEMENT_S_MUST_BE_A_SINGLE_ELEMENT_NOT_AN_ARRAY_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                        COLUMN_LENGTH, columnName, this.getName()));
            }
            String columnLength = columnLengthElement.getAsString();

            // column's notNull
            JsonElement columnNotNullElement = jsonObject.get(COLUMN_NOT_NULL);
            if (columnNotNullElement == null) {
                throw new EDataStructureModelFormatException(
                        String.format(ELEMENT_S_DOES_NOT_EXIST_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                                COLUMN_NOT_NULL, columnName, this.getName()));
            }
            if (columnNotNullElement instanceof JsonArray) {
                throw new EDataStructureModelFormatException(String.format(
                        ELEMENT_S_MUST_BE_A_SINGLE_ELEMENT_NOT_AN_ARRAY_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                        COLUMN_NOT_NULL, columnName, this.getName()));
            }
            boolean columnNotNull = columnNotNullElement.getAsBoolean();

            // column's primaryKey
            JsonElement columnPrimaryKeyElement = jsonObject.get(COLUMN_PRIMARY_KEY);
            if (columnPrimaryKeyElement == null) {
                throw new EDataStructureModelFormatException(
                        String.format(ELEMENT_S_DOES_NOT_EXIST_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                                COLUMN_PRIMARY_KEY, columnName, this.getName()));
            }
            if (columnPrimaryKeyElement instanceof JsonArray) {
                throw new EDataStructureModelFormatException(String.format(
                        ELEMENT_S_MUST_BE_A_SINGLE_ELEMENT_NOT_AN_ARRAY_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                        COLUMN_PRIMARY_KEY, columnName, this.getName()));
            }
            boolean columnPrimaryKey = columnPrimaryKeyElement.getAsBoolean();

            // column's defaultValue
            JsonElement columnDefaultValueElement = jsonObject.get(COLUMN_DEFAULT_VALUE);
            if (columnDefaultValueElement == null) {
                throw new EDataStructureModelFormatException(
                        String.format(ELEMENT_S_DOES_NOT_EXIST_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                                COLUMN_DEFAULT_VALUE, columnName, this.getName()));
            }
            if (columnDefaultValueElement instanceof JsonArray) {
                throw new EDataStructureModelFormatException(String.format(
                        ELEMENT_S_MUST_BE_A_SINGLE_ELEMENT_NOT_AN_ARRAY_IN_THIS_COLUMN_S_IN_THE_TABLE_MODEL_S,
                        COLUMN_DEFAULT_VALUE, columnName, this.getName()));
            }
            String columnDefaultValue = columnDefaultValueElement.getAsString();

            TableColumnModel tableColumnModel = new TableColumnModel(columnName, columnType, columnLength,
                    columnNotNull, columnPrimaryKey, columnDefaultValue);
            this.columns.add(tableColumnModel);
        }
    }
}

From source file:org.eclipse.eavp.viz.service.paraview.widgets.ParaViewCanvas.java

License:Open Source License

/**
 * Sends an update request to the specified client. This operation waits for
 * the response, after which it will construct an Image from the encoded
 * image string. If the returned image is stale, then {@link #stale} is set
 * to true.//from w  w w  .j av a  2 s .co m
 * <p>
 * <b>Note:</b> This operation is intended to be called from the refresh
 * thread in {@link #refreshRunnable}.
 * </p>
 * 
 * @param client
 *            The client from which to request a new image.
 * @param viewId
 *            The ID of the view to render on the client.
 * @param width
 *            The width of the Canvas when making the request.
 * @param height
 *            The height of the Canvas when making the request.
 * @return An Image from the client, or {@code null} if the render request
 *         could not be completed.
 */
private Image refreshClient(IParaViewWebClient client, int viewId, int width, int height) {

    // Set the default return value.
    Image image = null;

    if (client != null && width > 0 && height > 0) {

        // The request to draw will return an object containing an encoded
        // image string and a flag stating whether the image is stale.

        // Send a render request to the client and wait for the reply.
        JsonObject response = null;
        try {
            response = client.render(viewId, IMAGE_QUALITY, width, height).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        // If the response was received, try to read in the encoded image
        // and the stale flag.
        if (response != null) {

            // Read the base 64 image string from the response, then
            // construct a new Image from the encoded string.
            JsonElement element = response.get("image");
            if (element != null && element.isJsonPrimitive()) {
                try {
                    String base64Image = element.getAsString();

                    // TODO When we start using Java 8, replace the
                    // DatatypeConverter with the java.util.Base64
                    // class.
                    byte[] decode = DatatypeConverter.parseBase64Binary(base64Image);
                    // byte[] decode = Base64.getDecoder().decode(
                    // base64Image.getBytes());
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(decode);

                    // Load the input stream into a new Image.
                    ImageData[] data = new ImageLoader().load(inputStream);
                    if (data.length > 0) {
                        image = new Image(getDisplay(), data[0]);
                    }
                } catch (ClassCastException e) {
                    // Could not read the image.
                }
            }

            // If the image is stale, trigger another refresh operation.
            element = response.get("stale");
            if (element != null && element.isJsonPrimitive()) {
                try {
                    if (element.getAsBoolean()) {
                        refresh();
                    }
                } catch (ClassCastException e) {
                    // Could not read the stale variable.
                }
            }

        }
    }

    return image;
}

From source file:org.eclipse.scada.base.json.VariantJsonDeserializer.java

License:Open Source License

@Override
public Variant deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    }//  ww  w.j  a  v a 2 s .  co m

    if (json instanceof JsonPrimitive) {
        return decodeFromPrimitive(json);
    }

    if (json instanceof JsonObject) {
        final JsonObject jsonObj = (JsonObject) json;
        final JsonElement type = jsonObj.get(VariantJson.FIELD_TYPE);
        final JsonElement value = jsonObj.get(VariantJson.FIELD_VALUE);

        if (type == null || type.isJsonNull()) {
            if (value == null) {
                throw new JsonParseException(String.format("Variant encoded as object must have a field '%s'",
                        VariantJson.FIELD_VALUE));
            }
            return Variant.valueOf(value.getAsString());
        }

        if (!type.isJsonPrimitive()) {
            throw new JsonParseException(
                    String.format("Variant field '%s' must be a string containing the variant type (%s)",
                            VariantJson.FIELD_TYPE, StringHelper.join(VariantType.values(), ", ")));
        }

        final String typeStr = type.getAsString();

        if (typeStr.equals("NULL")) {
            return Variant.NULL;
        }

        if (value == null || value.isJsonNull()) {
            throw new JsonParseException(String.format(
                    "The type '%s' does not support a null value. Use variant type NULL instead.", typeStr));
        }

        if (value.isJsonObject() || value.isJsonArray()) {
            throw new JsonParseException(
                    "The variant value must be a JSON primitive matching the type. Arrays and objects are not supported");
        }

        switch (type.getAsString()) {
        case "BOOLEAN":
            return Variant.valueOf(value.getAsBoolean());
        case "STRING":
            return Variant.valueOf(value.getAsString());
        case "DOUBLE":
            return Variant.valueOf(value.getAsDouble());
        case "INT32":
            return Variant.valueOf(value.getAsInt());
        case "INT64":
            return Variant.valueOf(value.getAsLong());
        default:
            throw new JsonParseException(String.format("Type '%s' is unknown (known types: %s)",
                    StringHelper.join(VariantType.values(), ", ")));
        }
    }

    throw new JsonParseException("Unknown serialization of Variant type");
}