List of usage examples for com.google.gson JsonPrimitive getAsBoolean
@Override public boolean getAsBoolean()
From source file:com.continusec.client.ObjectHash.java
License:Apache License
/** * Calculate the objecthash for a Gson JsonElement object, with a custom redaction prefix string. * @param o the Gson JsonElement to calculated the objecthash for. * @param r the string to use as a prefix to indicate that a string should be treated as a redacted subobject. * @return the objecthash for this object * @throws ContinusecException upon error *//* w w w.j a v a 2 s . co m*/ public static final byte[] objectHashWithRedaction(JsonElement o, String r) throws ContinusecException { if (o == null || o.isJsonNull()) { return hashNull(); } else if (o.isJsonArray()) { return hashArray(o.getAsJsonArray(), r); } else if (o.isJsonObject()) { return hashObject(o.getAsJsonObject(), r); } else if (o.isJsonPrimitive()) { JsonPrimitive p = o.getAsJsonPrimitive(); if (p.isBoolean()) { return hashBoolean(p.getAsBoolean()); } else if (p.isNumber()) { return hashDouble(p.getAsDouble()); } else if (p.isString()) { return hashString(p.getAsString(), r); } else { throw new InvalidObjectException(); } } else { throw new InvalidObjectException(); } }
From source file:com.facebook.buck.json.RawParser.java
License:Apache License
/** * @return One of: String, Boolean, Long, Number, List<Object>, Map<String, Object>. */// www . j a v a2 s. co m @Nullable @VisibleForTesting static Object toRawTypes(JsonElement json) { // Cases are ordered from most common to least common. if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { return interner.intern(primitive.getAsString()); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { Number number = primitive.getAsNumber(); // Number is likely an instance of class com.google.gson.internal.LazilyParsedNumber. if (number.longValue() == number.doubleValue()) { return number.longValue(); } else { return number; } } else { throw new IllegalStateException("Unknown primitive type: " + primitive); } } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); List<Object> out = Lists.newArrayListWithCapacity(array.size()); for (JsonElement item : array) { out.add(toRawTypes(item)); } return out; } else if (json.isJsonObject()) { Map<String, Object> out = new LinkedHashMap<>(json.getAsJsonObject().entrySet().size()); for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { // On a large project, without invoking intern(), we have seen `buck targets` OOM. When this // happened, according to the .hprof file generated using -XX:+HeapDumpOnOutOfMemoryError, // 39.6% of the memory was spent on char[] objects while 14.5% was spent on Strings. // (Another 10.5% was spent on java.util.HashMap$Entry.) Introducing intern() stopped the // OOM from happening. out.put(interner.intern(entry.getKey()), toRawTypes(entry.getValue())); } return out; } else if (json.isJsonNull()) { return null; } else { throw new IllegalStateException("Unknown type: " + json); } }
From source file:com.flipkart.polyguice.config.JsonConfiguration.java
License:Apache License
private void flatten(String prefix, JsonElement element) { if (element.isJsonPrimitive()) { JsonPrimitive jsonPrim = element.getAsJsonPrimitive(); if (jsonPrim.isBoolean()) { configTab.put(prefix, jsonPrim.getAsBoolean()); } else if (jsonPrim.isNumber()) { configTab.put(prefix, jsonPrim.getAsNumber()); } else if (jsonPrim.isString()) { configTab.put(prefix, jsonPrim.getAsString()); }/*from ww w . jav a 2s. c o m*/ } else if (element.isJsonObject()) { JsonObject jsonObj = element.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) { String prefix1 = ((prefix != null) ? prefix + "." : "") + entry.getKey(); flatten(prefix1, entry.getValue()); } } }
From source file:com.frontier45.flume.sink.elasticsearch2.ElasticSearchLogStashEventSerializer.java
License:Apache License
private Object getValue(JsonElement obj) { JsonPrimitive jp = obj.getAsJsonPrimitive(); if (jp.isBoolean()) { return jp.getAsBoolean(); } else if (jp.isNumber()) { String value = jp.getAsString(); if (value.indexOf(".") > 0) { return jp.getAsDouble(); } else {//from w w w . ja v a2 s .com return jp.getAsLong(); } } else if (jp.isString()) { return jp.getAsString(); } return obj; }
From source file:com.getperka.flatpack.codexes.DynamicCodex.java
License:Apache License
/** * Attempt to infer the type from the JsonElement presented. * <ul>/*w w w . ja va 2 s . co m*/ * <li>boolean -> {@link Boolean} * <li>number -> {@link BigDecimal} * <li>string -> {@link HasUuid} or {@link String} * <li>array -> {@link ListCodex} * <li>object -> {@link StringMapCodex} * </ul> */ @Override public Object readNotNull(JsonElement element, DeserializationContext context) throws Exception { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { // Always return numbers as BigDecimals for consistency return primitive.getAsBigDecimal(); } else { String value = primitive.getAsString(); // Interpret UUIDs as entity references if (UUID_PATTERN.matcher(value).matches()) { UUID uuid = UUID.fromString(value); HasUuid entity = context.getEntity(uuid); if (entity != null) { return entity; } } return value; } } else if (element.isJsonArray()) { return listCodex.get().readNotNull(element, context); } else if (element.isJsonObject()) { return mapCodex.get().readNotNull(element, context); } context.fail(new UnsupportedOperationException("Cannot infer data type for " + element.toString())); return null; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
public JsonArray extractTags(JsonDataSources sources) { JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(VendorAPISource.MainTypes.categories.name()); JsonDataSource tagCategoryMappingSource = sources .getSource(ExtraSource.MainTypes.tag_category_mapping.name()); JsonDataSource tagsConfSource = sources.getSource(ExtraSource.MainTypes.tag_conf.name()); categoryToTagMap = new HashMap<String, JsonObject>(); // Only for checking duplicates. HashSet<String> originalTagNames = new HashSet<String>(); if (source != null) { for (JsonObject origin : source) { JsonObject dest = new JsonObject(); // set tag category, looking for parentid in the tag_category_mapping data source JsonElement parentId = get(origin, VendorAPISource.Categories.parentid); // Ignore categories with null parents, because they are roots (tag categories). if (parentId != null && !parentId.getAsString().equals("")) { JsonElement category = null; if (tagCategoryMappingSource != null) { JsonObject categoryMapping = tagCategoryMappingSource .getElementById(parentId.getAsString()); if (categoryMapping != null) { category = get(categoryMapping, ExtraSource.CategoryTagMapping.tag_name); JsonPrimitive isDefault = (JsonPrimitive) get(categoryMapping, ExtraSource.CategoryTagMapping.is_default); if (isDefault != null && isDefault.getAsBoolean()) { mainCategory = category; }/*from ww w . j a va 2s . c om*/ } set(category, dest, OutputJsonKeys.Tags.category); } // Ignore categories unrecognized parents (no category) if (category == null) { continue; } // Tag name is by convention: "TAGCATEGORY_TAGNAME" JsonElement name = get(origin, VendorAPISource.Categories.name); JsonElement tagName = new JsonPrimitive( category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString()); JsonElement originalTagName = tagName; set(tagName, dest, OutputJsonKeys.Tags.tag); set(name, dest, OutputJsonKeys.Tags.name); set(origin, VendorAPISource.Categories.id, dest, OutputJsonKeys.Tags.original_id); set(origin, VendorAPISource.Categories.description, dest, OutputJsonKeys.Tags._abstract, null); if (tagsConfSource != null) { JsonObject tagConf = tagsConfSource.getElementById(originalTagName.getAsString()); if (tagConf != null) { set(tagConf, ExtraSource.TagConf.order_in_category, dest, OutputJsonKeys.Tags.order_in_category); set(tagConf, ExtraSource.TagConf.color, dest, OutputJsonKeys.Tags.color); set(tagConf, ExtraSource.TagConf.hashtag, dest, OutputJsonKeys.Tags.hashtag); } } categoryToTagMap.put(get(origin, VendorAPISource.Categories.id).getAsString(), dest); if (originalTagNames.add(originalTagName.getAsString())) { result.add(dest); } } } } return result; }
From source file:com.google.iosched.model.DataExtractor.java
License:Open Source License
private boolean isHiddenSession(JsonObject sessionObj) { JsonPrimitive hide = getMapValue(get(sessionObj, VendorAPISource.Topics.info), InputJsonKeys.VendorAPISource.Topics.INFO_HIDDEN_SESSION, Converters.BOOLEAN, null); if (hide != null && hide.isBoolean() && hide.getAsBoolean()) { return true; }//from w ww. j a v a 2s .c o m return false; }
From source file:com.google.samples.apps.iosched.server.schedule.model.DataExtractor.java
License:Open Source License
public JsonArray extractTags(JsonDataSources sources) { JsonArray result = new JsonArray(); JsonDataSource source = sources.getSource(InputJsonKeys.VendorAPISource.MainTypes.categories.name()); JsonDataSource tagCategoryMappingSource = sources .getSource(InputJsonKeys.ExtraSource.MainTypes.tag_category_mapping.name()); JsonDataSource tagsConfSource = sources.getSource(InputJsonKeys.ExtraSource.MainTypes.tag_conf.name()); categoryToTagMap = new HashMap<String, JsonObject>(); // Only for checking duplicates. HashSet<String> originalTagNames = new HashSet<String>(); if (source != null) { for (JsonObject origin : source) { JsonObject dest = new JsonObject(); // set tag category, looking for parentid in the tag_category_mapping data source JsonElement parentId = get(origin, InputJsonKeys.VendorAPISource.Categories.parentid); // Ignore categories with null parents, because they are roots (tag categories). if (parentId != null && !parentId.getAsString().equals("")) { JsonElement category = null; if (tagCategoryMappingSource != null) { JsonObject categoryMapping = tagCategoryMappingSource .getElementById(parentId.getAsString()); if (categoryMapping != null) { category = get(categoryMapping, InputJsonKeys.ExtraSource.CategoryTagMapping.tag_name); JsonPrimitive isDefault = (JsonPrimitive) get(categoryMapping, InputJsonKeys.ExtraSource.CategoryTagMapping.is_default); if (isDefault != null && isDefault.getAsBoolean()) { mainCategory = category; }//from w ww. j a v a 2s . c om } set(category, dest, OutputJsonKeys.Tags.category); } // Ignore categories unrecognized parents (no category) if (category == null) { continue; } // Tag name is by convention: "TAGCATEGORY_TAGNAME" JsonElement name = get(origin, InputJsonKeys.VendorAPISource.Categories.name); JsonElement tagName = new JsonPrimitive( category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString()); JsonElement originalTagName = tagName; if (obfuscate) { name = Converters.OBFUSCATE.convert(name); tagName = new JsonPrimitive( category.getAsString() + "_" + Converters.TAG_NAME.convert(name).getAsString()); } set(tagName, dest, OutputJsonKeys.Tags.tag); set(name, dest, OutputJsonKeys.Tags.name); set(origin, InputJsonKeys.VendorAPISource.Categories.id, dest, OutputJsonKeys.Tags.original_id); set(origin, InputJsonKeys.VendorAPISource.Categories.description, dest, OutputJsonKeys.Tags._abstract, obfuscate ? Converters.OBFUSCATE : null); if (tagsConfSource != null) { JsonObject tagConf = tagsConfSource.getElementById(originalTagName.getAsString()); if (tagConf != null) { set(tagConf, InputJsonKeys.ExtraSource.TagConf.order_in_category, dest, OutputJsonKeys.Tags.order_in_category); set(tagConf, InputJsonKeys.ExtraSource.TagConf.color, dest, OutputJsonKeys.Tags.color); set(tagConf, InputJsonKeys.ExtraSource.TagConf.hashtag, dest, OutputJsonKeys.Tags.hashtag); } } categoryToTagMap.put(get(origin, InputJsonKeys.VendorAPISource.Categories.id).getAsString(), dest); if (originalTagNames.add(originalTagName.getAsString())) { result.add(dest); } } } } if (Config.DEBUG_FIX_DATA) { DebugDataExtractorHelper.changeCategories(categoryToTagMap, result); } return result; }
From source file:com.google.samples.apps.iosched.server.schedule.model.DataExtractor.java
License:Open Source License
private boolean isHiddenSession(JsonObject sessionObj) { JsonPrimitive hide = getMapValue(get(sessionObj, InputJsonKeys.VendorAPISource.Topics.info), InputJsonKeys.VendorAPISource.Topics.INFO_HIDDEN_SESSION, Converters.BOOLEAN, null); if (hide != null && hide.isBoolean() && hide.getAsBoolean()) { return true; }/* ww w . j a va 2 s . c o m*/ return false; }
From source file:com.google.samples.apps.iosched.server.schedule.model.DataExtractor.java
License:Open Source License
/** Check whether a session is featured or not. * * @param sessionObj Session to check/* w w w. j ava2 s . c om*/ * @return True if featured, false otherwise. */ private boolean isFeatured(JsonObject sessionObj) { // Extract "Featured Session" flag from EventPoint "info" block. JsonPrimitive featured = getMapValue(get(sessionObj, InputJsonKeys.VendorAPISource.Topics.info), InputJsonKeys.VendorAPISource.Topics.INFO_FEATURED_SESSION, Converters.BOOLEAN, null); if (featured != null && featured.isBoolean() && featured.getAsBoolean()) { return true; } return false; }