Java Code Examples for com.google.gson.JsonElement#getAsDouble()

The following examples show how to use com.google.gson.JsonElement#getAsDouble() . 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: ReflexDB.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static ReflexActionAlertmeLifesign convertAlAction(JsonObject al, JsonDeserializationContext context) {
   ReflexActionAlertmeLifesign.Type a = ReflexActionAlertmeLifesign.Type.valueOf(al.get("a").getAsString());

   Double m = null;
   Double n = null;

   JsonElement me = al.get("m");
   if (me != null && !me.isJsonNull()) {
      m = me.getAsDouble();
   }

   JsonElement ne = al.get("n");
   if (ne != null && !ne.isJsonNull()) {
      n = ne.getAsDouble();
   }

   if (m != null && n != null) {
      return new ReflexActionAlertmeLifesign(a,m,n);
   } else {
      return new ReflexActionAlertmeLifesign(a);
   }
}
 
Example 2
Source File: ReflexJson.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static ReflexActionAlertmeLifesign convertAlAction(JsonObject al, JsonDeserializationContext context) {
   ReflexActionAlertmeLifesign.Type a = ReflexActionAlertmeLifesign.Type.valueOf(al.get("a").getAsString());

   Double m = null;
   Double n = null;

   JsonElement me = al.get("m");
   if (me != null && !me.isJsonNull()) {
      m = me.getAsDouble();
   }

   JsonElement ne = al.get("n");
   if (ne != null && !ne.isJsonNull()) {
      n = ne.getAsDouble();
   }

   if (m != null && n != null) {
      return new ReflexActionAlertmeLifesign(a,m,n);
   } else {
      return new ReflexActionAlertmeLifesign(a);
   }
}
 
Example 3
Source File: GsonUtil.java    From star-zone-android with Apache License 2.0 5 votes vote down vote up
@Override
public Double deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    try {
        if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为double类型,如果后台返回""或者null,则返回0.00
            return 0.0;
        }
    } catch (Exception ignore) {
    }
    try {
        return json.getAsDouble();
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}
 
Example 4
Source File: TmfIntervalDeserializer.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITmfStateInterval deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject object = json.getAsJsonObject();
    long start = object.get(TmfIntervalStrings.START).getAsLong();
    long end = object.get(TmfIntervalStrings.END).getAsLong();
    int quark = object.get(TmfIntervalStrings.QUARK).getAsInt();
    String type = object.get(TmfIntervalStrings.TYPE).getAsString();
    if (type.equals(TmfIntervalStrings.NULL)) {
        return new TmfStateInterval(start, end, quark, (Object) null);
    }
    JsonElement value = object.get(TmfIntervalStrings.VALUE);
    try {
        Class<?> typeClass = Class.forName(type);

        if (typeClass.isAssignableFrom(CustomStateValue.class)) {
            String encoded = value.getAsString();
            byte[] serialized = Base64.getDecoder().decode(encoded);
            ByteBuffer buffer = ByteBuffer.wrap(serialized);
            ISafeByteBufferReader sbbr = SafeByteBufferFactory.wrapReader(buffer, serialized.length);
            TmfStateValue sv = CustomStateValue.readSerializedValue(sbbr);
            return new TmfStateInterval(start, end, quark, sv.unboxValue());
        }
        if (typeClass.isAssignableFrom(Integer.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsInt());
        } else if (typeClass.isAssignableFrom(Long.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsLong());
        } else if (typeClass.isAssignableFrom(Double.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsDouble());
        } else if (typeClass.isAssignableFrom(String.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsString());
        }
    } catch (ClassNotFoundException e) {
        // Fall through
    }
    // last ditch attempt
    return new TmfStateInterval(start, end, quark, value.toString());
}
 
Example 5
Source File: SliderSetting.java    From Wurst7 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public final void fromJson(JsonElement json)
{
	if(!JsonUtils.isNumber(json))
		return;
	
	double value = json.getAsDouble();
	if(value > maximum || value < minimum)
		return;
	
	setValueIgnoreLock(value);
}
 
