List of usage examples for com.google.gson JsonElement getAsCharacter
@Deprecated public char getAsCharacter()
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//w w w .ja v a 2 s . c o m 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:com.cloud.bridge.util.JsonAccessor.java
License:Apache License
public char getAsCharacter(String propPath) { JsonElement jsonElement = eval(propPath); return jsonElement.getAsCharacter(); }
From source file:net.doubledoordev.backend.util.TypeHellhole.java
License:Open Source License
public static void set(Field field, Object object, JsonElement value) throws Exception { if (field.getType() == byte.class) field.setByte(object, value.getAsByte()); else if (field.getType() == short.class) field.setShort(object, value.getAsShort()); else if (field.getType() == int.class) field.setInt(object, value.getAsInt()); else if (field.getType() == long.class) field.setLong(object, value.getAsLong()); else if (field.getType() == float.class) field.setFloat(object, value.getAsFloat()); else if (field.getType() == double.class) field.setDouble(object, value.getAsDouble()); else if (field.getType() == boolean.class) field.setBoolean(object, value.getAsBoolean()); else if (field.getType() == char.class) field.setChar(object, value.getAsCharacter()); ////from w ww. j ava 2 s . c om else if (field.getType() == Byte.class) field.set(object, value.getAsByte()); else if (field.getType() == Short.class) field.set(object, value.getAsShort()); else if (field.getType() == Integer.class) field.set(object, value.getAsInt()); else if (field.getType() == Long.class) field.set(object, value.getAsLong()); else if (field.getType() == Float.class) field.set(object, value.getAsFloat()); else if (field.getType() == Double.class) field.set(object, value.getAsDouble()); else if (field.getType() == Boolean.class) field.set(object, value.getAsBoolean()); else if (field.getType() == Character.class) field.set(object, value.getAsCharacter()); // else if (field.getType() == String.class) field.set(object, value.getAsString()); else { String m = String.format("Unknown type! Field type: %s Json value: %s Data class: %s", field.getType(), value.toString(), object.getClass().getSimpleName()); Main.LOGGER.error(m); throw new Exception(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 w w . java 2s. co 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 www .ja va2 s . c om } } return bundle; } return null; }
From source file:org.structr.core.PropertySetGSONAdapter.java
License:Open Source License
private Object getTypedValue(JsonElement valueElement, String type) { Object value = null;//from w ww.j a v a2s . c om if ((type == null) || type.equals("null")) { value = valueElement.getAsJsonNull(); } else if (type.equals("String")) { value = valueElement.getAsString(); } else if (type.equals("Number")) { value = valueElement.getAsNumber(); } else if (type.equals("Boolean")) { value = valueElement.getAsBoolean(); } else if (type.equals("JsonArray")) { value = valueElement.getAsJsonArray(); } else if (type.equals("JsonObject")) { value = valueElement.getAsJsonObject(); } else if (type.equals("Integer")) { value = valueElement.getAsInt(); } else if (type.equals("Long")) { value = valueElement.getAsLong(); } else if (type.equals("Double")) { value = valueElement.getAsDouble(); } else if (type.equals("Float")) { value = valueElement.getAsFloat(); } else if (type.equals("Byte")) { value = valueElement.getAsByte(); } else if (type.equals("Short")) { value = valueElement.getAsShort(); } else if (type.equals("Character")) { value = valueElement.getAsCharacter(); } else if (type.equals("BigDecimal")) { value = valueElement.getAsBigDecimal(); } else if (type.equals("BigInteger")) { value = valueElement.getAsBigInteger(); } return value; }