List of usage examples for com.google.gson JsonElement getAsDouble
public double getAsDouble()
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 va 2s .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;//w w w .ja va 2 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 www .j ava 2s.c om 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.hibernate.search.elasticsearch.query.impl.QueryHitConverter.java
License:LGPL
public EntityInfo convert(SearchResult searchResult, JsonObject hit) { String type = hit.get("_type").getAsString(); EntityIndexBinding binding = targetedEntityBindingsByName.get(type); if (binding == null) { LOG.warnf("Found unknown type in Elasticsearch index: " + type); return null; }//w ww . jav a 2 s . c om DocumentBuilderIndexedEntity documentBuilder = binding.getDocumentBuilder(); IndexedTypeIdentifier typeId = documentBuilder.getTypeIdentifier(); ConversionContext conversionContext = new ContextualExceptionBridgeHelper(); conversionContext.setConvertedTypeId(typeId); FieldProjection idProjection = idProjectionByEntityBinding.get(binding); Object id = idProjection.convertHit(hit, conversionContext); Object[] projections = null; if (projectedFields != null) { projections = new Object[projectedFields.length]; for (int i = 0; i < projections.length; i++) { String field = projectedFields[i]; if (field == null) { continue; } switch (field) { case ElasticsearchProjectionConstants.SOURCE: projections[i] = hit.getAsJsonObject().get("_source").toString(); break; case ElasticsearchProjectionConstants.ID: projections[i] = id; break; case ElasticsearchProjectionConstants.OBJECT_CLASS: projections[i] = typeId.getPojoType(); break; case ElasticsearchProjectionConstants.SCORE: projections[i] = hit.getAsJsonObject().get("_score").getAsFloat(); break; case ElasticsearchProjectionConstants.SPATIAL_DISTANCE: JsonElement distance = null; // if we sort by distance, we need to find the index of the DistanceSortField and use it // to extract the values from the sort array // if we don't sort by distance, we use the field generated by the script_field added earlier if (sortByDistanceIndex != null) { distance = hit.getAsJsonObject().get("sort").getAsJsonArray().get(sortByDistanceIndex); } else { JsonElement fields = hit.getAsJsonObject().get("fields"); if (fields != null) { // "fields" seems to be missing if there are only null results in script fields distance = hit.getAsJsonObject().get("fields").getAsJsonObject() .get(SPATIAL_DISTANCE_FIELD); } } if (distance != null && distance.isJsonArray()) { JsonArray array = distance.getAsJsonArray(); distance = array.size() >= 1 ? array.get(0) : null; } if (distance == null || distance.isJsonNull()) { projections[i] = null; } else { Double distanceAsDouble = distance.getAsDouble(); if (distanceAsDouble == Double.MAX_VALUE || distanceAsDouble.isInfinite()) { /* * When we extract the distance from the sort, its default value is: * - Double.MAX_VALUE on older ES versions (5.0 and lower) * - Double.POSITIVE_INFINITY on newer ES versions (from somewhere around 5.2 onwards) */ projections[i] = null; } else { projections[i] = distance.getAsDouble(); } } break; case ElasticsearchProjectionConstants.TOOK: projections[i] = searchResult.getTook(); break; case ElasticsearchProjectionConstants.TIMED_OUT: projections[i] = searchResult.getTimedOut(); break; case ElasticsearchProjectionConstants.THIS: // Use EntityInfo.ENTITY_PLACEHOLDER as placeholder. // It will be replaced when we populate // the EntityInfo with the real entity. projections[i] = EntityInfo.ENTITY_PLACEHOLDER; break; default: FieldProjection projection = fieldProjectionsByEntityBinding.get(binding)[i]; projections[i] = projection.convertHit(hit, conversionContext); } } } return new EntityInfoImpl(typeId, documentBuilder.getIdPropertyName(), (Serializable) id, projections); }
From source file:org.lanternpowered.server.script.function.value.json.ConstantDoubleValueProviderJsonSerializer.java
License:MIT License
@Override public DoubleValueProvider.Constant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return DoubleValueProvider.constant(json.getAsDouble()); }
From source file:org.openhab.binding.miio.internal.basic.Conversions.java
License:Open Source License
public static JsonElement divideTen(JsonElement value10) { double value = value10.getAsDouble() / 10; return new JsonPrimitive(value); }
From source file:org.openstreetmap.josm.plugins.scoutsigns.service.deserializer.SignPositionDeserializer.java
License:Apache License
@Override public SignPosition deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException { final JsonObject obj = (JsonObject) jsonElement; final double lat = obj.get(LATITUDE).getAsDouble(); final double lon = obj.get(LONGITUDE).getAsDouble(); // height is null for searchSign responses final JsonElement heightElement = obj.get(HEIGHT); final Double height = heightElement != null ? heightElement.getAsDouble() : null; return new SignPosition(new LatLon(lat, lon), height); }
From source file:org.qcert.runtime.BinaryOperators.java
License:Apache License
static double asDouble(JsonElement e) { return e.getAsDouble(); }
From source file:org.qcert.runtime.DataComparator.java
License:Apache License
/** Note: this comparator * imposes orderings that are inconsistent with equals. *//*from w ww . j a va 2 s . c o m*/ @Override public int compare(JsonElement o1, JsonElement o2) { // short-circuit in this case if (o1 == o2) { return 0; } DType typ1 = getType(o1); DType typ2 = getType(o2); // For lazily parsed numbers, check type of other operand and try and coerce if (typ1 == DType.DT_LAZYNUM) { switch (typ2) { case DT_LONG: return Long.compare(o1.getAsLong(), o2.getAsLong()); case DT_DOUBLE: return Double.compare(o1.getAsDouble(), o2.getAsDouble()); case DT_LAZYNUM: // Tricky here... there is no way to know what to coerce to, // underlying gson code relies on string equality, hence so do we typ1 = DType.DT_STRING; typ2 = DType.DT_STRING; } } else if (typ2 == DType.DT_LAZYNUM) { switch (typ1) { case DT_LONG: return Long.compare(o1.getAsLong(), o2.getAsLong()); case DT_DOUBLE: return Double.compare(o1.getAsDouble(), o2.getAsDouble()); } } final int typCompare = typ1.compareTo(typ2); if (typCompare != 0) { return typCompare; } switch (typ1) { case DT_JNULL: case DT_NULL: return 0; case DT_BOOL: return Boolean.compare(o1.getAsBoolean(), o2.getAsBoolean()); case DT_STRING: String str1 = o1.getAsString(); String str2 = o2.getAsString(); // TODO // WARNING // HACK // special hack for dates. // what could go wrong??? :-D // of course, this breaks the transitivity of compareTo // sigh... // a real solution to this is a bit challenging // since we need type information // or a wrapper around date times. try { final ZonedDateTime date1 = ZonedDateTime.parse(str1); final ZonedDateTime date2 = ZonedDateTime.parse(str2); // if they are both parseable as dates, we will compare them as dates return date1.toInstant().compareTo(date2.toInstant()); } catch (DateTimeException e) { // If they are not both parseable as dates, just compare them as strings return str1.compareTo(str2); } case DT_LONG: return Long.compare(o1.getAsLong(), o2.getAsLong()); case DT_DOUBLE: return Double.compare(o1.getAsDouble(), o2.getAsDouble()); case DT_COLL: return compare(o1.getAsJsonArray(), o2.getAsJsonArray()); case DT_REC: return compare(o1.getAsJsonObject(), o2.getAsJsonObject()); default: // We should never get here. // but if we do, we can use toString to give // a deterministic order return o1.toString().compareTo(o2.toString()); } }
From source file:org.qcert.runtime.UnaryOperators.java
License:Apache License
public static JsonElement float_neg(JsonElement e) { return new JsonPrimitive(-e.getAsDouble()); }