Example usage for com.google.gson JsonPrimitive getAsBoolean

List of usage examples for com.google.gson JsonPrimitive getAsBoolean

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive getAsBoolean.

Prototype

@Override
public boolean getAsBoolean() 

Source Link

Document

convenience method to get this element as a boolean value.

Usage

From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java

License:Apache License

public Object unwrap(final Object o) {

    if (o == null) {
        return null;
    }// w ww .j  a  v  a  2 s  . c om

    if (!(o instanceof JsonElement)) {
        return o;
    }

    JsonElement e = (JsonElement) o;

    if (e.isJsonNull()) {
        return null;
    } else if (e.isJsonPrimitive()) {

        JsonPrimitive p = e.getAsJsonPrimitive();
        if (p.isString()) {
            return p.getAsString();
        } else if (p.isBoolean()) {
            return p.getAsBoolean();
        } else if (p.isNumber()) {
            return unwrapNumber(p.getAsNumber());
        }
    }

    return o;
}

From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java

License:Open Source License

public Object toPrimitiveObject(JsonElement element) {

    JsonPrimitive primitive = (JsonPrimitive) element;
    if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else if (primitive.isNumber()) {
        Number number = primitive.getAsNumber();
        double value = number.doubleValue();
        if (((int) value == value)) {
            return (int) value;
        } else {/*from ww  w .  jav  a 2s  .  c o m*/
            return (float) value;
        }
    } else if (primitive.isString()) {
        return primitive.getAsString();
    } else {
        throw new RuntimeException("Unrecognized JsonPrimitive: " + primitive);
    }
}

From source file:com.qmetry.qaf.automation.gson.GsonObjectDeserializer.java

License:Open Source License

public static Object read(JsonElement in) {

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<Object>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(read(anArr));//from w w  w. j  a  va 2s.c  o m
        }
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), read(entry.getValue()));
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().contains("."))
                return prim.getAsDouble();
            else {
                return prim.getAsLong();
            }
        }
    }
    return null;
}

