List of usage examples for com.google.gson JsonNull INSTANCE
JsonNull INSTANCE
To view the source code for com.google.gson JsonNull INSTANCE.
Click Source Link
From source file:com.money.manager.ex.investment.prices.FixerService.java
License:Open Source License
private SecurityPriceModel getSecurityPriceFor(Map.Entry<String, JsonElement> quote, String dateString) { SecurityPriceModel priceModel = new SecurityPriceModel(); priceModel.symbol = quote.getKey();/*from w w w . ja v a2 s .co m*/ UIHelper ui = new UIHelper(getContext()); // Price JsonElement priceElement = quote.getValue(); if (priceElement == JsonNull.INSTANCE) { ui.showToast( getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol); return null; } String priceString = priceElement.getAsString(); if (!NumericHelper.isNumeric(priceString)) { ui.showToast( getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol); return null; } Money retrievedPrice = MoneyFactory.fromString(priceString); // invert the price. Round to 10 decimals. BigDecimal invertedPrice = BigDecimal.ONE.divide(retrievedPrice.toBigDecimal(), 10, RoundingMode.HALF_EVEN); priceModel.price = MoneyFactory.fromBigDecimal(invertedPrice); // Date Date date = new MmxDate(dateString).toDate(); priceModel.date = date; return priceModel; }
From source file:com.money.manager.ex.investment.yql.YqlSecurityPriceUpdaterRetrofit.java
License:Open Source License
private SecurityPriceModel getSecurityPriceFor(JsonObject quote) { SecurityPriceModel priceModel = new SecurityPriceModel(); priceModel.symbol = quote.get("symbol").getAsString(); ExceptionHandler handler = new ExceptionHandler(getContext(), this); // Price/*ww w . ja va 2 s. co m*/ JsonElement priceElement = quote.get("LastTradePriceOnly"); if (priceElement == JsonNull.INSTANCE) { handler.showMessage( getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol); return null; } String priceString = priceElement.getAsString(); if (!NumericHelper.isNumeric(priceString)) { handler.showMessage( getContext().getString(R.string.error_no_price_found_for_symbol) + " " + priceModel.symbol); return null; } priceModel.price = readPrice(priceString, quote); // Date Date date = new MmxDate().toDate(); JsonElement dateElement = quote.get("LastTradeDate"); if (dateElement != JsonNull.INSTANCE) { // Sometimes the date is not available. For now we will use today's date. date = new MmxDate(dateElement.getAsString(), "MM/dd/yyyy").toDate(); } priceModel.date = date; return priceModel; }
From source file:com.relayrides.pushy.apns.DateAsMillisecondsSinceEpochTypeAdapter.java
License:Open Source License
@Override public JsonElement serialize(final Date src, final Type typeOfSrc, final JsonSerializationContext context) { final JsonElement element; if (src != null) { element = new JsonPrimitive(src.getTime()); } else {//from w ww. jav a2 s . c o m element = JsonNull.INSTANCE; } return element; }
From source file:com.relayrides.pushy.apns.DateAsSecondsSinceEpochTypeAdapter.java
License:Open Source License
@Override public JsonElement serialize(final Date src, final Type typeOfSrc, final JsonSerializationContext context) { final JsonElement element; if (src != null) { element = new JsonPrimitive(src.getTime() / 1000); } else {/*from w w w .j a v a 2 s . c o m*/ element = JsonNull.INSTANCE; } return element; }
From source file:com.relayrides.pushy.apns.DateAsTimeSinceEpochTypeAdapter.java
License:Open Source License
@Override public JsonElement serialize(final Date src, final Type typeOfSrc, final JsonSerializationContext context) { final JsonElement element; if (src != null) { element = new JsonPrimitive(this.timeUnit.convert(src.getTime(), TimeUnit.MILLISECONDS)); } else {/* w ww . ja v a 2 s .co m*/ element = JsonNull.INSTANCE; } return element; }
From source file:com.sixt.service.framework.jetty.JsonHandler.java
License:Apache License
@SuppressWarnings("unchecked") private JsonRpcResponse dispatchJsonRpcRequest(JsonRpcRequest rpcRequest, OrangeContext cxt) { ServiceMethodHandler handler = handlers.getMethodHandler(rpcRequest.getMethod()); Message innerRequest = convertJsonToProtobuf(handler, rpcRequest); JsonRpcResponse jsonResponse = new JsonRpcResponse(rpcRequest.getId(), JsonNull.INSTANCE, JsonNull.INSTANCE, HttpServletResponse.SC_OK);/*ww w . j a v a 2 s. co m*/ JsonElement idElement = rpcRequest.getId(); if (idElement == null) { jsonResponse.setId(new JsonPrimitive(-1)); } try { Message innerResponse = invokeHandlerChain(rpcRequest.getMethod(), handler, innerRequest, cxt); jsonResponse.setResult(ProtobufUtil.protobufToJson(innerResponse)); } catch (RpcCallException rpcEx) { logger.warn("Error processing request", rpcEx); jsonResponse.setError(rpcEx.toJson()); jsonResponse.setStatusCode(rpcEx.getCategory().getHttpStatus()); } catch (Exception ex) { logger.warn("Error processing request", ex); jsonResponse.setError(new JsonPrimitive(ex.getMessage())); jsonResponse.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return jsonResponse; }
From source file:com.sixt.service.framework.json.JsonUtil.java
License:Apache License
public static JsonRpcResponse parseJsonRpcResponse(String rawResponse) { JsonParser parser = new JsonParser(); JsonObject response = parser.parse(rawResponse).getAsJsonObject(); JsonElement id = response.get("id"); JsonElement errorElement = response.get("error"); int responseStatus = HttpServletResponse.SC_OK; String error;/*from w ww . j av a 2s . c om*/ if (!(errorElement instanceof JsonNull)) { if (errorElement instanceof JsonObject) { error = errorElement.toString(); // try parsing it into RpcCallException to get the HttpStatus from there RpcCallException rpcEx = RpcCallException.fromJson(error); if (rpcEx != null) { responseStatus = rpcEx.getCategory().getHttpStatus(); JsonElement resultElement = response.get("result"); return new JsonRpcResponse(id, resultElement == null ? JsonNull.INSTANCE : resultElement, errorElement, responseStatus); } } error = errorElement.getAsString(); if (StringUtils.isNotBlank(error)) { responseStatus = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } } JsonElement resultElement = response.get("result"); return new JsonRpcResponse(id, resultElement == null ? JsonNull.INSTANCE : resultElement, errorElement, responseStatus); }
From source file:com.synflow.cx.internal.instantiation.properties.JsonMaker.java
License:Open Source License
@Override public JsonNull caseNull(Null null_) { return JsonNull.INSTANCE; }
From source file:com.talvish.tales.serialization.json.translators.ArrayToJsonArrayTranslator.java
License:Apache License
/** * Translates the received object into a json array. * If the object is of the wrong type, a TranslationException will occur. *//*from www .ja v a 2s . com*/ @Override public Object translate(Object anObject) { Object returnValue; if (anObject == null) { returnValue = JsonNull.INSTANCE; } else { try { if (anObject.getClass().isArray()) { int arrayLength = Array.getLength(anObject); if (arrayLength != 1 || !writeSingle) { JsonArray jsonArray = new JsonArray(); for (int count = 0; count < arrayLength; count += 1) { jsonArray.add((JsonElement) elementTranslator.translate(Array.get(anObject, count))); } returnValue = jsonArray; } else { // this happens we are allowing the single item arrays to be written out as a non-array returnValue = (JsonElement) elementTranslator.translate(Array.get(anObject, 0)); } } else { throw new TranslationException(String.format( "Received a '%s' instead of an array, so unable to translate into a json array.", anObject.getClass().getName())); } } catch (ClassCastException e) { throw new TranslationException(e); } } return returnValue; }
From source file:com.talvish.tales.serialization.json.translators.BooleanToJsonPrimitiveTranslator.java
License:Apache License
/** * Translates the received object into a json primitive. * If the object is of the wrong type, a TranslationException will occur. *///from w w w . j a v a2 s .co m @Override public Object translate(Object anObject) { try { if (anObject == null) { return JsonNull.INSTANCE; } else { return new JsonPrimitive((Boolean) anObject); } } catch (ClassCastException e) { throw new TranslationException(e); } }