List of usage examples for com.google.gson JsonPrimitive getAsInt
@Override public int getAsInt()
From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java
License:Apache License
@Override public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) { assertValidConversion(src, srcType, targetType); if (src == null || src.getClass().equals(JsonNull.class)) { return null; }/* w w w. j a v a2s. c om*/ if (JsonPrimitive.class.isAssignableFrom(srcType)) { JsonPrimitive primitive = (JsonPrimitive) src; if (targetType.equals(Long.class)) { return primitive.getAsLong(); } else if (targetType.equals(Integer.class)) { return primitive.getAsInt(); } else if (targetType.equals(BigInteger.class)) { return primitive.getAsBigInteger(); } else if (targetType.equals(Byte.class)) { return primitive.getAsByte(); } else if (targetType.equals(BigDecimal.class)) { return primitive.getAsBigDecimal(); } else if (targetType.equals(Double.class)) { return primitive.getAsDouble(); } else if (targetType.equals(Float.class)) { return primitive.getAsFloat(); } else if (targetType.equals(String.class)) { return primitive.getAsString(); } else if (targetType.equals(Boolean.class)) { return primitive.getAsBoolean(); } else if (targetType.equals(Date.class)) { if (primitive.isNumber()) { return new Date(primitive.getAsLong()); } else if (primitive.isString()) { try { return DateFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { throw new MappingException(e); } } } } else if (JsonObject.class.isAssignableFrom(srcType)) { JsonObject srcObject = (JsonObject) src; if (targetType.equals(Map.class)) { Map<String, Object> targetMap = new LinkedHashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) { Object val = null; JsonElement element = entry.getValue(); if (element.isJsonPrimitive()) { val = GsonJsonProvider.unwrap(element); } else if (element.isJsonArray()) { val = convert(element, element.getClass(), List.class, conf); } else if (element.isJsonObject()) { val = convert(element, element.getClass(), Map.class, conf); } else if (element.isJsonNull()) { val = null; } targetMap.put(entry.getKey(), val); } return targetMap; } } else if (JsonArray.class.isAssignableFrom(srcType)) { JsonArray srcArray = (JsonArray) src; if (targetType.equals(List.class)) { List<Object> targetList = new ArrayList<Object>(); for (JsonElement element : srcArray) { if (element.isJsonPrimitive()) { targetList.add(GsonJsonProvider.unwrap(element)); } else if (element.isJsonArray()) { targetList.add(convert(element, element.getClass(), List.class, conf)); } else if (element.isJsonObject()) { targetList.add(convert(element, element.getClass(), Map.class, conf)); } else if (element.isJsonNull()) { targetList.add(null); } } return targetList; } } throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName()); }
From source file:com.softwarementors.extjs.djn.test.config.GsonBuilderConfiguratorForTesting.java
License:Open Source License
private static int getIntValue(JsonObject parent, String elementName) { assert parent != null; assert !StringUtils.isEmpty(elementName); JsonElement element = parent.get(elementName); if (!element.isJsonPrimitive()) { throw new JsonParseException("Element + '" + elementName + "' must be a valid integer"); }// w ww . j av a2s . co m JsonPrimitive primitiveElement = (JsonPrimitive) element; if (!primitiveElement.isNumber()) { throw new JsonParseException("Element + '" + elementName + "' must be a valid integer"); } return primitiveElement.getAsInt(); }
From source file:com.stackmob.sdk.push.StackMobPushTokenDeserializer.java
License:Apache License
public StackMobPushToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { JsonObject obj = json.getAsJsonObject(); JsonPrimitive tokenStringPrimitive = obj.getAsJsonPrimitive("token"); String tokenString = tokenStringPrimitive.getAsString(); JsonPrimitive tokenTypePrimitive = obj.getAsJsonPrimitive("type"); StackMobPushToken.TokenType tokenType = StackMobPushToken.TokenType .valueOf(tokenTypePrimitive.getAsString()); JsonPrimitive registeredMSPrimitive = obj.getAsJsonPrimitive("registered_milliseconds"); long registeredMS = registeredMSPrimitive.getAsInt(); return new StackMobPushToken(tokenString, tokenType, registeredMS); }
From source file:com.yandex.money.api.typeadapters.JsonUtils.java
License:Open Source License
/** * Gets nullable Integer from a JSON object. * * @param object json object// w w w . j a v a 2 s .c o m * @param memberName member's name * @return {@link Integer} value */ public static Integer getInt(JsonObject object, String memberName) { JsonPrimitive primitive = getPrimitiveChecked(object, memberName); return primitive == null ? null : primitive.getAsInt(); }
From source file:Days.Day12.java
public int parsePrimitive(JsonPrimitive primitive) { if (primitive.isNumber()) { return primitive.getAsInt(); }/* w ww.j a v a 2s .co m*/ return 0; }
From source file:de.azapps.mirakel.model.task.TaskDeserializer.java
License:Open Source License
private static void handleAdditionalEntries(final Task t, final String key, final JsonElement val) { if (val.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) val; if (p.isBoolean()) { t.addAdditionalEntry(key, String.valueOf(val.getAsBoolean())); } else if (p.isNumber()) { t.addAdditionalEntry(key, String.valueOf(val.getAsInt())); } else if (p.isJsonNull()) { t.addAdditionalEntry(key, "null"); } else if (p.isString()) { t.addAdditionalEntry(key, '"' + val.getAsString() + '"'); } else {//from w ww . j a v a2 s.co m Log.w(TAG, "unknown json-type"); } } else if (val.isJsonArray()) { final JsonArray a = (JsonArray) val; StringBuilder s = new StringBuilder("["); boolean first = true; for (final JsonElement e : a) { if (e.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) e; final String add; if (p.isBoolean()) { add = String.valueOf(p.getAsBoolean()); } else if (p.isNumber()) { add = String.valueOf(p.getAsInt()); } else if (p.isString()) { add = '"' + p.getAsString() + '"'; } else if (p.isJsonNull()) { add = "null"; } else { Log.w(TAG, "unknown json-type"); break; } s.append(first ? "" : ",").append(add); first = false; } else { Log.w(TAG, "unknown json-type"); } } t.addAdditionalEntry(key, s + "]"); } else { Log.w(TAG, "unknown json-type"); } }
From source file:fr.zcraft.MultipleInventories.snaphots.PlayerSnapshot.java
License:Open Source License
private static PotionEffect potionEffectFromJSON(final JsonObject json) { final JsonPrimitive color = json.get("color").isJsonNull() ? null : json.getAsJsonPrimitive("color"); return new PotionEffect(PotionEffectType.getByName(json.getAsJsonPrimitive("type").getAsString()), isNull(json, "duration") ? 1 : json.getAsJsonPrimitive("duration").getAsInt(), isNull(json, "amplifier") ? 1 : json.getAsJsonPrimitive("amplifier").getAsInt(), !isNull(json, "ambient") && json.getAsJsonPrimitive("ambient").getAsBoolean(), isNull(json, "has-particles") || json.getAsJsonPrimitive("has-particles").getAsBoolean(), color != null && !color.isJsonNull() ? Color.fromRGB(color.getAsInt()) : null); }
From source file:io.flutter.run.daemon.DaemonApi.java
License:Open Source License
/** * Parses some JSON and handles it as either a command's response or an event. *//*from ww w . ja v a 2 s. c om*/ void dispatch(@NotNull String json, @Nullable DaemonEvent.Listener listener) { final JsonObject obj; try { final JsonParser jp = new JsonParser(); final JsonElement elem = jp.parse(json); obj = elem.getAsJsonObject(); } catch (JsonSyntaxException e) { LOG.error("Unable to parse response from Flutter daemon", e); return; } final JsonPrimitive primId = obj.getAsJsonPrimitive("id"); if (primId == null) { // It's an event. if (listener != null) { DaemonEvent.dispatch(obj, listener); } else { LOG.info("ignored event from Flutter daemon: " + json); } return; } final int id; try { id = primId.getAsInt(); } catch (NumberFormatException e) { LOG.error("Unable to parse response from Flutter daemon", e); return; } final JsonElement error = obj.get("error"); if (error != null) { LOG.warn("Flutter process returned an error: " + json); final Command cmd = takePending(id); if (cmd != null) { cmd.completeExceptionally(new IOException("unexpected response: " + json)); } } final JsonElement result = obj.get("result"); complete(id, result); }
From source file:io.flutter.run.daemon.FlutterAppManager.java
License:Open Source License
void processInput(@NotNull String string, @NotNull FlutterDaemonController controller) { try {// w w w . j a v a2s . c om JsonParser jp = new JsonParser(); JsonElement elem = jp.parse(string); JsonObject obj = elem.getAsJsonObject(); JsonPrimitive primId = obj.getAsJsonPrimitive("id"); if (primId == null) { handleEvent(obj, controller, string); } else { handleResponse(primId.getAsInt(), obj, controller); } } catch (JsonSyntaxException ex) { myLogger.error(ex); } }
From source file:io.kodokojo.commons.utils.servicelocator.marathon.MarathonServiceLocator.java
License:Open Source License
private static Set<Service> convertToService(String name, JsonObject json) { Set<Service> res = new HashSet<>(); JsonObject app = json.getAsJsonObject("app"); JsonObject container = app.getAsJsonObject("container"); String containerType = container.getAsJsonPrimitive("type").getAsString(); if ("DOCKER".equals(containerType)) { List<String> ports = new ArrayList<>(); JsonObject docker = container.getAsJsonObject("docker"); JsonArray portMappings = docker.getAsJsonArray("portMappings"); for (int i = 0; i < portMappings.size(); i++) { JsonObject portMapping = (JsonObject) portMappings.get(i); ports.add(portMapping.getAsJsonPrimitive("containerPort").getAsString()); }/*from w w w . java2 s . c om*/ JsonArray tasks = app.getAsJsonArray("tasks"); for (int i = 0; i < tasks.size(); i++) { JsonObject task = (JsonObject) tasks.get(i); String host = task.getAsJsonPrimitive("host").getAsString(); boolean alive = false; if (task.has("healthCheckResults")) { JsonArray healthCheckResults = task.getAsJsonArray("healthCheckResults"); for (int j = 0; j < healthCheckResults.size() && !alive; j++) { JsonObject healthCheck = (JsonObject) healthCheckResults.get(j); alive = healthCheck.getAsJsonPrimitive("alive").getAsBoolean(); } } if (alive) { JsonArray jsonPorts = task.getAsJsonArray("ports"); for (int j = 0; j < jsonPorts.size(); j++) { JsonPrimitive jsonPort = (JsonPrimitive) jsonPorts.get(j); String portName = ports.get(j); res.add(new Service(name + "-" + portName, host, jsonPort.getAsInt())); } } } } return res; }