Example usage for com.google.gson JsonElement getAsInt

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

Introduction

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

Prototype

public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer value.

Usage

From source file:org.eclipse.smarthome.binding.tradfri.internal.model.TradfriLightData.java

License:Open Source License

public PercentType getColorTemperature() {
    JsonElement colorX = attributes.get(COLOR_X);
    JsonElement colorY = attributes.get(COLOR_Y);
    if (colorX != null && colorY != null) {
        TradfriColor color = new TradfriColor(colorX.getAsInt(), colorY.getAsInt(), null);
        return color.getColorTemperature();
    } else {//w  w  w  .  j a va 2 s  . c  om
        return null;
    }
}

From source file:org.eclipse.smarthome.binding.tradfri.internal.model.TradfriLightData.java

License:Open Source License

public HSBType getColor() {
    // XY color coordinates plus brightness is needed for color calculation
    JsonElement colorX = attributes.get(COLOR_X);
    JsonElement colorY = attributes.get(COLOR_Y);
    JsonElement dimmer = attributes.get(DIMMER);
    if (colorX != null && colorY != null && dimmer != null) {
        int x = colorX.getAsInt();
        int y = colorY.getAsInt();
        int brightness = dimmer.getAsInt();
        TradfriColor color = new TradfriColor(x, y, brightness);
        return color.getHSB();
    }/*  w  ww  . j  a  v a2  s .co m*/
    return null;
}

From source file:org.eclipse.smarthome.binding.tradfri.internal.model.TradfriPlugData.java

License:Open Source License

public int getTransitionTime() {
    JsonElement transitionTime = attributes.get(TRANSITION_TIME);
    if (transitionTime != null) {
        return transitionTime.getAsInt();
    } else {//w  ww.  ja  v a 2  s  . c  om
        return 0;
    }
}

From source file:org.eclipse.smarthome.binding.tradfri.internal.model.TradfriPlugData.java

License:Open Source License

public boolean getOnOffState() {
    JsonElement onOff = attributes.get(ONOFF);
    if (onOff != null) {
        return onOff.getAsInt() == 1;
    } else {//from   www. j  a  va2 s  .  c o m
        return false;
    }
}

From source file:org.eclipse.tm4e.languageconfiguration.internal.LanguageConfiguration.java

License:Open Source License

private static Integer getAsInt(JsonElement element) {
    if (element == null) {
        return null;
    }/*www  . j a v a2  s.c  om*/
    try {
        return element.getAsInt();
    } catch (Exception e) {
        return null;
    }
}

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();
        }/*from w  w  w. j av  a2s.c  o  m*/

        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.fenixedu.spaces.ui.services.OccupationService.java

License:Open Source License

private OccupationConfig parseConfig(String config, List<Interval> intervals) {
    JsonObject json = jsonParser.parse(config).getAsJsonObject();

    DateTime start = DateTime.parse(json.get("start").getAsString(), datetimeFormatter);
    DateTime end = DateTime.parse(json.get("end").getAsString(), datetimeFormatter);
    String jsonFrequency = json.get("frequency").getAsString();
    Boolean allDay = json.get("isAllDay").getAsBoolean();
    Integer repeatsEvery = null;//  ww w .j a  va 2  s  .  c  o  m
    List<Integer> weekdays = null;
    MonthlyType monthlyType = null;
    Frequency frequency = null;

    switch (jsonFrequency) {
    case "n":
        frequency = Frequency.NEVER;
        break;
    case "d":
        frequency = Frequency.DAILY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        break;
    case "w":
        frequency = Frequency.WEEKLY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        weekdays = new ArrayList<>();
        for (JsonElement day : json.get("weekdays").getAsJsonArray()) {
            weekdays.add(day.getAsInt());
        }
        break;
    case "m":
        frequency = Frequency.MONTHLY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        monthlyType = json.get("monthlyType").getAsString().equals("dayofmonth") ? MonthlyType.DAY_OF_MONTH
                : MonthlyType.DAY_OF_WEEK;
        break;
    case "y":
        frequency = Frequency.YEARLY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        break;
    }

    return new ExplicitConfigWithSettings(start, end, allDay, repeatsEvery, frequency, weekdays, monthlyType,
            intervals);
}

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

License:Open Source License

@Nullable
public static Integer asInteger(JsonElement jsonElement) {
    return jsonElement instanceof JsonPrimitive && ((JsonPrimitive) jsonElement).isNumber()
            ? jsonElement.getAsInt()
            : null;/* www. j a va 2  s.com*/
}

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();//  w  w w .  j a  v  a 2  s.c  o m
    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));
                }/*from   w w  w  . j a va2s  .  co m*/
            }
        }
        return bundle;
    }
    return null;
}