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.lanternpowered.server.script.function.value.json.ConstantIntValueProviderJsonSerializer.java

License:MIT License

@Override
public IntValueProvider.Constant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return IntValueProvider.constant(json.getAsInt());
}

From source file:org.mitre.util.JsonUtils.java

License:Apache License

/**
 * Gets the value of the given member (expressed as integer seconds since epoch) as a Date
 *//*from  w  ww.  j av  a  2s  . c  o  m*/
public static Date getAsDate(JsonObject o, String member) {
    if (o.has(member)) {
        JsonElement e = o.get(member);
        if (e != null && e.isJsonPrimitive()) {
            return new Date(e.getAsInt() * 1000L);
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.openhab.binding.miio.handler.MiIoBasicHandler.java

License:Open Source License

void updateProperties(MiIoSendCommand response) {
    JsonArray res = response.getResult().getAsJsonArray();
    JsonArray para = parser.parse(response.getCommandString()).getAsJsonObject().get("params").getAsJsonArray();
    if (res.size() != para.size()) {
        logger.debug("Unexpected size different. Request size {},  response size {}. (Req: {}, Resp:{})",
                para.size(), res.size(), para.toString(), res.toString());
    }/*w ww. j  a  v a 2s.  c  o  m*/
    for (int i = 0; i < para.size(); i++) {
        JsonElement val = res.get(i);
        if (val.isJsonNull()) {
            logger.debug("Property '{}' returned null (is it supported?).", para.get(i).getAsString());
            continue;
        }
        MiIoBasicChannel basicChannel = getChannel(para.get(i).getAsString());
        if (basicChannel != null) {
            if (basicChannel.getTransfortmation() != null) {
                JsonElement transformed = Conversions.execute(basicChannel.getTransfortmation(), val);
                logger.debug("Transformed with '{}': {} {} -> {} ", basicChannel.getTransfortmation(),
                        basicChannel.getFriendlyName(), val, transformed);
                val = transformed;
            }
            try {
                if (basicChannel.getType().equals("Number")) {
                    updateState(basicChannel.getChannel(), new DecimalType(val.getAsBigDecimal()));
                }
                if (basicChannel.getType().equals("String")) {
                    updateState(basicChannel.getChannel(), new StringType(val.getAsString()));
                }
                if (basicChannel.getType().equals("Switch")) {
                    updateState(basicChannel.getChannel(),
                            val.getAsString().toLowerCase().equals("on")
                                    || val.getAsString().toLowerCase().equals("true") ? OnOffType.ON
                                            : OnOffType.OFF);
                }
                if (basicChannel.getType().equals("Color")) {
                    Color rgb = new Color(val.getAsInt());
                    HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
                    updateState(basicChannel.getChannel(), hsb);
                }
            } catch (Exception e) {
                logger.debug("Error updating {} property {} with '{}' : {}", getThing().getUID().getAsString(),
                        basicChannel.getChannel(), val.getAsString(), e.getMessage());
                logger.trace("Property update error detail:", e);
            }
        } else {
            logger.debug("Channel not found for {}", para.get(i).getAsString());
        }
    }
}

From source file:org.openhab.binding.miio.internal.basic.Conversions.java

License:Open Source License

public static JsonElement secondsToHours(JsonElement seconds) {
    long hours = TimeUnit.SECONDS.toHours(seconds.getAsInt());
    return new JsonPrimitive(hours);
}

From source file:org.openhab.binding.miio.internal.basic.Conversions.java

License:Open Source License

public static JsonElement yeelightSceneConversion(JsonElement intValue) {
    switch (intValue.getAsInt()) {
    case 1://from w  w w.  jav a2 s.  c  o m
        return new JsonPrimitive("color");
    case 2:
        return new JsonPrimitive("hsv");
    case 3:
        return new JsonPrimitive("ct");
    case 4:
        return new JsonPrimitive("nightlight");
    case 5: // don't know the number for colorflow...
        return new JsonPrimitive("cf");
    case 6: // don't know the number for auto_delay_off, or if it is even in the properties visible...
        return new JsonPrimitive("auto_delay_off");
    default:
        return new JsonPrimitive("unknown");
    }
}

From source file:org.openhab.binding.robonect.internal.model.MowerModeDeserializer.java

License:Open Source License

@Override
public MowerMode deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    int mode = jsonElement.getAsInt();
    return MowerMode.fromMode(mode);
}

From source file:org.openhab.binding.robonect.internal.model.MowerStatusDeserializer.java

License:Open Source License

@Override
public MowerStatus deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    int code = jsonElement.getAsInt();
    return MowerStatus.fromCode(code);
}

From source file:org.openhab.binding.robonect.internal.model.TimerModeDeserializer.java

License:Open Source License

@Override
public Timer.TimerMode deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    int code = jsonElement.getAsInt();
    return Timer.TimerMode.fromCode(code);
}

From source file:org.openhab.binding.russound.internal.rio.models.RioBankSerializer.java

License:Open Source License

/**
 * Overridden to simply read the id/name elements and create a {@link RioBank}
 *
 * @param elm the {@link JsonElement} to read from
 * @param type the type//www.j  a  v a  2s  . com
 * @param context the serialization context
 */
@Override
public RioBank deserialize(JsonElement elm, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jo = (JsonObject) elm;

    final JsonElement id = jo.get("id");
    final JsonElement name = jo.get("name");
    return new RioBank((id == null ? -1 : id.getAsInt()), (name == null ? null : name.getAsString()));
}

From source file:org.openhab.binding.russound.internal.rio.models.RioFavoriteSerializer.java

License:Open Source License

/**
 * Overridden to simply read the id/valid/name elements and create a {@link RioFavorite}
 *
 * @param elm the {@link JsonElement} to read from
 * @param type the type//from  w  ww  . j  a v a 2s.  c o m
 * @param context the serialization context
 */
@Override
public RioFavorite deserialize(JsonElement elm, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jo = (JsonObject) elm;

    final JsonElement id = jo.get("id");
    final JsonElement valid = jo.get("valid");
    final JsonElement name = jo.get("name");

    return new RioFavorite((id == null ? -1 : id.getAsInt()), (valid == null ? false : valid.getAsBoolean()),
            (name == null ? null : name.getAsString()));
}