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.eclipse.tm4e.languageconfiguration.internal.LanguageConfiguration.java

License:Open Source License

private static Boolean getAsBoolean(JsonElement element, Boolean defaultValue) {
    if (element == null) {
        return defaultValue;
    }//from  w w w. j  a  v  a 2  s.  co m
    try {
        return element.getAsBoolean();
    } catch (Exception e) {
        return defaultValue;
    }
}

From source file:org.fenixedu.spaces.domain.Information.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T extends Object> Optional<T> getMetadata(String field) {
    Optional<JsonElement> spec = getClassification().getMetadataSpecJson(field);
    if (spec.isPresent()) {
        JsonObject jsObj = spec.get().getAsJsonObject();
        final String type = jsObj.get("type").getAsString();
        final JsonObject metadata = getMetadata().getAsJsonObject();

        JsonElement fieldValue = metadata.get(field);

        if (fieldValue == null || fieldValue.isJsonNull()) {
            return Optional.empty();
        }/* w ww  .  j  a  v a 2 s . com*/

        if (Boolean.class.getName().equalsIgnoreCase(type)) {
            return (Optional<T>) Optional.of(new Boolean(fieldValue.getAsBoolean()));
        }
        if (Integer.class.getName().equalsIgnoreCase(type)) {
            return (Optional<T>) Optional.of(new Integer(fieldValue.getAsInt()));
        }
        if (BigDecimal.class.getName().equalsIgnoreCase(type)) {
            return (Optional<T>) Optional.of(fieldValue.getAsBigDecimal());
        }

        return (Optional<T>) Optional.of(new String(fieldValue.getAsString()));
    }
    return Optional.empty();
}

From source file:org.ff4j.elastic.FeatureConverter.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*w  ww.  j a v a2  s  . c  o m*/
public Feature deserialize(JsonElement json, Type type, JsonDeserializationContext context) {

    String uid = json.getAsJsonObject().get("uid").getAsString();

    Feature feature = new Feature(uid);

    // Property "enable"
    JsonElement enable = json.getAsJsonObject().get("enable");
    if (enable != null) {
        feature.setEnable(enable.getAsBoolean());
    }

    // Description
    JsonElement description = json.getAsJsonObject().get("description");
    if (description != null) {
        feature.setDescription(description.getAsString());
    }

    // Group
    JsonElement group = json.getAsJsonObject().get("group");
    if (group != null) {
        feature.setGroup(group.getAsString());
    }

    // Permissions
    JsonElement permissions = json.getAsJsonObject().get("permissions");
    if (permissions != null) {
        Set<String> auths = gson.fromJson(permissions, new TypeToken<HashSet<String>>() {
        }.getType());
        feature.setPermissions(auths);
    }

    // Flipping strategy
    JsonElement flippingStrategy = json.getAsJsonObject().get("flippingStrategy");
    if (flippingStrategy != null && !flippingStrategy.isJsonNull()) {
        Map<String, ?> flippingStrategyParameters = gson.fromJson(flippingStrategy,
                new TypeToken<HashMap<String, ?>>() {
                }.getType());
        String flippingStrategyType = flippingStrategyParameters.get("type").toString();
        Map<String, String> initParams = (Map<String, String>) flippingStrategyParameters.get("initParams");
        // Adding flipping strategy
        feature.setFlippingStrategy(
                MappingUtil.instanceFlippingStrategy(uid, flippingStrategyType, initParams));
    }

    // Custom properties
    JsonElement customProperties = json.getAsJsonObject().get("customProperties");
    if (customProperties != null && !customProperties.isJsonNull()) {
        Map<String, LinkedTreeMap<String, Object>> map = new Gson().fromJson(customProperties,
                new TypeToken<com.google.gson.internal.LinkedTreeMap<String, LinkedTreeMap<String, Object>>>() {
                }.getType());
        for (Entry<String, LinkedTreeMap<String, Object>> element : map.entrySet()) {
            LinkedTreeMap<String, Object> propertyValues = element.getValue();
            // Getting values
            String pName = (String) propertyValues.get("name");
            String pType = (String) propertyValues.get("type");
            String pValue = (String) propertyValues.get("value");
            String desc = (String) propertyValues.get("description");
            Object fixedValues = propertyValues.get("fixedValues");
            Set pFixedValues = fixedValues != null ? new HashSet((Collection) fixedValues) : null;
            // Creating property with it
            Property<?> property = PropertyFactory.createProperty(pName, pType, pValue, desc, pFixedValues);
            feature.addProperty(property);
        }
    }

    return feature;
}

