List of usage examples for com.google.gson JsonElement getAsBoolean
public boolean getAsBoolean()
From source file:NetworkCustomizationExample.java
License:Open Source License
public static void main(String[] args) { try {//from w ww . j av a 2s .c o m // IMPORTANT NOTE: // This is an over-simplified code example. // In real products, please log the error and determine // whether a retry is needed to prevent duplicated API calls. APIRequest.changeRequestExecutor(new APIRequest.DefaultRequestExecutor() { public static final int MAX_RETRY = 3; @Override public String sendPost(String apiUrl, Map<String, Object> allParams, APIContext context) throws APIException, IOException { String response; int retry = 0; while (true) { try { response = super.sendPost(apiUrl, allParams, context); break; } catch (APIException e) { retry++; if (retry >= MAX_RETRY) throw e; if (e instanceof APIException.FailedRequestException && e.getMessage() != null) { JsonElement isTransient = e.getRawResponseAsJsonObject().get("is_transient"); if (isTransient != null && isTransient.getAsBoolean() == false) throw e; } try { Thread.sleep(3000); } catch (InterruptedException e1) { e1.printStackTrace(); } } catch (IOException e) { retry++; if (retry >= MAX_RETRY) throw e; try { Thread.sleep(3000); } catch (InterruptedException e1) { e1.printStackTrace(); } } } return response; } }); AdAccount account = new AdAccount(ACCOUNT_ID, context); Campaign campaign = account.createCampaign().setName("Java SDK Test Campaign") .setObjective(Campaign.EnumObjective.VALUE_LINK_CLICKS).setSpendCap(10000L) .setStatus(Campaign.EnumStatus.VALUE_PAUSED).execute(); System.out.println(campaign.fetch()); } catch (APIException e) { e.printStackTrace(); } }
From source file:augsburg.se.alltagsguide.common.Location.java
License:Open Source License
@NonNull public static Location fromJson(JsonObject jsonPage) { int id = jsonPage.get("id").getAsInt(); String name = jsonPage.get("name").getAsString(); String icon = jsonPage.get("icon").isJsonNull() ? null : jsonPage.get("icon").getAsString(); String path = jsonPage.get("path").getAsString(); String description = jsonPage.get("description").getAsString(); boolean global = false; if (jsonPage.has("global")) { global = !jsonPage.get("global").isJsonNull() && jsonPage.get("global").getAsBoolean(); }/*from w w w.j a v a 2 s . c o m*/ int color = jsonPage.get("color").isJsonNull() ? Color.parseColor("#00BCD4") : Color.parseColor(jsonPage.get("color").getAsString()); String cityImage = jsonPage.get("cover_image").isJsonNull() ? "" : jsonPage.get("cover_image").getAsString(); if (cityImage == null || "".equals(cityImage)) { cityImage = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a6/Brandenburger_Tor_abends.jpg/300px-Brandenburger_Tor_abends.jpg"; } float latitude = 0.0f; //TODO float longitude = 0.0f; //TODO boolean debug = false; if (jsonPage.has("live")) { JsonElement elem = jsonPage.get("live"); if (elem != null && !elem.isJsonNull()) { debug = elem.getAsBoolean(); } } return new Location(id, name, icon, path, description, global, color, cityImage, latitude, longitude, debug); }
From source file:augsburg.se.alltagsguide.common.Page.java
License:Open Source License
@NonNull public static Page fromJson(@NonNull final JsonObject jsonPage) { int id = jsonPage.get("id").getAsInt(); String title = jsonPage.get("title").getAsString(); String type = jsonPage.get("type").getAsString(); String status = jsonPage.get("status").getAsString(); long modified; try {/* w ww . java 2 s .com*/ modified = Helper.FROM_DATE_FORMAT.parse(jsonPage.get("modified_gmt").getAsString()).getTime(); } catch (ParseException e) { Ln.e(e); modified = -1; } String description = jsonPage.get("excerpt").getAsString(); String content = jsonPage.get("content").getAsString(); int parentId = jsonPage.get("parent").getAsInt(); int order = jsonPage.get("order").getAsInt(); String thumbnail = jsonPage.get("thumbnail").isJsonNull() ? "" : jsonPage.get("thumbnail").getAsString(); Author author = Author.fromJson(jsonPage.get("author").getAsJsonObject()); List<AvailableLanguage> languages = AvailableLanguage.fromJson(jsonPage.get("available_languages")); boolean autoTranslated = false; if (jsonPage.has("automatic_translation")) { JsonElement elem = jsonPage.get("automatic_translation"); if (elem != null && !elem.isJsonNull()) { autoTranslated = elem.getAsBoolean(); } } return new Page(id, title, type, status, modified, description, content, parentId, order, thumbnail, author, autoTranslated, languages); }
From source file:br.ufg.inf.es.saep.sandbox.dominio.infraestrutura.ValorDeserializer.java
License:Creative Commons License
@Override public Valor deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObject = jsonElement.getAsJsonObject(); byte tipo = jsonObject.get("tipo").getAsByte(); JsonElement valor = jsonObject.get("valor"); if (tipo == Valor.STRING) { return new Valor(valor.getAsString()); } else if (tipo == REAL) { return new Valor(valor.getAsFloat()); } else if (tipo == LOGICO) { return new Valor(valor.getAsBoolean()); } else if (tipo == DATA) { return Valor.dataFromString(valor.getAsString()); }/* w w w.j a v a 2 s .com*/ return null; }
From source file:brooklyn.event.feed.http.JsonFunctions.java
License:Apache License
@SuppressWarnings("unchecked") public static <T> Function<JsonElement, T> cast(final Class<T> expected) { return new Function<JsonElement, T>() { @Override/* w w w . ja va 2 s . c om*/ public T apply(JsonElement input) { if (input == null) { return (T) null; } else if (input.isJsonNull()) { return (T) null; } else if (expected == boolean.class || expected == Boolean.class) { return (T) (Boolean) input.getAsBoolean(); } else if (expected == char.class || expected == Character.class) { return (T) (Character) input.getAsCharacter(); } else if (expected == byte.class || expected == Byte.class) { return (T) (Byte) input.getAsByte(); } else if (expected == short.class || expected == Short.class) { return (T) (Short) input.getAsShort(); } else if (expected == int.class || expected == Integer.class) { return (T) (Integer) input.getAsInt(); } else if (expected == long.class || expected == Long.class) { return (T) (Long) input.getAsLong(); } else if (expected == float.class || expected == Float.class) { return (T) (Float) input.getAsFloat(); } else if (expected == double.class || expected == Double.class) { return (T) (Double) input.getAsDouble(); } else if (expected == BigDecimal.class) { return (T) input.getAsBigDecimal(); } else if (expected == BigInteger.class) { return (T) input.getAsBigInteger(); } else if (Number.class.isAssignableFrom(expected)) { // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above return (T) input.getAsNumber(); } else if (expected == String.class) { return (T) input.getAsString(); } else if (expected.isArray()) { JsonArray array = input.getAsJsonArray(); Class<?> componentType = expected.getComponentType(); if (JsonElement.class.isAssignableFrom(componentType)) { JsonElement[] result = new JsonElement[array.size()]; for (int i = 0; i < array.size(); i++) { result[i] = array.get(i); } return (T) result; } else { Object[] result = (Object[]) Array.newInstance(componentType, array.size()); for (int i = 0; i < array.size(); i++) { result[i] = cast(componentType).apply(array.get(i)); } return (T) result; } } else { throw new IllegalArgumentException("Cannot cast json element to type " + expected); } } }; }
From source file:ch.cern.db.flume.sink.kite.parser.JSONtoAvroParser.java
License:GNU General Public License
private Object getElementAsType(Schema schema, JsonElement element) { if (element == null || element.isJsonNull()) return null; switch (schema.getType()) { case BOOLEAN: return element.getAsBoolean(); case DOUBLE://from w w w . j av a 2 s. co m return element.getAsDouble(); case FLOAT: return element.getAsFloat(); case INT: return element.getAsInt(); case LONG: return element.getAsLong(); case NULL: return null; case UNION: return getElementAsType(schema.getTypes().get(0), element); // case FIXED: // case ARRAY: // case BYTES: // case ENUM: // case MAP: // case RECORD: // case STRING: default: return element.getAsString(); } }
From source file:ch.jamiete.hilda.configuration.Configuration.java
License:Apache License
public boolean getBoolean(final String name, final boolean def) { final JsonElement ele = this.json.get(name); if (ele == null) { return def; }// w ww . j ava 2s . c o m try { return ele.getAsBoolean(); } catch (final Exception e) { return def; } }
From source file:com.adr.datasql.orm.KindResultsJson.java
License:Apache License
@Override public Boolean getBoolean(String columnName) throws DataLinkException { JsonElement element = json.get(columnName); return element == null || element.isJsonNull() ? null : element.getAsBoolean(); }
From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java
License:Apache License
@Override public void booleanProperty(PropertyName name, JsonObject context) throws IOException { JsonElement prop = property(context, name); if (prop == null) { return;/*from w ww .j a v a 2s . c o m*/ } builder.add(name, prop.getAsBoolean()); }
From source file:com.b12kab.tmdblibrary.AccountStateDeserializer.java
License:Apache License
@Override public AccountState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { AccountState accountState = new AccountState(); String convertVariable = ""; JsonObject obj = (JsonObject) json;/*from w w w .ja va 2 s . c o m*/ JsonElement element; JsonElement ratingElement; JsonPrimitive rating; try { convertVariable = "id"; element = obj.get("id"); if (element != null) { if (element.isJsonPrimitive()) { if (element.getAsJsonPrimitive().isNumber()) { accountState.setId(element.getAsInt()); } } } convertVariable = "favorite"; element = obj.get("favorite"); if (element != null) { if (element.isJsonPrimitive()) { if (element.getAsJsonPrimitive().isBoolean()) { accountState.setFavorite(element.getAsBoolean()); } } } accountState.setRated(null); convertVariable = "rated"; element = obj.get("rated"); if (element != null) { if (element.isJsonObject()) { if (element.getAsJsonObject().has("value")) { ratingElement = element.getAsJsonObject().get("value"); if (ratingElement.isJsonPrimitive()) { // rating if (ratingElement.getAsJsonPrimitive().isNumber()) { accountState.setRated(ratingElement.getAsFloat()); } } } } } convertVariable = "watchlist"; element = obj.get("watchlist"); if (element != null) { if (element.isJsonPrimitive()) { if (element.getAsJsonPrimitive().isBoolean()) { accountState.setWatchlist(element.getAsBoolean()); } } } } catch (NullPointerException npe) { Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NullPointerException occurred" + npe.toString()); return null; } catch (NumberFormatException npe) { Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NumberFormatException occurred" + npe.toString()); return null; } catch (IllegalStateException ise) { Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, IllegalStateException occurred" + ise.toString()); } return accountState; }