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:org.apache.sling.jms.Json.java

License:Apache License

private static <T> T toMapValue(JsonPrimitive p) {
    if (p.isString()) {
        return (T) p.getAsString();
    } else if (p.isBoolean()) {
        return (T) ((Boolean) p.getAsBoolean());
    } else if (p.isNumber()) {
        double d = p.getAsDouble();
        if (Math.floor(d) == d) {
            return (T) ((Long) p.getAsLong());
        }/*from ww  w .  java 2 s . c o m*/
        return (T) ((Double) d);
    } else {
        return null;
    }
}

From source file:org.cvasilak.jboss.mobile.app.model.ManagementModelBase.java

License:Apache License

public void setValue(JsonElement value) {
    if (value instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) value;

        if (primitive.isNumber()) {
            try {
                this.value = NumberFormat.getInstance().parse(primitive.getAsString());
            } catch (ParseException e) {
            }/*w  w  w .j a v a2s  .  c o  m*/
        } else if (primitive.isBoolean()) {
            this.value = primitive.getAsBoolean();
        } else if (primitive.isString()) {
            this.value = primitive.getAsString();
        }

    } else if (value instanceof JsonNull) {
        this.value = "undefined";

    } else if (value instanceof JsonArray) {
        List<String> list = new ArrayList<String>();

        Iterator<JsonElement> iterator = value.getAsJsonArray().iterator();

        while (iterator.hasNext()) {
            JsonElement elem = iterator.next();

            if (elem instanceof JsonObject)
                list.add(elem.toString());
            else
                list.add(elem.getAsString());
        }

        this.value = list;

    } else if (value instanceof JsonObject) {
        this.value = value.toString();
    }
}

From source file:org.cvasilak.jboss.mobile.app.model.OperationParameter.java

License:Apache License

public void setDefaultValue(JsonElement value) {
    if (value instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) value;

        if (primitive.isNumber()) {
            try {
                this.defaultValue = NumberFormat.getInstance().parse(primitive.getAsString());
            } catch (ParseException e) {
            }/*from w w w.j av a2s . c  o m*/
        } else if (primitive.isBoolean()) {
            this.defaultValue = primitive.getAsBoolean();
        } else if (primitive.isString()) {
            this.defaultValue = primitive.getAsString();
        }
    }
}

From source file:org.debux.webmotion.server.call.ClientSession.java

License:Open Source License

/**
 * Return only JsonElement use getAttribute and getAttributes with class as 
 * parameter to get a value./*from   w w w .  j  a  va 2s .  co  m*/
 */
@Override
public Object getAttribute(String name) {
    JsonElement value = attributes.get(name);
    if (value == null) {
        return value;

    } else if (value.isJsonPrimitive()) {
        JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();

        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();

        } else if (primitive.isNumber()) {
            return primitive.getAsDouble();
        }

    } else if (value.isJsonArray()) {
        return value.getAsJsonArray();

    } else if (value.isJsonObject()) {
        return value.getAsJsonObject();

    } else if (value.isJsonNull()) {
        return value.getAsJsonNull();
    }
    return value;
}

From source file:org.devnexus.aerogear.ShadowStore.java

License:Apache License

private void saveElement(JsonObject serialized, String path, Serializable id) {
    String sql = String.format(
            "insert into shadow_%s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)",
            className);/*  w ww  .j av  a2s  . c  o m*/
    Set<Map.Entry<String, JsonElement>> members = serialized.entrySet();
    String pathVar = path.isEmpty() ? "" : ".";
    for (Map.Entry<String, JsonElement> member : members) {
        JsonElement jsonValue = member.getValue();
        String propertyName = member.getKey();
        if (jsonValue.isJsonObject()) {
            saveElement((JsonObject) jsonValue, path + pathVar + propertyName, id);
        } else if (jsonValue.isJsonArray()) {
            JsonArray jsonArray = jsonValue.getAsJsonArray();
            for (int index = 0; index < jsonArray.size(); index++) {
                JsonElement arrayElement = jsonArray.get(index);
                if (arrayElement.isJsonPrimitive()) {
                    JsonPrimitive primitive = arrayElement.getAsJsonPrimitive();
                    if (primitive.isBoolean()) {
                        String value = primitive.getAsBoolean() ? "true" : "false";
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else if (primitive.isNumber()) {
                        Number value = primitive.getAsNumber();
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else if (primitive.isString()) {
                        String value = primitive.getAsString();
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else {
                        throw new IllegalArgumentException(
                                arrayElement + " isn't a number, boolean, or string");
                    }

                } else {
                    saveElement(arrayElement.getAsJsonObject(),
                            path + pathVar + propertyName + String.format("[%d]", index), id);
                }

            }
        } else {
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    String value = primitive.getAsBoolean() ? "true" : "false";
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isNumber()) {
                    Number value = primitive.getAsNumber();
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isString()) {
                    String value = primitive.getAsString();
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else {
                    throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string");
                }

            } else {
                throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive");
            }

        }
    }
}

From source file:org.devnexus.aerogear.ShadowStore.java

License:Apache License

private void buildKeyValuePairs(JsonObject where, List<Pair<String, String>> keyValues, String parentPath) {
    Set<Map.Entry<String, JsonElement>> keys = where.entrySet();
    String pathVar = parentPath.isEmpty() ? "" : ".";//Set a dot if parent path is not empty
    for (Map.Entry<String, JsonElement> entry : keys) {
        String key = entry.getKey();
        String path = parentPath + pathVar + key;
        JsonElement jsonValue = entry.getValue();
        if (jsonValue.isJsonObject()) {
            buildKeyValuePairs((JsonObject) jsonValue, keyValues, path);
        } else {//  w  w w.j a va2  s  .  c  o m
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    String value = primitive.getAsBoolean() ? "true" : "false";
                    keyValues.add(new Pair<String, String>(path, value));
                } else if (primitive.isNumber()) {
                    Number value = primitive.getAsNumber();
                    keyValues.add(new Pair<String, String>(path, value.toString()));
                } else if (primitive.isString()) {
                    String value = primitive.getAsString();
                    keyValues.add(new Pair<String, String>(path, value));
                } else {
                    throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string");
                }

            } else {
                throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive");
            }

        }
    }
}

