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:com.simiacryptus.mindseye.layers.java.BinaryNoiseLayer.java

License:Apache License

/**
 * Instantiates a new Binary noise key.//from  w  w  w .java  2s . co m
 *
 * @param json the json
 */
protected BinaryNoiseLayer(@Nonnull final JsonObject json) {
    super(json);
    value = json.get("value").getAsDouble();
    JsonElement enabled = json.get("enabled");
    this.enabled = enabled == null || enabled.getAsBoolean();
}

From source file:com.sixt.service.framework.protobuf.ProtobufUtil.java

License:Apache License

private static Object parseField(Descriptors.FieldDescriptor field, JsonElement value,
        Message.Builder enclosingBuilder) throws Exception {
    switch (field.getType()) {
    case DOUBLE:/* ww  w  . j ava2  s  . co  m*/
        if (!value.isJsonPrimitive()) {
            // fail;
        }
        return value.getAsDouble();
    case FLOAT:
        if (!value.isJsonPrimitive()) {
            // fail;
        }
        return value.getAsFloat();
    case INT64:
    case UINT64:
    case FIXED64:
    case SINT64:
    case SFIXED64:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsLong();
    case INT32:
    case UINT32:
    case FIXED32:
    case SINT32:
    case SFIXED32:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsInt();
    case BOOL:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsBoolean();
    case STRING:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsString();
    case GROUP:
    case MESSAGE:
        if (!value.isJsonObject()) {
            // fail
        }
        return fromJson(enclosingBuilder.newBuilderForField(field), value.getAsJsonObject());
    case BYTES:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return ByteString.copyFrom(BaseEncoding.base64().decode(value.getAsString()));
    case ENUM:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        String protoEnumValue = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, value.getAsString());
        return field.getEnumType().findValueByName(protoEnumValue);
    }
    return null;
}

From source file:com.sixt.service.framework.rpc.RpcCallException.java

License:Apache License

public static RpcCallException fromJson(String json) {
    try {//from   w ww. j  a va2  s  . c om
        JsonParser parser = new JsonParser();
        JsonElement rawObject = parser.parse(json);
        if (rawObject instanceof JsonObject) {
            JsonObject object = (JsonObject) rawObject;
            Category category = Category.fromStatus(object.get(CATEGORY).getAsInt());
            String message = object.get(MESSAGE).getAsString();
            RpcCallException retval = new RpcCallException(category, message);
            JsonElement element = object.get(SOURCE);
            if (element != null && !(element instanceof JsonNull)) {
                retval.withSource(element.getAsString());
            }
            element = object.get(CODE);
            if (element != null && !(element instanceof JsonNull)) {
                retval.withErrorCode(element.getAsString());
            }
            element = object.get(DATA);
            if (element != null && !(element instanceof JsonNull)) {
                retval.withData(element.getAsString());
            }
            element = object.get(RETRIABLE);
            if (element != null && !(element instanceof JsonNull)) {
                retval.withRetriable(element.getAsBoolean());
            }
            return retval;
        } else if (rawObject instanceof JsonPrimitive) {
            logger.warn("Expected an RpcCallException json object, but received: {}", rawObject.toString());
        }
    } catch (Exception ex) {
        logger.warn("Caught exception parsing RpcCallException: " + json, ex);
    }
    return null;
}

From source file:com.skysql.manager.api.Monitors.java

License:Open Source License