From source file:com.razza.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 = DataModelHelper.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 = DataModelHelper.get(categoryMapping,
                                InputJsonKeys.ExtraSource.CategoryTagMapping.tag_name);
                        JsonPrimitive isDefault = (JsonPrimitive) DataModelHelper.get(categoryMapping,
                                InputJsonKeys.ExtraSource.CategoryTagMapping.is_default);
                        if (isDefault != null && isDefault.getAsBoolean()) {
                            mainCategory = category;
                        }//from  w  w w . j  ava 2s  .  co  m
                    }
                    DataModelHelper.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 = DataModelHelper.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());
                }

                DataModelHelper.set(tagName, dest, OutputJsonKeys.Tags.tag);
                DataModelHelper.set(name, dest, OutputJsonKeys.Tags.name);
                DataModelHelper.set(origin, InputJsonKeys.VendorAPISource.Categories.id, dest,
                        OutputJsonKeys.Tags.original_id);
                DataModelHelper.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) {
                        DataModelHelper.set(tagConf, InputJsonKeys.ExtraSource.TagConf.order_in_category, dest,
                                OutputJsonKeys.Tags.order_in_category);
                        DataModelHelper.set(tagConf, InputJsonKeys.ExtraSource.TagConf.color, dest,
                                OutputJsonKeys.Tags.color);
                        DataModelHelper.set(tagConf, InputJsonKeys.ExtraSource.TagConf.hashtag, dest,
                                OutputJsonKeys.Tags.hashtag);
                    }
                }

                categoryToTagMap.put(
                        DataModelHelper.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.razza.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private boolean isHiddenSession(JsonObject sessionObj) {
    JsonPrimitive hide = DataModelHelper.getMapValue(
            DataModelHelper.get(sessionObj, InputJsonKeys.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  2  s .  c om*/
    return false;
}

From source file:com.razza.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 ww .  j a v  a2  s  . c  o m
 * @return True if featured, false otherwise.
 */
private boolean isFeatured(JsonObject sessionObj) {
    // Extract "Featured Session" flag from EventPoint "info" block.
    JsonPrimitive featured = DataModelHelper.getMapValue(
            DataModelHelper.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;
}

From source file:com.ryanantkowiak.jOptionsHouseAPI.JsonUtilities.java

License:Open Source License

/**
 * Recursively print the contents of a GSON JSON element.
 * /*from   w w  w.j  a va2 s  . c om*/
 * @param element   the GSON JSON element to be printed
 * @param prefix   output will be prefixed with this string
 */
public static void printJson(JsonElement element, String prefix) {
    if (null == prefix || prefix.isEmpty()) {
        prefix = "";
    }

    if (null == element || element.isJsonNull()) {
        System.out.println(prefix + " [null]");
        return;
    }

    else if (element.isJsonPrimitive()) {
        JsonPrimitive p = element.getAsJsonPrimitive();
        if (p.isBoolean()) {
            System.out.println(prefix + " [bool=" + p.getAsBoolean() + "]");
        } else if (p.isString()) {
            System.out.println(prefix + " [string='" + p.getAsString() + "']");
        } else if (p.isNumber()) {
            System.out.println(prefix + " [number=" + p.getAsDouble() + "]");
        }
    }

    else if (element.isJsonArray()) {
        System.out.println(prefix + " [array]");
        for (int i = 0; i < element.getAsJsonArray().size(); ++i) {
            String newPrefix = prefix + "[" + i + "]";
            printJson(element.getAsJsonArray().get(i), newPrefix);
        }
    }

    else if (element.isJsonObject()) {
        JsonObject obj = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            String newPrefix = prefix + "." + key;
            printJson(value, newPrefix);
        }
    }

}

From source file:com.seleritycorp.common.base.config.ConfigUtils.java

License:Apache License

/**
 * Adds a JSON primitive to a Config.//from ww w .j a  v a2 s . c om
 * 
 * @param primitive The primitive to add
 * @param config The config instance to add the primitive to
 * @param key The key in the config space
 */
private static void loadJson(JsonPrimitive primitive, ConfigImpl config, String key) {
    String value = null;
    if (primitive.isBoolean()) {
        boolean bool = primitive.getAsBoolean();
        value = bool ? "true" : "false";
    } else if (primitive.isString()) {
        value = primitive.getAsString();
    } else if (primitive.isNumber()) {
        value = Double.toString(primitive.getAsDouble());
    }
    config.set(key, value);
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

License:Open Source License

private @CheckForNull Object toSimpleJavaType(JsonElement jsonValue) {
    if (jsonValue == null)
        return null; //VR
    Object value = null;// ww  w.  java2  s  . com
    if (!jsonValue.isJsonNull()) {
        if (jsonValue.isJsonPrimitive()) {
            JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
            if (primitive.isBoolean()) {
                value = Boolean.valueOf(primitive.getAsBoolean());
            } else if (primitive.isNumber()) {
                value = Double.valueOf(primitive.getAsDouble());
            } else if (primitive.isString()) {
                value = primitive.getAsString();
            } else {
                throw UnexpectedException.forUnexpectedCodeBranchExecuted();
            }
        } else if (jsonValue.isJsonArray()) {
            //This simply does not work (?) 
            JsonArray array = jsonValue.getAsJsonArray();
            Object[] result = new Object[array.size()];
            for (int i = 0; i < array.size(); i++) {
                result[i] = toSimpleJavaType(array.get(i));
            }
            value = result;
        } else if (jsonValue.isJsonObject()) {
            //This simply does not work (?)
            //value = getGson().fromJson(jsonValue, Map.class );

            JsonObject obj = jsonValue.getAsJsonObject();
            Iterator<Entry<String, JsonElement>> properties = obj.entrySet().iterator();
            Map<String, Object> result = new HashMap<String, Object>();
            while (properties.hasNext()) {
                Entry<String, JsonElement> property = properties.next();
                JsonElement propertyValue = property.getValue();
                result.put(property.getKey(), toSimpleJavaType(propertyValue));
            }
            value = result;
        } else {
            throw UnexpectedException.forUnexpectedCodeBranchExecuted();
        }
    }

    return value;
}

From source file:com.synflow.cx.internal.instantiation.properties.Specializer.java

License:Open Source License

/**
 * Returns an IR expression from the given JSON element.
 * /* w  w  w .  j a  v  a2s  . c o  m*/
 * @param json
 *            a JSON element (should be a primitive)
 * @return an expression, or <code>null</code>
 */
public Expression transformJson(JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return eINSTANCE.createExprBool(primitive.getAsBoolean());
        } else if (primitive.isNumber()) {
            return eINSTANCE.createExprInt(primitive.getAsBigInteger());
        } else if (primitive.isString()) {
            return eINSTANCE.createExprString(primitive.getAsString());
        }
    }
    return null;
}