From source file:org.graylog2.indexer.gson.GsonUtils.java

License:Open Source License

@Nullable
public static Boolean asBoolean(JsonElement jsonElement) {
    return jsonElement instanceof JsonPrimitive && ((JsonPrimitive) jsonElement).isBoolean()
            ? jsonElement.getAsBoolean()
            : null;/*w ww  .ja  va2 s.c  o m*/
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Object getValue(Method.Data data, JsonObject json) {
    if (data == null)
        return null;

    String id = data.getId();/*from w  ww .  jav a2 s  . c om*/
    Class<?> clazz = data.getType();
    boolean isNull = data.getNull();

    JsonElement value = json == null ? new JsonNull() : json.get(id);
    if (!isNull && value.isJsonNull())
        throw new EventBusException(
                "Method.Data[" + data + "], the id[" + id + "] has null value to give " + data.getType());

    if (!value.isJsonNull()) {
        if (clazz == Boolean.class) {
            return value.getAsBoolean();
        } else if (clazz == Integer.class) {
            return value.getAsInt();
        } else if (clazz == Long.class) {
            return value.getAsLong();
        } else if (clazz == Float.class) {
            return value.getAsFloat();
        } else if (clazz == Double.class) {
            return value.getAsDouble();
        } else if (clazz == String.class) {
            return value.getAsString();
        } else if (clazz == Byte.class) {
            return value.getAsByte();
        } else if (clazz == char.class) {
            return value.getAsCharacter();
        } else {
            return new Gson().fromJson(value, clazz);
        }
    }
    return null;
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Bundle getBundleByPage(Page page, JsonObject json) throws Exception {
    if (page != null && json != null && page.getBundleList().size() > 0) {
        Bundle bundle = new Bundle();
        for (Page.Bundle item : page.getBundleList()) {
            String id = item.getId();
            String key = item.getKey();
            Class<? extends Serializable> clazz = item.getType();
            boolean isNull = item.getNull();

            JsonElement value = json.get(id);
            if (!isNull && value.isJsonNull())
                throw new EventBusException(
                        "Page.Bundle[" + item + "], the id[" + id + "] has null value to give " + key);

            if (!value.isJsonNull()) {
                if (clazz == Boolean.class) {
                    bundle.putBoolean(key, value.getAsBoolean());
                } else if (clazz == Integer.class) {
                    bundle.putInt(key, value.getAsInt());
                } else if (clazz == Long.class) {
                    bundle.putLong(key, value.getAsLong());
                } else if (clazz == Float.class) {
                    bundle.putFloat(key, value.getAsFloat());
                } else if (clazz == Double.class) {
                    bundle.putDouble(key, value.getAsDouble());
                } else if (clazz == String.class) {
                    bundle.putString(key, value.getAsString());
                } else if (clazz == Byte.class) {
                    bundle.putByte(key, value.getAsByte());
                } else if (clazz == char.class) {
                    bundle.putChar(key, value.getAsCharacter());
                } else {
                    bundle.putSerializable(key, new Gson().fromJson(value, clazz));
                }/*  www.  j  a  va2  s  . co m*/
            }
        }
        return bundle;
    }
    return null;
}

From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java

License:LGPL

public void addDocumentField(Document tmp, JsonElement jsonValue) {
    if (jsonValue == null || jsonValue.isJsonNull()) {
        return;/*  w ww  .  java2  s . c om*/
    }
    switch (fieldType) {
    case INTEGER:
        tmp.add(new IntField(absoluteName, jsonValue.getAsInt(), Store.NO));
        break;
    case LONG:
        tmp.add(new LongField(absoluteName, jsonValue.getAsLong(), Store.NO));
        break;
    case FLOAT:
        tmp.add(new FloatField(absoluteName, jsonValue.getAsFloat(), Store.NO));
        break;
    case DOUBLE:
        tmp.add(new DoubleField(absoluteName, jsonValue.getAsDouble(), Store.NO));
        break;
    case UNKNOWN_NUMERIC:
        throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName);
    case BOOLEAN:
        tmp.add(new StringField(absoluteName, String.valueOf(jsonValue.getAsBoolean()), Store.NO));
        break;
    default:
        tmp.add(new StringField(absoluteName, jsonValue.getAsString(), Store.NO));
        break;
    }
}

From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java

License:LGPL

@Override
public Object convertHit(JsonObject hit, ConversionContext conversionContext) {
    JsonElement jsonValue = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName);
    if (jsonValue == null || jsonValue.isJsonNull()) {
        return null;
    }/*from ww w  .  java  2  s . c  o m*/
    switch (fieldType) {
    case INTEGER:
        return jsonValue.getAsInt();
    case LONG:
        return jsonValue.getAsLong();
    case FLOAT:
        return jsonValue.getAsFloat();
    case DOUBLE:
        return jsonValue.getAsDouble();
    case UNKNOWN_NUMERIC:
        throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName);
    case BOOLEAN:
        return jsonValue.getAsBoolean();
    default:
        return jsonValue.getAsString();
    }
}