private void parseMonitors(JsonArray array,
        LinkedHashMap<String, LinkedHashMap<String, MonitorRecord>> monitorsMap) {

    for (int i = 0; i < array.size(); i++) {
        JsonObject jsonObject = array.get(i).getAsJsonObject();
        JsonElement element;
        String systemType = (element = jsonObject.get("systemtype")).isJsonNull() ? null
                : element.getAsString();
        String id = (element = jsonObject.get("monitor")).isJsonNull() ? null : element.getAsString();
        String name = (element = jsonObject.get("name")).isJsonNull() ? null : element.getAsString();
        String description = (element = jsonObject.get("description")).isJsonNull() ? null
                : element.getAsString();
        String unit = (element = jsonObject.get("unit")).isJsonNull() ? null : element.getAsString();
        String monitorType = (element = jsonObject.get("monitortype")).isJsonNull() ? null
                : element.getAsString();
        boolean delta = (element = jsonObject.get("delta")).isJsonNull() ? false : element.getAsBoolean();
        boolean average = (element = jsonObject.get("systemaverage")).isJsonNull() ? false
                : element.getAsBoolean();
        String chartType = (element = jsonObject.get("charttype")).isJsonNull() ? null : element.getAsString();
        String intervalString = (element = jsonObject.get("interval")).isJsonNull() ? null
                : element.getAsString();
        int interval = (intervalString != null && !intervalString.isEmpty()) ? Integer.valueOf(intervalString)
                : 0;/*w ww.  j  a  v  a2 s  . co m*/
        String sql = (element = jsonObject.get("sql")).isJsonNull() ? null : element.getAsString();
        for (PermittedMonitorType permitted : PermittedMonitorType.values()) {
            if (permitted.name().equals(monitorType)) {
                MonitorRecord monitorRecord = new MonitorRecord(systemType, id, name, description, unit,
                        monitorType, delta, average, chartType, interval, sql);
                monitorsMap.get(systemType).put(id, monitorRecord);
                break;
            }
        }

    }

}

From source file:com.sldeditor.exportdata.esri.renderer.UniqueValueRenderer.java

License:Open Source License

@Override
public StyledLayerDescriptor convert(JsonObject json, String layerName, double minScale, double maxScale,
        int transparency) {
    StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();

    NamedLayer namedLayer = styleFactory.createNamedLayer();

    boolean useDefaultSymbol = false;
    JsonElement useDefaultSymbolElement = json.get(UniqueValueRendererKeys.USE_DEFAULTSYMBOL);
    if (useDefaultSymbolElement != null) {
        useDefaultSymbol = useDefaultSymbolElement.getAsBoolean();
    }//from ww  w .jav a2s  .c om

    JsonElement jsonElement = json.get(CommonRendererKeys.LABEL);
    if (jsonElement != null) {
        namedLayer.setName(jsonElement.getAsString());
    }
    sld.addStyledLayer(namedLayer);

    Style style = styleFactory.createStyle();
    namedLayer.addStyle(style);

    List<FeatureTypeStyle> ftsList = style.featureTypeStyles();

    // style.setAbstract(json.get(IntermediateFileKeys.DESCRIPTION).getAsString());

    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();

    ftsList.add(fts);

    JsonElement element = json.get(UniqueValueRendererKeys.VALUES);
    if (element != null) {
        JsonArray valueList = element.getAsJsonArray();

        for (int index = 0; index < valueList.size(); index++) {
            JsonElement valueElement = valueList.get(index);
            if (valueElement != null) {
                Rule rule = styleFactory.createRule();
                JsonObject valueObj = valueElement.getAsJsonObject();

                String value = getString(valueObj, UniqueValueRendererKeys.VALUES_VALUE);

                String label = getString(valueObj, UniqueValueRendererKeys.VALUES_LABEL);
                rule.setName(label);

                createFilter(rule, json.get(UniqueValueRendererKeys.FIELDS),
                        json.get(UniqueValueRendererKeys.FIELD_DELIMETER), value);

                // Heading /description
                String heading = getString(valueObj, UniqueValueRendererKeys.VALUES_HEADING);
                @SuppressWarnings("unused")
                String description = getString(valueObj, UniqueValueRendererKeys.VALUES_DESCRIPTION);

                if ((heading != null) && !heading.isEmpty() || (label != null) && !label.isEmpty()) {
                    if (label == null) {
                        label = "";
                    }
                    InternationalString titleString = Text.text(label);

                    if (heading == null) {
                        heading = "";
                    }

                    InternationalString descriptionString = Text.text(heading);

                    Description descriptionObj = styleFactory.description(titleString, descriptionString);

                    rule.setDescription(descriptionObj);
                }

                if (minScale > 0.0) {
                    rule.setMinScaleDenominator(minScale);
                }

                if (maxScale > 0.0) {
                    rule.setMaxScaleDenominator(maxScale);
                }

                JsonElement jsonSymbolElement = valueObj.get(UniqueValueRendererKeys.VALUES_SYMBOL);
                SymbolManager.getInstance().convertSymbols(rule, layerName, transparency, jsonSymbolElement);

                if (useDefaultSymbol && value == null) {
                    rule.setIsElseFilter(true);
                }
                fts.rules().add(rule);
            }
        }
    }
    return sld;
}

