Java Code Examples for com.google.gson.JsonPrimitive#getAsLong()

The following examples show how to use com.google.gson.JsonPrimitive#getAsLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LwM2mNodeDeserializer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Object deserializeValue(JsonPrimitive val, org.eclipse.leshan.core.model.ResourceModel.Type expectedType) {
    switch (expectedType) {
    case BOOLEAN:
        return val.getAsBoolean();
    case STRING:
        return val.getAsString();
    case INTEGER:
        return val.getAsLong();
    case FLOAT:
        return val.getAsDouble();
    case TIME:
    case OPAQUE:
    default:
        // TODO we need to better handle this.
        return val.getAsString();
    }
}
 
Example 2
Source File: LwM2mNodeDeserializer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Object deserializeValue(JsonPrimitive val, org.eclipse.leshan.core.model.ResourceModel.Type expectedType) {
    switch (expectedType) {
    case BOOLEAN:
        return val.getAsBoolean();
    case STRING:
        return val.getAsString();
    case INTEGER:
        return val.getAsLong();
    case FLOAT:
        return val.getAsDouble();
    case TIME:
    case OPAQUE:
    default:
        // TODO we need to better handle this.
        return val.getAsString();
    }
}
 
Example 3
Source File: GsonObjectDeserializer.java    From qaf with MIT License 5 votes vote down vote up
public static Object read(JsonElement in) {

		if (in.isJsonArray()) {
			List<Object> list = new ArrayList<Object>();
			JsonArray arr = in.getAsJsonArray();
			for (JsonElement anArr : arr) {
				list.add(read(anArr));
			}
			return list;
		} else if (in.isJsonObject()) {
			Map<String, Object> map = new LinkedTreeMap<String, Object>();
			JsonObject obj = in.getAsJsonObject();
			Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
			for (Map.Entry<String, JsonElement> entry : entitySet) {
				map.put(entry.getKey(), read(entry.getValue()));
			}
			return map;
		} else if (in.isJsonPrimitive()) {
			JsonPrimitive prim = in.getAsJsonPrimitive();
			if (prim.isBoolean()) {
				return prim.getAsBoolean();
			} else if (prim.isString()) {
				return prim.getAsString();
			} else if (prim.isNumber()) {
				if (prim.getAsString().contains("."))
					return prim.getAsDouble();
				else {
					return prim.getAsLong();
				}
			}
		}
		return null;
	}
 
Example 4
Source File: LwM2mNodeDeserializer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) {
    if (val.isBoolean())
        return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
    if (val.isString())
        return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
    if (val.isNumber()) {
        if (val.getAsDouble() == (double) val.getAsLong()) {
            return org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER;
        } else {
            return org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT;
        }
    }
    // use string as default value
    return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
}
 
Example 5
Source File: MobileServiceTableBase.java    From azure-mobile-apps-android-client with Apache License 2.0 5 votes vote down vote up
/**
 * @return the numeric value represented by the object.
 *
 * @param o the object for which numberic value is to be extracted
 */
protected long getNumericValue(Object o) {
    long result;

    if (o instanceof Integer) {
        result = (Integer) o;
    } else if (o instanceof Long) {
        result = (Long) o;
    } else if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();

            if (primitive.isNumber()) {
                result = primitive.getAsLong();
            } else {
                throw new IllegalArgumentException("Object does not represent a string value.");
            }
        } else {
            throw new IllegalArgumentException("Object does not represent a string value.");
        }
    } else {
        throw new IllegalArgumentException("Object does not represent a string value.");
    }

    return result;
}
 
Example 6
Source File: LwM2mNodeDeserializer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) {
    if (val.isBoolean())
        return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
    if (val.isString())
        return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
    if (val.isNumber()) {
        if (val.getAsDouble() == (double) val.getAsLong()) {
            return org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER;
        } else {
            return org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT;
        }
    }
    // use string as default value
    return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
}
 
Example 7
Source File: QueryOptions.java    From yawp with MIT License 5 votes vote down vote up
private Object getJsonObjectValue(JsonElement jsonElement) {
    if (jsonElement.isJsonArray()) {
        return getJsonObjectValueForArrays(jsonElement);
    }
    if (jsonElement.isJsonNull()) {
        return null;
    }

    JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();

    if (jsonPrimitive.isNumber()) {
        if (jsonPrimitive.getAsString().indexOf(".") != -1) {
            return jsonPrimitive.getAsDouble();
        }
        return jsonPrimitive.getAsLong();
    }

    if (jsonPrimitive.isString()) {
        return jsonPrimitive.getAsString();
    }

    if (jsonPrimitive.isBoolean()) {
        return jsonPrimitive.getAsBoolean();
    }

    // TODO timestamp
    throw new RuntimeException("Invalid json value: " + jsonPrimitive.getAsString());
}
 
Example 8
Source File: GsonDeserializerObjectWrapper.java    From qaf with MIT License 4 votes vote down vote up
private Object read(JsonElement in, Type typeOfT) {
	
	if (in.isJsonArray()) {
		JsonArray arr = in.getAsJsonArray();

		boolean isArray = isArray(typeOfT);
		Collection<Object> list = isArray ? new ArrayList<Object>()
				: context.deserialize(new JsonArray(0), typeOfT);
		for (JsonElement anArr : arr) {
			((Collection<Object>) list).add(read(anArr, getTypeArguments(typeOfT, 0)));
		}
		if (isArray) {
			return toArray((List<Object>) list);
		}
		try {
			return ClassUtil.getClass(typeOfT).cast(list);
		} catch (Exception e) {
			return context.deserialize(in, typeOfT);
		}
	} else if (in.isJsonObject()
			&& (isAssignableFrom(typeOfT, Map.class) || ClassUtil.getClass(typeOfT).equals(Object.class))) {
		Map<Object, Object> map = context.deserialize(new JsonObject(), typeOfT);//new LinkedTreeMap<Object, Object>();
		JsonObject obj = in.getAsJsonObject();
		Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
		for (Map.Entry<String, JsonElement> entry : entitySet) {
			map.put(entry.getKey(), read(entry.getValue(), getTypeArguments(typeOfT, 1)));
		}
		return map;
	} else if (in.isJsonPrimitive() && ClassUtil.getClass(typeOfT).equals(Object.class)) {
		JsonPrimitive prim = in.getAsJsonPrimitive();
		if (prim.isBoolean()) {
			return prim.getAsBoolean();
		} else if (prim.isString()) {
			return prim.getAsString();
		} else if (prim.isNumber()) {
			if (prim.getAsString().contains("."))
				return prim.getAsDouble();
			else {
				return prim.getAsLong();
			}
		}
	}
	return context.deserialize(in, typeOfT);
}
 
Example 9
Source File: JsonUtils.java    From yandex-money-sdk-java with MIT License 2 votes vote down vote up
/**
 * Gets nullable Long from a JSON object.
 *
 * @param object json object
 * @param memberName member's name
 * @return {@link Long} value
 */
public static Long getLong(JsonObject object, String memberName) {
    JsonPrimitive primitive = getPrimitiveChecked(object, memberName);
    return primitive == null ? null : primitive.getAsLong();
}