Example usage for com.google.gson JsonElement getAsBoolean

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

Introduction

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

Prototype

public boolean getAsBoolean() 

Source Link

Document

convenience method to get this element as a boolean value.

Usage

From source file:JSONformating.reader.JSONReader.java

public static JValue getJValue(JsonObject cell) throws IOException {
    JSONFormatType type = JSONFormatType.getType(cell.get("type").getAsString());
    JsonElement value = cell.get("value");
    switch (type) {
    case BOOLEAN:
        JBooleanValue boolValue = new JBooleanValue();
        boolValue.setValue(value.getAsBoolean());
        return boolValue;
    case DATE://from ww w.j  a  v  a2  s  .co  m
    case IMAGE:
    case STRING:
    case URL:
    case VERSION:
        JStringValue stringValue = new JStringValue();
        stringValue.setValue(value.getAsString());
        return stringValue;
    case INTEGER:
    case REAL:
        JNumberValue numValue = new JNumberValue();
        numValue.setValue(value.getAsDouble());
        return numValue;
    case MULTIPLE:
        JMultipleValue mulValue = new JMultipleValue();
        JsonArray array = value.getAsJsonArray();
        for (JsonElement j : array) {
            if (j instanceof JsonNull) {
                System.out.println("JsonNull in JSONReader.getJValue(...)");
                System.out.println(value.toString());
            } else {
                mulValue.addValue(getJValueForMultiple(j));
            }
        }
        return mulValue;
    case UNDEFINED:
    default:
        return null;
    }
}

From source file:JSONformating.reader.JSONReader.java

private static JValue getJValueForMultiple(JsonElement j) {
    try {/*w  w w.  ja  v a  2  s  . co m*/
        Double d = j.getAsDouble();
        JNumberValue numberValue = new JNumberValue();
        numberValue.setValue(d);
        return numberValue;
    } catch (java.lang.NumberFormatException e) {
        String str = j.getAsString();
        if (str.equals("true") || str.equals("false")) {
            JBooleanValue booleanValue = new JBooleanValue();
            booleanValue.setValue(j.getAsBoolean());
            return booleanValue;
        } else {
            JStringValue stringValue = new JStringValue();
            stringValue.setValue(str);
            return stringValue;
        }
    }
}

From source file:melnorme.lang.utils.gson.GsonHelper.java

License:Open Source License