From source file:com.sldeditor.exportdata.esri.symbols.BaseSymbol.java

License:Open Source License

/**
 * Gets the boolean value./*  w w w  .j a  v a2  s  . c om*/
 *
 * @param obj the obj
 * @param field the field
 * @return the boolean value
 */
protected static boolean getBoolean(JsonObject obj, String field) {
    boolean value = false;

    if (obj != null) {
        JsonElement element = obj.get(field);

        if (element != null) {
            value = element.getAsBoolean();
        }
    }
    return value;
}

From source file:com.stratio.decision.serializer.gson.impl.ColumnNameTypeValueDeserializer.java

License:Apache License

@Override
public ColumnNameTypeValue deserialize(JsonElement element, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    final JsonObject object = element.getAsJsonObject();
    String name = null;//from www  .ja  v a 2s.  co  m
    ColumnType columnType = null;
    Object value = null;
    if (object != null && object.has(COLUMN_FIELD) && object.has(TYPE_FIELD)) {
        name = object.get(COLUMN_FIELD).getAsString();
        columnType = ColumnType.valueOf(object.get(TYPE_FIELD).getAsString());

        if (object.has(VALUE_FIELD)) {
            JsonElement jsonValue = object.get(VALUE_FIELD);
            switch (columnType) {
            case BOOLEAN:
                value = jsonValue.getAsBoolean();
                break;
            case DOUBLE:
                value = jsonValue.getAsDouble();
                break;
            case FLOAT:
                value = jsonValue.getAsFloat();
                break;
            case INTEGER:
                value = jsonValue.getAsInt();
                break;
            case LONG:
                value = jsonValue.getAsLong();
                break;
            case STRING:
                value = jsonValue.getAsString();
                break;
            default:
                break;
            }
        } else {
            log.warn("Column with name {} has no value", name);
        }

        if (log.isDebugEnabled()) {
            log.debug("Values obtained into ColumnNameTypeValue deserialization: "
                    + "NAME: {}, VALUE: {}, COLUMNTYPE: {}", name, value, columnType);
        }
    } else {
        log.warn("Error deserializing ColumnNameTypeValue from json. JsonObject is not complete: {}", element);
    }

    return new ColumnNameTypeValue(name, columnType, value);
}

From source file:com.strato.hidrive.api.JSONDataReader.java

License:Apache License

@Override
public boolean readBooleanWithName(String name) {
    boolean defaultValue = false;
    return get(name, defaultValue, new ElementValue<Boolean>() {
        @Override//  w  ww  .j  a v a2s  . co m
        public Boolean value(JsonElement jsonElement) {
            return jsonElement.getAsBoolean();
        }
    });
}

From source file:com.wallellen.wechat.common.util.json.GsonHelper.java

License:Open Source License

public static Boolean getAsBoolean(JsonElement element) {
    return isNull(element) ? null : element.getAsBoolean();
}

From source file:com.yandex.money.api.typeadapters.model.showcase.uicontrol.ControlTypeAdapter.java

License:Open Source License

@Override
protected void deserialize(JsonObject src, U builder, JsonDeserializationContext context) {
    builder.setAlert(getString(src, MEMBER_ALERT));
    builder.setHint(getString(src, MEMBER_HINT));
    builder.setLabel(getString(src, MEMBER_LABEL));

    JsonElement readOnly = src.get(MEMBER_READONLY);
    if (readOnly != null) {
        builder.setReadonly(readOnly.getAsBoolean());
    }/* ww  w. j a v  a  2  s .  c o m*/

    JsonElement required = src.get(MEMBER_REQUIRED);
    if (required != null) {
        builder.setRequired(required.getAsBoolean());
    }
}