Example 6
Source File: GsonUtil.java    From FriendCircle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Double deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    try {
        if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为double类型,如果后台返回""或者null,则返回0.00
            return 0.0;
        }
    } catch (Exception ignore) {
    }
    try {
        return json.getAsDouble();
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}
 
Example 7
Source File: JsonDeserializer.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public static double getDoubleValue(JsonElement jsonElement, String... paths) throws JsonPathNotFoundException, IOException {
    JsonElement jsonNode = getChildNode(jsonElement, paths);
    if (jsonNode != null) {
        return jsonNode.getAsDouble();
    }
    throw new JsonPathNotFoundException();
}
 
Example 8
Source File: CommonSupplementReader.java    From moql with Apache License 2.0 5 votes vote down vote up
protected void readHits(JsonObject hits) {
  JsonElement jValue = hits.getAsJsonPrimitive("total");
  totalHits = jValue.getAsInt();
  jValue = hits.get("max_score");
  if (!jValue.isJsonNull())
    maxScore = jValue.getAsDouble();
  JsonArray hitArray = hits.getAsJsonArray("hits");
  for (int i = 0; i < hitArray.size(); i++) {
    JsonObject jo = (JsonObject) hitArray.get(i);
    this.hitSupplements.add(readHit(jo));
  }
}
 
Example 9
Source File: LabelEngineLayerProperties.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Extract double.
 *
 * @param jsonObj the json obj
 * @param field the field
 * @return the double
 */
private double extractDouble(JsonObject jsonObj, String field)
{
    double value = 0.0;
    
    if(jsonObj != null)
    {
        JsonElement element = jsonObj.get(field);
        if(element != null)
        {
            value = element.getAsDouble();
        }
    }
    return value;
}
 
Example 10
Source File: JsonHelperGson.java    From symbol-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public Integer getInteger(Object object, String... path) {
    JsonElement child = getNode(convert(object, JsonObject.class), path);
    if (child == null || child.isJsonNull()) {
        return null;
    }
    if (child.isJsonObject()) {
        throw new IllegalArgumentException("Cannot extract a Integer from an json object");
    }
    return (int) child.getAsDouble();
}
 
Example 11
Source File: VectorColumnFiller.java    From secor with Apache License 2.0 5 votes vote down vote up
public void convert(JsonElement value, ColumnVector vect, int row) {
    if (value == null || value.isJsonNull()) {
        vect.noNulls = false;
        vect.isNull[row] = true;
    } else {
        DoubleColumnVector vector = (DoubleColumnVector) vect;
        vector.vector[row] = value.getAsDouble();
    }
}
 
Example 12
Source File: GsonFactory.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@Override
public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    try {
        if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为double类型,如果后台返回""或者null,则返回0.00
            return 0.00;
        }
    } catch (Exception ignore) {
    }
    try {
        return json.getAsDouble();
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}
 
Example 13
Source File: JsonUtil.java    From ja-micro with Apache License 2.0 5 votes vote down vote up
public double extractDouble(JsonObject json, String name, int defaultValue) {
    if (json != null) {
        int dotIndex = name.indexOf('.');
        if (dotIndex > 0) {
            String baseName = name.substring(0, dotIndex);
            JsonElement childElement = json.get(baseName);
            return extractDouble((JsonObject) childElement, name.substring(dotIndex + 1), defaultValue);
        }
        JsonElement element = json.get(name);
        if (element != null && ! element.isJsonNull()) {
            return element.getAsDouble();
        }
    }
    return defaultValue;
}
 
Example 14
Source File: BaseSymbol.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the value as a double.
 *
 * @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;
}
 
Example 15
Source File: GsonHelper.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public static Double getAsDouble(JsonElement element) {
	return isNull(element) ? null : element.getAsDouble();
}
 
Example 16
Source File: JsonElementConversionFactory.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
@Override
DoubleValue convertField(JsonElement value) {
  return new DoubleValue(value.getAsDouble());
}
 
Example 17
Source File: JsonElementConversionFactory.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
@Override
DoubleValue convertField(JsonElement value) {
  return new DoubleValue(value.getAsDouble());
}
 
