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:Change.java

License:Open Source License

@Override
public JsonElement onStateSet(String path, JsonElement value) throws JsonRpcException {
    try {//www  .ja  v a 2 s.  com
        int newValue = value.getAsInt();
        if (newValue % 10 != 0) {
            newValue = ((newValue + 5) / 10) * 10;
            return new JsonPrimitive(newValue);
        } else {
            return null;
        }
    } catch (ClassCastException | IllegalStateException e) {
        throw new JsonRpcException(JsonRpcException.INVALID_PARAMS, e.getMessage());
    }
}

From source file:AddState.java

License:Open Source License

@Override
public JsonElement onStateSet(String path, JsonElement value) throws JsonRpcException {
    try {//from   www . j  av  a2 s .  c o m
        int newValue = value.getAsInt();
        if (newValue % 10 != 0) {
            newValue = ((newValue + 5) / 10) * 10;
            Logger.getLogger(AddState.class.getName()).log(Level.INFO, "setting state to: {0}",
                    new Object[] { newValue });
            return new JsonPrimitive(newValue);
        } else {
            return null;
        }
    } catch (ClassCastException | IllegalStateException e) {
        throw new JsonRpcException(JsonRpcException.INVALID_PARAMS, e.getMessage());
    }
}

From source file:AddMethod.java

License:Open Source License

@Override
public JsonElement onMethodCalled(String path, JsonElement value) throws JsonRpcException {
    try {//from   w ww.ja  v a 2  s. com
        int newValue = value.getAsInt();
        Logger.getLogger(AddMethod.class.getName()).log(Level.INFO, "Method called with: {0}", newValue);
        newValue++;
        return new JsonPrimitive(newValue);
    } catch (ClassCastException | IllegalStateException e) {
        throw new JsonRpcException(JsonRpcException.INVALID_PARAMS, e.getMessage());
    }
}

From source file:augsburg.se.alltagsguide.utilities.Helper.java

License:Open Source License

public static int getIntOrDefault(JsonElement elem, int defaultValue) {
    try {// w ww  .ja va  2  s  .  c o m
        return elem.getAsInt();
    } catch (Exception e) {
        Ln.d(e);
        return defaultValue;
    }
}

From source file:blockplus.transport.BlockplusGame.java

License:Open Source License

public IGame<Context> play(final IMoveSubmit moveSubmitInterface) {
    final Context context = this.getContext();
    final SortedSet<IPosition> positions = Sets.newTreeSet();
    for (final JsonElement element : moveSubmitInterface.getPositions()) {
        final int id = element.getAsInt();
        final IPosition position = Position(id / 20, id % 20); // TODO !!!
        positions.add(position);// w w  w  .  j ava 2 s. c  o m
    }
    final Move move = new Move(context.side(), positions);
    return new BlockplusGame(this.getOrdinal(), this.getClients(), context.apply(move).forward(),
            this.isPaused);
}

From source file:brooklyn.event.feed.http.JsonFunctions.java

License:Apache License

@SuppressWarnings("unchecked")
public static <T> Function<JsonElement, T> cast(final Class<T> expected) {
    return new Function<JsonElement, T>() {
        @Override/*from  www  .  j a  v a2s . c  om*/
        public T apply(JsonElement input) {
            if (input == null) {
                return (T) null;
            } else if (input.isJsonNull()) {
                return (T) null;
            } else if (expected == boolean.class || expected == Boolean.class) {
                return (T) (Boolean) input.getAsBoolean();
            } else if (expected == char.class || expected == Character.class) {
                return (T) (Character) input.getAsCharacter();
            } else if (expected == byte.class || expected == Byte.class) {
                return (T) (Byte) input.getAsByte();
            } else if (expected == short.class || expected == Short.class) {
                return (T) (Short) input.getAsShort();
            } else if (expected == int.class || expected == Integer.class) {
                return (T) (Integer) input.getAsInt();
            } else if (expected == long.class || expected == Long.class) {
                return (T) (Long) input.getAsLong();
            } else if (expected == float.class || expected == Float.class) {
                return (T) (Float) input.getAsFloat();
            } else if (expected == double.class || expected == Double.class) {
                return (T) (Double) input.getAsDouble();
            } else if (expected == BigDecimal.class) {
                return (T) input.getAsBigDecimal();
            } else if (expected == BigInteger.class) {
                return (T) input.getAsBigInteger();
            } else if (Number.class.isAssignableFrom(expected)) {
                // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above
                return (T) input.getAsNumber();
            } else if (expected == String.class) {
                return (T) input.getAsString();
            } else if (expected.isArray()) {
                JsonArray array = input.getAsJsonArray();
                Class<?> componentType = expected.getComponentType();
                if (JsonElement.class.isAssignableFrom(componentType)) {
                    JsonElement[] result = new JsonElement[array.size()];
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = array.get(i);
                    }
                    return (T) result;
                } else {
                    Object[] result = (Object[]) Array.newInstance(componentType, array.size());
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = cast(componentType).apply(array.get(i));
                    }
                    return (T) result;
                }
            } else {
                throw new IllegalArgumentException("Cannot cast json element to type " + expected);
            }
        }
    };
}

From source file:cc.kave.commons.utils.json.EnumDeSerializer.java

License:Apache License

public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    int index = json.getAsInt();
    if (index >= 0 && index < values.length) {
        return values[index];
    }//from   w  w  w .  ja  va  2  s  .  c o m
    return values[0];
}

From source file:ch.cern.db.flume.sink.kite.parser.JSONtoAvroParser.java

License:GNU General Public License

private Object getElementAsType(Schema schema, JsonElement element) {
    if (element == null || element.isJsonNull())
        return null;

    switch (schema.getType()) {
    case BOOLEAN:
        return element.getAsBoolean();
    case DOUBLE:/*from   w w w  . ja  va 2  s . c  o m*/
        return element.getAsDouble();
    case FLOAT:
        return element.getAsFloat();
    case INT:
        return element.getAsInt();
    case LONG:
        return element.getAsLong();
    case NULL:
        return null;
    case UNION:
        return getElementAsType(schema.getTypes().get(0), element);
    //      case FIXED:
    //      case ARRAY:
    //      case BYTES:
    //      case ENUM:
    //      case MAP:
    //      case RECORD:
    //      case STRING:
    default:
        return element.getAsString();
    }
}

From source file:ch.jamiete.hilda.configuration.Configuration.java

License:Apache License

public int getInteger(final String name, final int def) {
    final JsonElement ele = this.json.get(name);

    if (ele == null) {
        return def;
    }//  w  ww  . j  av  a2 s.  c  om

    try {
        return ele.getAsInt();
    } catch (final Exception e) {
        return def;
    }
}

From source file:co.cask.cdap.proto.codec.MapReduceSpecificationCodec.java

License:Apache License

/**
 * Deserialize the resources field from the serialized object.
 *
 * @param jsonObj The object representing the MapReduceSpecification
 * @param prefix Field name prefix. Either "mapper" or "reducer"
 * @param context The context to deserialize object.
 * @return A {@link Resources} or {@code null}.
 *//*from w  ww. j a  v a 2 s .c o  m*/
private Resources deserializeResources(JsonObject jsonObj, String prefix, JsonDeserializationContext context) {
    // See if it of new format
    String name = prefix + "Resources";
    JsonElement element = jsonObj.get(name);
    if (element != null) {
        return context.deserialize(element, Resources.class);
    }

    // Try the old format, which is an int field representing the memory in MB.
    name = prefix + "MemoryMB";
    element = jsonObj.get(name);
    if (element != null) {
        return new Resources(element.getAsInt());
    }
    return null;
}