List of usage examples for com.google.gson JsonElement getAsFloat
public float getAsFloat()
From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java
License:Apache License
/** * convert a jsonElement into Object//from w ww .j av a2 s . c om * * @param type * @param element * @return * @throws RepresentationException */ protected <T> T get(Class<T> type, JsonElement element) throws RepresentationException { if (type == Long.class) { return (T) (Long) element.getAsLong(); } else if (type == Integer.class) { return (T) (Integer) element.getAsInt(); } else if (type == Short.class) { return (T) (Short) element.getAsShort(); } else if (type == Byte.class) { return (T) (Byte) element.getAsByte(); } else if (type == BigInteger.class) { return (T) (BigInteger) element.getAsBigInteger(); } else if (type == Double.class) { return (T) (Double) element.getAsDouble(); } else if (type == Float.class) { return (T) (Float) element.getAsFloat(); } else if (type == BigDecimal.class) { return (T) (BigDecimal) element.getAsBigDecimal(); } else if (type == Boolean.class) { return (T) (Boolean) element.getAsBoolean(); } else if (type == String.class) { return (T) element.getAsString(); } else { return (T) gson.fromJson(element, type); } }
From source file:logic.Util.java
private static CVE getCveFromJson(CVE cve, JsonObject access, JsonElement cvss, JsonElement cwe, JsonObject impact) {// ww w . j ava 2 s. com CVE newCve = new CVE(); newCve.setId(cve.getId()); newCve.setVendor(cve.getVendor()); newCve.setProduct(cve.getProduct()); newCve.setSearchedVersion(cve.getSearchedVersion()); newCve.setCpe(cve.getCpe()); try { newCve.setAccess(new Access(access.get("authentication").getAsString(), access.get("complexity").getAsString(), access.get("vector").getAsString())); } catch (Exception e) { } try { newCve.setCvss(cvss.getAsFloat()); } catch (Exception e) { } try { newCve.setCwe(cwe.getAsString()); } catch (Exception e) { } try { newCve.setImpact(new Impact(impact.get("availability").getAsString(), impact.get("confidentiality").getAsString(), impact.get("integrity").getAsString())); } catch (Exception e) { } return newCve; }
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
/** * Gets the float value of the given JsonElement. Expects the second parameter to be the name of the element's * field if an error message needs to be thrown. *//*from ww w. j ava 2 s .co m*/ public static float getFloat(JsonElement json, String memberName) { if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) { return json.getAsFloat(); } else { throw new JsonSyntaxException("Expected " + memberName + " to be a Float, was " + toString(json)); } }
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 w w .j a v a 2 s. c o m*/ 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.apache.gobblin.converter.grok.GrokToJsonConverter.java
License:Apache License
@VisibleForTesting JsonObject createOutput(JsonArray outputSchema, String inputRecord) throws DataConversionException { JsonObject outputRecord = new JsonObject(); Match gm = grok.match(inputRecord);/*w w w. j a v a 2s . c o m*/ gm.captures(); JsonElement capturesJson = JSON_PARSER.parse(gm.toJson()); for (JsonElement anOutputSchema : outputSchema) { JsonObject outputSchemaJsonObject = anOutputSchema.getAsJsonObject(); String key = outputSchemaJsonObject.get(COLUMN_NAME_KEY).getAsString(); String type = outputSchemaJsonObject.getAsJsonObject(DATA_TYPE).get(TYPE_KEY).getAsString(); if (isFieldNull(capturesJson, key)) { if (!outputSchemaJsonObject.get(NULLABLE).getAsBoolean()) { throw new DataConversionException( "Field " + key + " is null or not exists but it is non-nullable by the schema."); } outputRecord.add(key, JsonNull.INSTANCE); } else { JsonElement jsonElement = capturesJson.getAsJsonObject().get(key); switch (type) { case "int": outputRecord.addProperty(key, jsonElement.getAsInt()); break; case "long": outputRecord.addProperty(key, jsonElement.getAsLong()); break; case "double": outputRecord.addProperty(key, jsonElement.getAsDouble()); break; case "float": outputRecord.addProperty(key, jsonElement.getAsFloat()); break; case "boolean": outputRecord.addProperty(key, jsonElement.getAsBoolean()); break; case "string": default: outputRecord.addProperty(key, jsonElement.getAsString()); } } } return outputRecord; }
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 ww . ja va2s. c om*/ 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)); }/* w w w. j a va 2 s . co m*/ } } return bundle; } return null; }
From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java
License:LGPL
public void addDocumentField(Document tmp, JsonElement jsonValue) { if (jsonValue == null || jsonValue.isJsonNull()) { return;//from ww w . ja v a2 s. c o m } switch (fieldType) { case INTEGER: tmp.add(new IntField(absoluteName, jsonValue.getAsInt(), Store.NO)); break; case LONG: tmp.add(new LongField(absoluteName, jsonValue.getAsLong(), Store.NO)); break; case FLOAT: tmp.add(new FloatField(absoluteName, jsonValue.getAsFloat(), Store.NO)); break; case DOUBLE: tmp.add(new DoubleField(absoluteName, jsonValue.getAsDouble(), Store.NO)); break; case UNKNOWN_NUMERIC: throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName); case BOOLEAN: tmp.add(new StringField(absoluteName, String.valueOf(jsonValue.getAsBoolean()), Store.NO)); break; default: tmp.add(new StringField(absoluteName, jsonValue.getAsString(), Store.NO)); break; } }
From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java
License:LGPL
@Override public Object convertHit(JsonObject hit, ConversionContext conversionContext) { JsonElement jsonValue = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName); if (jsonValue == null || jsonValue.isJsonNull()) { return null; }/*from ww w. j a va 2 s.c o m*/ switch (fieldType) { case INTEGER: return jsonValue.getAsInt(); case LONG: return jsonValue.getAsLong(); case FLOAT: return jsonValue.getAsFloat(); case DOUBLE: return jsonValue.getAsDouble(); case UNKNOWN_NUMERIC: throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName); case BOOLEAN: return jsonValue.getAsBoolean(); default: return jsonValue.getAsString(); } }
From source file:org.lanternpowered.server.script.function.value.json.ConstantFloatValueProviderJsonSerializer.java
License:MIT License
@Override public FloatValueProvider.Constant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return FloatValueProvider.constant(json.getAsFloat()); }