From source file:org.kurento.jsonrpc.JsonRpcAndJavaMethodManager.java

License:Apache License

private Object getAsJavaType(Class<?> type, JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return null;
    } else if (type == String.class) {
        return jsonElement.getClass().equals(JsonObject.class) ? jsonElement.toString()
                : jsonElement.getAsString();
    } else if (type == boolean.class) {
        return jsonElement.getAsBoolean();
    } else if (type.isEnum()) {
        return gson.fromJson(jsonElement, type);
    } else if (type == int.class) {
        return jsonElement.getAsInt();
    } else {//  w  ww  .  ja  v  a 2  s  .  c o  m
        return null;
    }
}

From source file:org.lanternpowered.server.text.gson.JsonTextBaseSerializer.java

License:MIT License

public static void deserialize(JsonObject json, Text.Builder builder, JsonDeserializationContext context,
        @Nullable JsonArray children) throws JsonParseException {
    JsonElement element;
    if ((element = json.get(COLOR)) != null) {
        Sponge.getRegistry().getType(TextColor.class, element.getAsString()).ifPresent(builder::color);
    }/*from ww w . j a va2 s.  c o  m*/
    TextStyle style = builder.getStyle();
    if ((element = json.get(BOLD)) != null) {
        style = style.bold(element.getAsBoolean());
    }
    if ((element = json.get(ITALIC)) != null) {
        style = style.italic(element.getAsBoolean());
    }
    if ((element = json.get(UNDERLINE)) != null) {
        style = style.underline(element.getAsBoolean());
    }
    if ((element = json.get(STRIKETHROUGH)) != null) {
        style = style.strikethrough(element.getAsBoolean());
    }
    if ((element = json.get(OBFUSCATED)) != null) {
        style = style.obfuscated(element.getAsBoolean());
    }
    builder.style(style);
    if (children != null) {
        builder.append((Text[]) context.deserialize(children, Text[].class));
    }
    if ((element = json.get(CLICK_EVENT)) != null) {
        final JsonObject jsonClickEvent = element.getAsJsonObject();
        if (jsonClickEvent != null) {
            final JsonPrimitive jsonEventAction = jsonClickEvent.getAsJsonPrimitive(EVENT_ACTION);
            final JsonPrimitive jsonEventValue = jsonClickEvent.getAsJsonPrimitive(EVENT_VALUE);
            if (jsonEventAction != null && jsonEventValue != null) {
                final String action = jsonEventAction.getAsString();
                final String value = jsonEventValue.getAsString();

                final ClickAction<?> clickAction = LanternTextHelper.parseClickAction(action, value);
                if (clickAction != null) {
                    builder.onClick(clickAction);
                }
            }
        }
    }
    if ((element = json.get(HOVER_EVENT)) != null) {
        final JsonObject jsonHoverEvent = element.getAsJsonObject();
        if (jsonHoverEvent != null) {
            final JsonPrimitive jsonEventAction = jsonHoverEvent.getAsJsonPrimitive(EVENT_ACTION);
            final JsonPrimitive jsonEventValue = jsonHoverEvent.getAsJsonPrimitive(EVENT_VALUE);
            if (jsonEventAction != null && jsonEventValue != null) {
                final String action = jsonEventAction.getAsString();
                final String value = jsonEventValue.getAsString();

                final HoverAction<?> hoverAction = LanternTextHelper.parseHoverAction(action, value);
                if (hoverAction != null) {
                    builder.onHover(hoverAction);
                }
            }
        }
    }
    if ((element = json.get(INSERTION)) != null) {
        builder.onShiftClick(TextActions.insertText(element.getAsString()));
    }
}