From source file:org.eclim.Eclim.java

License:Open Source License

public static Object toType(JsonElement json) {
    // null/*from w ww .  j  a v  a2s  . c om*/
    if (json.isJsonNull()) {
        return null;

        // int, double, boolean, String
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive prim = json.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().indexOf('.') != -1) {
                return prim.getAsDouble();
            }
            return prim.getAsInt();
        }

        // List
    } else if (json.isJsonArray()) {
        ArrayList<Object> type = new ArrayList<Object>();
        for (JsonElement element : json.getAsJsonArray()) {
            type.add(toType(element));
        }
        return type;

        // Map
    } else if (json.isJsonObject()) {
        HashMap<String, Object> type = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            type.put(entry.getKey(), toType(entry.getValue()));
        }
        return type;
    }

    return null;
}

From source file:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcUnmarshaller.java

License:Open Source License

private Object getInnerItem(JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return null;
    }//from  ww w.  ja  va 2 s  .c  o m

    if (jsonElement.isJsonObject()) {
        return jsonElement.getAsJsonObject();
    }

    if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
        if (jsonPrimitive.isNumber()) {
            return jsonPrimitive.getAsDouble();
        } else if (jsonPrimitive.isString()) {
            return jsonPrimitive.getAsString();
        } else {
            return jsonPrimitive.getAsBoolean();
        }
    }

    throw new IllegalStateException("Unexpected json element type");
}

From source file:org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeDeserializer.java

License:Open Source License

private Object deserializeValue(JsonPrimitive val,
        org.eclipse.leshan.core.model.ResourceModel.Type expectedType) {
    switch (expectedType) {
    case BOOLEAN:
        return val.getAsBoolean();
    case STRING:/*w ww .jav a 2s.co  m*/
        return val.getAsString();
    case INTEGER:
        return val.getAsLong();
    case FLOAT:
        return val.getAsDouble();
    case TIME:
    case OPAQUE:
    default:
        // TODO we need to better handle this.
        return val.getAsString();
    }
}

From source file:org.eclipse.leshan.server.demo.servlet.json.SecurityDeserializer.java

License:Open Source License

@Override
public SecurityInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json == null) {
        return null;
    }/*from w  w w  .  j a  v  a 2  s  .  c  o  m*/

    SecurityInfo info = null;

    if (json.isJsonObject()) {
        JsonObject object = (JsonObject) json;

        String endpoint;
        if (object.has("endpoint")) {
            endpoint = object.get("endpoint").getAsString();
        } else {
            throw new JsonParseException("Missing endpoint");
        }

        JsonObject psk = (JsonObject) object.get("psk");
        JsonObject rpk = (JsonObject) object.get("rpk");
        JsonPrimitive x509 = object.getAsJsonPrimitive("x509");
        if (psk != null) {
            // PSK Deserialization
            String identity;
            if (psk.has("identity")) {
                identity = psk.get("identity").getAsString();
            } else {
                throw new JsonParseException("Missing PSK identity");
            }
            byte[] key;
            try {
                key = Hex.decodeHex(psk.get("key").getAsString().toCharArray());
            } catch (IllegalArgumentException e) {
                throw new JsonParseException("key parameter must be a valid hex string", e);
            }

            info = SecurityInfo.newPreSharedKeyInfo(endpoint, identity, key);
        } else if (rpk != null) {
            PublicKey key;
            try {
                byte[] x = Hex.decodeHex(rpk.get("x").getAsString().toCharArray());
                byte[] y = Hex.decodeHex(rpk.get("y").getAsString().toCharArray());
                String params = rpk.get("params").getAsString();

                AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
                algoParameters.init(new ECGenParameterSpec(params));
                ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);

                KeySpec keySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)),
                        parameterSpec);

                key = KeyFactory.getInstance("EC").generatePublic(keySpec);
            } catch (IllegalArgumentException | InvalidKeySpecException | NoSuchAlgorithmException
                    | InvalidParameterSpecException e) {
                throw new JsonParseException("Invalid security info content", e);
            }
            info = SecurityInfo.newRawPublicKeyInfo(endpoint, key);
        } else if (x509 != null && x509.getAsBoolean()) {
            info = SecurityInfo.newX509CertInfo(endpoint);
        } else {
            throw new JsonParseException("Invalid security info content");
        }
    }

    return info;
}