public boolean getBoolean(JsonObject jsonObject, String key) throws CommonException {
    JsonElement element = jsonObject.get(key);
    if (element != null && element.isJsonPrimitive() && element.getAsJsonPrimitive().isBoolean()) {
        return element.getAsBoolean();
    } else {/*from   w w  w . j a  v  a 2s  .  c  om*/
        throw wrongTypeException(key, "boolean");
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Gets the boolean value of the given JsonElement.  Expects the second parameter to be the name of the element's
 * field if an error message needs to be thrown.
 *//*from   www  .  j av a  2s . c o  m*/
public static boolean getBoolean(JsonElement json, String memberName) {
    if (json.isJsonPrimitive()) {
        return json.getAsBoolean();
    } else {
        throw new JsonSyntaxException("Expected " + memberName + " to be a Boolean, was " + toString(json));
    }
}

From source file:net.daporkchop.toobeetooteebot.util.Config.java

License:Open Source License

public boolean getBoolean(String key, boolean def) {
    JsonElement element = this.get(key);
    if (element == null) {
        return this.set(key, def).getAsBoolean();
    } else {/*from w  ww .j a va 2s .c  o m*/
        return element.getAsBoolean();
    }
}

From source file:net.doubledoordev.backend.util.TypeHellhole.java

License:Open Source License

public static void set(Field field, Object object, JsonElement value) throws Exception {
    if (field.getType() == byte.class)
        field.setByte(object, value.getAsByte());
    else if (field.getType() == short.class)
        field.setShort(object, value.getAsShort());
    else if (field.getType() == int.class)
        field.setInt(object, value.getAsInt());
    else if (field.getType() == long.class)
        field.setLong(object, value.getAsLong());
    else if (field.getType() == float.class)
        field.setFloat(object, value.getAsFloat());
    else if (field.getType() == double.class)
        field.setDouble(object, value.getAsDouble());
    else if (field.getType() == boolean.class)
        field.setBoolean(object, value.getAsBoolean());
    else if (field.getType() == char.class)
        field.setChar(object, value.getAsCharacter());
    ///*from w  ww . ja v a 2  s. c om*/
    else if (field.getType() == Byte.class)
        field.set(object, value.getAsByte());
    else if (field.getType() == Short.class)
        field.set(object, value.getAsShort());
    else if (field.getType() == Integer.class)
        field.set(object, value.getAsInt());
    else if (field.getType() == Long.class)
        field.set(object, value.getAsLong());
    else if (field.getType() == Float.class)
        field.set(object, value.getAsFloat());
    else if (field.getType() == Double.class)
        field.set(object, value.getAsDouble());
    else if (field.getType() == Boolean.class)
        field.set(object, value.getAsBoolean());
    else if (field.getType() == Character.class)
        field.set(object, value.getAsCharacter());
    //
    else if (field.getType() == String.class)
        field.set(object, value.getAsString());
    else {
        String m = String.format("Unknown type! Field type: %s Json value: %s Data class: %s", field.getType(),
                value.toString(), object.getClass().getSimpleName());
        Main.LOGGER.error(m);
        throw new Exception(m);
    }
}

From source file:net.redwarp.tool.resizer.misc.Settings.java

License:Apache License

public void load(String path) {
    try {/*from   ww  w  .j av a  2s  .  c  om*/
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        InputStream preferenceStream;
        try {
            preferenceStream = new FileInputStream(new File(path));
        } catch (Exception e) {
            preferenceStream = this.getClass().getClassLoader().getResourceAsStream("misc/densities.json");
        }
        JsonObject densitiesObject = parser.parse(new InputStreamReader(preferenceStream)).getAsJsonObject();
        JsonArray densitiesArray = densitiesObject.get(KEY_DENSITIES).getAsJsonArray();

        Type listType = new TypeToken<List<ScreenDensity>>() {
        }.getType();
        list = gson.fromJson(densitiesArray, listType);
        String defaultDensityName = densitiesObject.get(KEY_SOURCE).getAsString();
        for (ScreenDensity density : list) {
            if (density.getName().equals(defaultDensityName)) {
                defaultInputDensity = density;
                break;
            }
        }
        if (defaultInputDensity == null) {
            defaultInputDensity = list.get(0);
        }

        JsonElement keepSameDensityElement = densitiesObject.get(KEY_KEEP_SAME_DENSITY_FILE);
        if (keepSameDensityElement != null) {
            keepSameDensityFile = keepSameDensityElement.getAsBoolean();
        } else {
            keepSameDensityFile = false;
        }
    } catch (Exception e) {
        list = new ArrayList<ScreenDensity>();
        list.add(new ScreenDensity("xhdpi", 2.0f, true));
        defaultInputDensity = list.get(0);
        keepSameDensityFile = false;
        //      versionCode = Configuration.getVersionCode();
    }
}

From source file:org.apache.airavata.workflow.model.graph.system.InputNode.java

License:Apache License

protected void parseConfiguration(JsonObject configObject) {
    super.parseConfiguration(configObject);
    JsonElement jsonElement = configObject.get(VALUE_TAG_NAME);
    if (jsonElement != null) {
        this.defaultValue = jsonElement.getAsString();
    }/*from   w w w  .jav  a 2 s .  c o  m*/

    jsonElement = configObject.get(VISIBILITY_TAG_NAME);
    if (jsonElement != null) {
        this.visibility = jsonElement.getAsBoolean();
    } else {
        this.visibility = true;
    }
}

From source file:org.apache.edgent.runtime.jsoncontrol.JsonControlService.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object[] getArguments(Method method, JsonArray args) {
    final Class<?>[] paramTypes = method.getParameterTypes();

    if (paramTypes.length == 0 || args == null || args.size() == 0)
        return null;

    assert paramTypes.length == args.size();

    Object[] oargs = new Object[paramTypes.length];
    for (int i = 0; i < oargs.length; i++) {
        final Class<?> pt = paramTypes[i];
        final JsonElement arg = args.get(i);
        Object jarg;//  w  ww  .  j  a  v  a 2s . c o  m

        if (String.class == pt) {
            if (arg instanceof JsonObject)
                jarg = gson.toJson(arg);
            else
                jarg = arg.getAsString();
        } else if (Integer.TYPE == pt)
            jarg = arg.getAsInt();
        else if (Long.TYPE == pt)
            jarg = arg.getAsLong();
        else if (Double.TYPE == pt)
            jarg = arg.getAsDouble();
        else if (Boolean.TYPE == pt)
            jarg = arg.getAsBoolean();
        else if (pt.isEnum())
            jarg = Enum.valueOf((Class<Enum>) pt, arg.getAsString());
        else
            throw new UnsupportedOperationException(pt.getName());

        oargs[i] = jarg;
    }
    return oargs;
}

From source file:org.apache.gobblin.converter.grok.GrokToJsonConverter.java

License:Apache License

@VisibleForTesting
JsonObject createOutput(JsonArray outputSchema, String inputRecord) throws DataConversionException {
    JsonObject outputRecord = new JsonObject();

    Match gm = grok.match(inputRecord);/*from w w  w. j  av  a 2s .  c om*/
    gm.captures();

    JsonElement capturesJson = JSON_PARSER.parse(gm.toJson());

    for (JsonElement anOutputSchema : outputSchema) {
        JsonObject outputSchemaJsonObject = anOutputSchema.getAsJsonObject();
        String key = outputSchemaJsonObject.get(COLUMN_NAME_KEY).getAsString();
        String type = outputSchemaJsonObject.getAsJsonObject(DATA_TYPE).get(TYPE_KEY).getAsString();

        if (isFieldNull(capturesJson, key)) {
            if (!outputSchemaJsonObject.get(NULLABLE).getAsBoolean()) {
                throw new DataConversionException(
                        "Field " + key + " is null or not exists but it is non-nullable by the schema.");
            }
            outputRecord.add(key, JsonNull.INSTANCE);
        } else {
            JsonElement jsonElement = capturesJson.getAsJsonObject().get(key);
            switch (type) {
            case "int":
                outputRecord.addProperty(key, jsonElement.getAsInt());
                break;
            case "long":
                outputRecord.addProperty(key, jsonElement.getAsLong());
                break;
            case "double":
                outputRecord.addProperty(key, jsonElement.getAsDouble());
                break;
            case "float":
                outputRecord.addProperty(key, jsonElement.getAsFloat());
                break;
            case "boolean":
                outputRecord.addProperty(key, jsonElement.getAsBoolean());
                break;
            case "string":
            default:
                outputRecord.addProperty(key, jsonElement.getAsString());
            }
        }
    }
    return outputRecord;
}