List of usage examples for com.google.gson JsonElement getAsDouble
public double getAsDouble()
From source file:com.sldeditor.exportdata.esri.symbols.BaseSymbol.java
License:Open Source License
/** * Gets the value as a double.// www .j a v a2 s .c o m * * @param obj the obj * @param field the field * @return the double */ protected static double getDouble(JsonObject obj, String field) { double value = 0.0; if (obj != null) { JsonElement element = obj.get(field); if (element != null) { value = element.getAsDouble(); } } return value; }
From source file:com.sldeditor.exportdata.esri.symbols.CartographicLineSymbol.java
License:Open Source License
/** * Convert.//from w ww. j a v a 2 s . c o m * * @param element the element * @return the list of strokes */ @SuppressWarnings("deprecation") @Override public List<Stroke> convert(JsonElement element) { if (element == null) return null; JsonObject obj = element.getAsJsonObject(); List<Stroke> strokeList = new ArrayList<Stroke>(); double width = getDouble(obj, CommonSymbolKeys.WIDTH); Stroke stroke = styleFactory.createStroke(getColour(obj.get(CommonSymbolKeys.COLOUR)), ff.literal(width)); // Line dash pattern JsonElement templateElement = obj.get(CartographicLineSymbolKeys.TEMPLATE); if (templateElement != null) { JsonArray templateArray = templateElement.getAsJsonArray(); List<Float> patternList = new ArrayList<Float>(); for (int index = 0; index < templateArray.size(); index++) { JsonObject patternElement = templateArray.get(index).getAsJsonObject(); float mark = 0.0f; JsonElement markElement = patternElement.get(CartographicLineSymbolKeys.MARK); if (markElement != null) { mark = markElement.getAsFloat(); } patternList.add(mark); float gap = 0.0f; JsonElement gapElement = patternElement.get(CartographicLineSymbolKeys.GAP); if (gapElement != null) { gap = gapElement.getAsFloat(); } patternList.add(gap); } float[] dashArray = new float[patternList.size()]; int index = 0; for (Float value : patternList) { dashArray[index] = value; index++; } stroke.setDashArray(dashArray); // Start of line offset JsonElement lineStartOffsetElement = obj.get(CartographicLineSymbolKeys.LINE_START_OFFSET); if (lineStartOffsetElement != null) { stroke.setDashOffset(ff.literal(lineStartOffsetElement.getAsDouble())); } } strokeList.add(stroke); return strokeList; }
From source file:com.solidfire.jsvcgen.serialization.OptionalAdapter.java
License:Open Source License
/** * Deserializes an Optional object.// w ww . j a v a 2 s . com * * @param json the element to deserialize. * @param typeOfT type of the expected return value. * @param context Context used for deserialization. * @return An Optional object containing an object of type typeOfT. */ public Optional<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { if (!json.isJsonObject() && !json.isJsonArray()) { if (json.isJsonNull() || json.getAsString() == null) { return Optional.empty(); } } ParameterizedType pType = (ParameterizedType) typeOfT; Type genericType = pType.getActualTypeArguments()[0]; // Special handling for string, "" will return Optional.of("") if (genericType.equals(String.class)) { return Optional.of(json.getAsString()); } if (!json.isJsonObject() && !json.isJsonArray() && json.getAsString().trim().length() == 0) { return Optional.empty(); } if (json.isJsonObject() || json.isJsonArray()) { if (json.isJsonNull()) { return Optional.empty(); } } if (genericType.equals(Integer.class)) { return Optional.of(json.getAsInt()); } else if (genericType.equals(Long.class)) { return Optional.of(json.getAsLong()); } else if (genericType.equals(Double.class)) { return Optional.of(json.getAsDouble()); } // Defer deserialization to handler for type contained in Optional Object obj = context.deserialize(json, genericType); OptionalAdaptorUtils.initializeAllNullOptionalFieldsAsEmpty(obj); return Optional.of(obj); }
From source file:com.stratio.decision.serializer.gson.impl.ColumnNameTypeValueDeserializer.java
License:Apache License
@Override public ColumnNameTypeValue deserialize(JsonElement element, Type type, JsonDeserializationContext ctx) throws JsonParseException { final JsonObject object = element.getAsJsonObject(); String name = null;/* ww w . j a va 2s . co m*/ ColumnType columnType = null; Object value = null; if (object != null && object.has(COLUMN_FIELD) && object.has(TYPE_FIELD)) { name = object.get(COLUMN_FIELD).getAsString(); columnType = ColumnType.valueOf(object.get(TYPE_FIELD).getAsString()); if (object.has(VALUE_FIELD)) { JsonElement jsonValue = object.get(VALUE_FIELD); switch (columnType) { case BOOLEAN: value = jsonValue.getAsBoolean(); break; case DOUBLE: value = jsonValue.getAsDouble(); break; case FLOAT: value = jsonValue.getAsFloat(); break; case INTEGER: value = jsonValue.getAsInt(); break; case LONG: value = jsonValue.getAsLong(); break; case STRING: value = jsonValue.getAsString(); break; default: break; } } else { log.warn("Column with name {} has no value", name); } if (log.isDebugEnabled()) { log.debug("Values obtained into ColumnNameTypeValue deserialization: " + "NAME: {}, VALUE: {}, COLUMNTYPE: {}", name, value, columnType); } } else { log.warn("Error deserializing ColumnNameTypeValue from json. JsonObject is not complete: {}", element); } return new ColumnNameTypeValue(name, columnType, value); }
From source file:com.strato.hidrive.api.JSONDataReader.java
License:Apache License
@Override public double readDoubleWithName(String name) { double defaultValue = 0D; return get(name, defaultValue, new ElementValue<Double>() { @Override// w ww.ja va2s . co m public Double value(JsonElement jsonElement) { return jsonElement.getAsDouble(); } }); }
From source file:com.tsc9526.monalisa.orm.tools.converters.impl.ArrayTypeConversion.java
License:Open Source License
protected Object convertJsonToArray(JsonArray array, Class<?> type) { Object value = null;//from w w w .j ava2s . co m if (type == int[].class) { int[] iv = new int[array.size()]; for (int i = 0; i < array.size(); i++) { JsonElement e = array.get(i); iv[i] = e.getAsInt(); } value = iv; } else if (type == float[].class) { float[] iv = new float[array.size()]; for (int i = 0; i < array.size(); i++) { JsonElement e = array.get(i); iv[i] = e.getAsFloat(); } value = iv; } else if (type == long[].class) { long[] iv = new long[array.size()]; for (int i = 0; i < array.size(); i++) { JsonElement e = array.get(i); iv[i] = e.getAsLong(); } value = iv; } else if (type == double[].class) { double[] iv = new double[array.size()]; for (int i = 0; i < array.size(); i++) { JsonElement e = array.get(i); iv[i] = e.getAsDouble(); } value = iv; } else {//String[] String[] iv = new String[array.size()]; for (int i = 0; i < array.size(); i++) { JsonElement e = array.get(i); if (e.isJsonPrimitive()) { iv[i] = e.getAsString(); } else { iv[i] = e.toString(); } } value = iv; } return value; }
From source file:com.tsc9526.monalisa.tools.json.MelpJson.java
License:Open Source License
public static double getDouble(JsonObject json, String name, double defaultValue) { JsonElement e = json.get(name); if (e == null || e.isJsonNull()) { return defaultValue; } else {/*from w w w. ja v a 2s . c om*/ return e.getAsDouble(); } }
From source file:com.tsp.solver.Solver.java
public static ArrayList<ArrayList<Double>> getDistancesFromMapBox(List<LatLng> places) { List<String> addresses = places.stream().map(x -> x.toLngLat()).collect(Collectors.toList()); ArrayList<ArrayList<Double>> result = new ArrayList<>(places.size()); for (int i = 0; i < places.size(); ++i) { result.add(new ArrayList<Double>(places.size())); for (int j = 0; j < places.size(); ++j) { result.get(i).add(0.0);// ww w . ja v a2 s. c om } } String url_start = "https://api.mapbox.com/directions-matrix/v1/mapbox/driving/"; String destinations = "&destinations="; String sources = "?sources="; String key = "access_token=pk.eyJ1IjoibGluYXI5NSIsImEiOiJjajNyOHFreXcwMDBwNHVvMmIwdDA3ZTdpIn0.nnpzy7elxJr2cM-gPKaAZQ"; int count = places.size(); if (count <= 25) { String coords = ""; for (int i = 0; i < count; ++i) { if (i != 0) { coords += ";"; } coords += addresses.get(i); } String url = url_start + coords + "?" + key; log.debug("URL=" + url); try { //Thread.sleep(100); InputStreamReader in = new InputStreamReader((new URL(url)).openStream()); JsonElement jelement = new JsonParser().parse(in); JsonObject jobject = jelement.getAsJsonObject(); JsonArray rows = jobject.getAsJsonArray("durations"); int i = 0; for (JsonElement je : rows) { JsonArray elements = je.getAsJsonArray(); int j = 0; for (JsonElement je2 : elements) { double value = je2.getAsDouble(); result.get(i).set(j, value * 1.0); j++; } i++; } } catch (Exception ex) { java.util.logging.Logger.getLogger(Solver.class.getName()).log(Level.SEVERE, "Unknown exception :D", ex); } return result; } //if count > 25 int url_count = 0; int maxSize = 12; //max size of Matrix for one request 12*12 for (int i = 0; i <= places.size() / maxSize; ++i) { int n = Math.min(count, maxSize); String coords1 = ""; String src = new String(sources); for (int j = i * maxSize; j < i * maxSize + n; ++j) { if (j != i * maxSize) { coords1 += ";"; src += ";"; } coords1 += addresses.get(j); src += j - i * maxSize; } int count2 = places.size(); for (int k = 0; k <= places.size() / maxSize; ++k) { int m = Math.min(maxSize, count2); String coords2 = ""; String dst = new String(destinations); for (int j = k * maxSize; j < k * maxSize + m; ++j) { if (j != k * maxSize) { dst += ";"; } dst += j - k * maxSize; coords2 += ";"; coords2 += addresses.get(j); } count2 -= m; String url = url_start + coords1 + coords2 + src + dst + "&" + key; log.debug("URL=" + url); try { if (url_count == 60) { Thread.sleep(60000); url_count = 0; } InputStreamReader in = new InputStreamReader((new URL(url)).openStream()); url_count++; JsonElement jelement = new JsonParser().parse(in); JsonObject jobject = jelement.getAsJsonObject(); JsonArray rows = jobject.getAsJsonArray("durations"); int ii = i * maxSize; for (JsonElement je : rows) { JsonArray elements = je.getAsJsonArray(); int jj = k * maxSize; for (JsonElement je2 : elements) { double value = je2.getAsDouble(); result.get(ii).set(jj, value * 1.0); jj++; } ii++; } } catch (Exception ex) { java.util.logging.Logger.getLogger(Solver.class.getName()).log(Level.SEVERE, "Unknown exception :D", ex); } } count -= n; if (count == 0) break; } return result; }
From source file:com.unovo.frame.utils.gson.deserializer.DoubleJsonDeserializer.java
License:Open Source License
@Override public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try {// ww w. ja v a 2 s. c o m return json.getAsDouble(); } catch (Exception e) { Logger.i("DoubleJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : "")); return 0D; } }
From source file:com.wallellen.wechat.common.util.json.GsonHelper.java
License:Open Source License
public static Double getAsDouble(JsonElement element) { return isNull(element) ? null : element.getAsDouble(); }