Example usage for com.google.gson JsonPrimitive isBoolean

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

Introduction

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

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains 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;
    }//from   w w  w.  ja  va2s.  com

    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 {//w ww  . j  a  v a2 s .  c  om
            return (float) value;
        }
    } else if (primitive.isString()) {
        return primitive.getAsString();
    } else {
        throw new RuntimeException("Unrecognized JsonPrimitive: " + primitive);
    }
}

From source file:com.make.json2java.ClassField.java

License:Apache License

private boolean isBooleanValue(JsonPrimitive primitive) {
    boolean isBoolean = primitive.isBoolean();
    if (!isBoolean && primitive.isString()) {
        String value = primitive.getAsString();
        isBoolean = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
    }//from   w  w w  . j  a v  a2  s . c om
    return isBoolean;
}

From source file:com.nextdoor.bender.operation.json.key.KeyNameOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {

        JsonElement val = entry.getValue();
        obj.remove(entry.getKey());/*from  w  ww . ja v a  2  s  .  c om*/
        String key = entry.getKey().toLowerCase().replaceAll("[ .]", "_");

        if (val.isJsonPrimitive()) {
            JsonPrimitive prim = val.getAsJsonPrimitive();

            if (prim.isBoolean()) {
                obj.add(key + "__bool", val);
            } else if (prim.isNumber()) {
                if (prim.toString().contains(".")) {
                    obj.add(key + "__float", val);
                } else {
                    obj.add(key + "__long", val);
                }
            } else if (prim.isString()) {
                obj.add(key + "__str", val);
            }
        } else if (val.isJsonObject()) {
            obj.add(key, val);
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            obj.add(key + "__arr", val);
        }
    }
}

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));/*ww 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

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  ww  w . j  a  v  a  2 s .  com
    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/*ww  w .jav a 2s .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.
 * /*  w  ww .  j  a v  a2  s. c  o  m*/
 * @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 w  w w . j  a  v a  2  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;//from w  ww  . j a va 2s .  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;
}