Example 18
Source File: GsonUtil.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static Double getMemberAsDouble(JsonObject jsonObject, String memberName)
{
   JsonElement memberElement = jsonObject.get(memberName);

   return memberElement == null ? null : memberElement.getAsDouble();
}
 
Example 19
Source File: CocosBcxApi.java    From AndroidWallet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * get_vesting_balances
 *
 * @param accountNameOrId
 * @return get_vesting_balances
 */
public vesting_balances_result get_vesting_balances(String accountNameOrId) throws NetworkStatusException, AccountNotFoundException, NoRewardAvailableException, AssetNotFoundException {
    account_object account_object = get_account_object(accountNameOrId);
    if (account_object == null) {
        throw new AccountNotFoundException("Account does not exist");
    }
    List<vesting_balances_object> vesting_balances_objects = mWebSocketApi.get_vesting_balances(account_object.id.toString());
    if (null == vesting_balances_objects || vesting_balances_objects.size() <= 0) {
        throw new NoRewardAvailableException("No reward available");
    }

    vesting_balances_object vestingBalancesObject = vesting_balances_objects.get(0);
    JsonElement jsonElement = vestingBalancesObject.policy.get(1);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonElement coin_seconds_earned = jsonObject.get("coin_seconds_earned");
    JsonElement vesting_seconds = jsonObject.get("vesting_seconds");
    double gas_amount = coin_seconds_earned.getAsDouble() / vesting_seconds.getAsDouble();
    double available_percent = gas_amount / vestingBalancesObject.balance.amount;

    asset_object asset_object = lookup_asset_symbols(vestingBalancesObject.balance.asset_id.toString());
    if (null == asset_object) {
        throw new AssetNotFoundException("Asset does not exist");
    }
    long require_coindays = new BigDecimal(vestingBalancesObject.balance.amount / Math.pow(10, asset_object.precision)).setScale(0, BigDecimal.ROUND_HALF_EVEN).longValue();
    double return_cash = vestingBalancesObject.balance.amount / Math.pow(10, asset_object.precision);

    vesting_balances_result vesting_balances_results = new vesting_balances_result();
    vesting_balances_results.id = vestingBalancesObject.id;
    vesting_balances_results.return_cash = return_cash;
    vesting_balances_results.earned_coindays = String.valueOf(BigDecimal.valueOf(require_coindays * available_percent).setScale(0, BigDecimal.ROUND_HALF_EVEN));
    vesting_balances_results.require_coindays = String.valueOf(require_coindays);
    vesting_balances_results.remaining_days = String.valueOf(BigDecimal.valueOf(1 - available_percent).setScale(2, BigDecimal.ROUND_HALF_DOWN));
    vesting_balances_results.available_percent = String.valueOf(BigDecimal.valueOf(available_percent * 100).setScale(2, BigDecimal.ROUND_UP));
    vesting_balances_result.AvailableBalanceBean availableBalanceBean = new vesting_balances_result.AvailableBalanceBean();
    availableBalanceBean.amount = String.valueOf(vestingBalancesObject.balance.amount * available_percent / Math.pow(10, asset_object.precision));
    availableBalanceBean.asset_id = asset_object.id.toString();
    availableBalanceBean.symbol = asset_object.symbol;
    availableBalanceBean.precision = asset_object.precision;
    vesting_balances_results.available_balance = availableBalanceBean;
    return vesting_balances_results;
}
 
Example 20
Source File: JsonDeserializer.java    From anomaly-detection with Apache License 2.0 3 votes vote down vote up
/**
 * Search a double number inside a JSON string matching the input path
 * expression
 *
 * @param jsonString an encoded JSON string
 * @param paths      path fragments
 * @return the matching double number
 * @throws JsonPathNotFoundException if json path is invalid
 * @throws IOException               if the underlying input source has problems
 *                                   during parsing
 */
public static double getDoubleValue(String jsonString, String... paths) throws JsonPathNotFoundException, IOException {
    JsonElement jsonNode = getChildNode(jsonString, paths);
    if (jsonNode != null) {
        return jsonNode.getAsDouble();
    }
    throw new JsonPathNotFoundException();
}