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:com.couchbase.cbadmin.client.ConnectionInfo.java

License:Open Source License

public ConnectionInfo(JsonObject poolsJson) throws RestApiException {
    JsonElement eTmp;
    eTmp = poolsJson.get("implementationVersion");
    if (eTmp == null) {
        throw new RestApiException("implementationVersion missing", poolsJson);
    }// w  w w.  j  a  v a  2  s  .c o m
    clusterVersion = eTmp.getAsString();

    eTmp = poolsJson.get("pools");
    if (eTmp == null || eTmp.isJsonArray() == false) {
        throw new RestApiException("pools missing", poolsJson);
    }

    if (eTmp.getAsJsonArray().size() > 0) {
        clusterActive = true;
        eTmp = eTmp.getAsJsonArray().get(0);

        if (eTmp.isJsonObject() == false) {
            throw new RestApiException("Expected object in pools entry", eTmp);
        }

        eTmp = eTmp.getAsJsonObject().get("uri");
        if (eTmp == null || eTmp.isJsonPrimitive() == false) {
            throw new RestApiException("uri missing or malformed", eTmp);
        }

        clusterId = eTmp.getAsString();

    } else {
        clusterActive = false;
        clusterId = null;
    }

    eTmp = poolsJson.get("isAdminCreds");
    adminCreds = eTmp != null && eTmp.getAsBoolean();
}

From source file:com.dsh105.powermessage.core.PowerMessage.java

License:Open Source License

/**
 * Converts a raw JSON string to a PowerMessage object. This JSON string is in the format given by {@link #toJson()}
 * @param json the JSON string which represents a PowerMessage
 * @return A PowerMessage representing the given JSON string
 *///from www  .ja  va  2 s  . co m
public static PowerMessage fromJson(String json) {
    JsonArray serialised = jsonParser.parse(json).getAsJsonObject().getAsJsonArray("extra");
    PowerMessage powerMessage = new PowerMessage();

    for (JsonElement serialisedElement : serialised) {
        PowerSnippet snippet = new PowerSnippet("");
        JsonObject message = serialisedElement.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : message.entrySet()) {
            String key = entry.getKey();
            JsonElement element = entry.getValue();

            if (key.equals("text")) {
                snippet.setText(element.getAsString());
            } else if (PowerSnippet.STYLE_TO_NAME_MAP.containsValue(key)) {
                if (element.getAsBoolean()) {
                    snippet.withColour(PowerSnippet.STYLE_TO_NAME_MAP.inverse().get(key));
                }
            } else if (key.equals("color")) {
                snippet.withColour(ChatColor.valueOf(element.getAsString().toUpperCase()));
            } else if (key.equals("clickEvent") || key.equals("hoverEvent")) {
                JsonObject event = element.getAsJsonObject();
                snippet.withEvent(key.substring(0, key.indexOf("Event")), event.get("action").getAsString(),
                        event.get("value").getAsString());
            }
        }
        powerMessage.then(snippet);
    }
    return powerMessage;
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) {

    JsonElement newDataElement;// ww  w. jav a 2  s  . co m
    JsonArray oldArray;
    JsonArray newArray;
    JsonElement oldArrayItem;
    JsonElement newArrayItem;
    JsonObject oldObject;

    if (oldJson == null || oldJson.isJsonNull()) {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    if (newJson == null || newJson.isJsonNull()) {
        newJson = JsonNull.INSTANCE;
        return newJson;
    }

    if (newJson.isJsonPrimitive()) {
        JsonPrimitive value;
        if (!useCopy) {
            return newJson;
        }
        if (newJson.getAsJsonPrimitive().isBoolean()) {
            value = new JsonPrimitive(newJson.getAsBoolean());
        } else if (newJson.getAsJsonPrimitive().isNumber()) {
            value = new JsonPrimitive(newJson.getAsNumber());
        } else if (newJson.getAsJsonPrimitive().isString()) {
            value = new JsonPrimitive(newJson.getAsString());
        } else {
            value = newJson.getAsJsonPrimitive();
        }
        return value;
    }

    if (newJson.isJsonArray()) {
        if (!oldJson.isJsonArray()) {
            return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson;
        } else {
            oldArray = oldJson.getAsJsonArray();
            newArray = newJson.getAsJsonArray();

            if (oldArray.size() > newArray.size()) {
                while (oldArray.size() > newArray.size()) {
                    oldArray.remove(oldArray.size() - 1);
                }
            }

            for (int index = 0; index < newArray.size(); index++) {
                if (index < oldArray.size()) {
                    oldArrayItem = oldArray.get(index);
                    newArrayItem = newArray.get(index);
                    oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson));
                } else {
                    oldArray.add(newArray.get(index));
                }
            }
        }
    } else if (newJson.isJsonObject()) {
        if (!oldJson.isJsonObject()) {
            return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson;
        } else {
            oldObject = oldJson.getAsJsonObject();
            for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) {
                newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson);
                oldObject.add(entry.getKey(), newDataElement);
            }
        }
    } else {
        return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson;
    }

    return oldJson;
}

From source file:com.fooock.shodan.model.exploit.ExploitDeserializer.java

License:Open Source License

private Exploit parseJsonExploit(JsonElement json) {
    Exploit exploit = new Exploit();

    JsonObject jsonObject = json.getAsJsonObject();

    String id = jsonObject.get("_id").getAsString();
    String desc = jsonObject.get("description").getAsString();
    String source = jsonObject.get("source").getAsString();

    JsonElement jsonAuthor = jsonObject.get("author");
    if (jsonAuthor != null && !jsonAuthor.isJsonNull()) {
        if (jsonAuthor.isJsonPrimitive()) {
            String author = jsonAuthor.getAsString();
            exploit.setAuthor(author);/*from w w  w  . j  a  v a2s  .  c  o m*/
        } else {
            JsonArray array = jsonAuthor.getAsJsonArray();
            if (array != null) {
                String resAuthors = "";
                for (JsonElement element : array) {
                    resAuthors += ", " + element.getAsString();
                }
                exploit.setAuthor(resAuthors);
            }
        }
    }

    JsonElement jsonCode = jsonObject.get("code");
    if (jsonCode != null && !jsonCode.isJsonNull()) {
        String code = jsonCode.getAsString();
        exploit.setCode(code);
    }

    JsonElement jsonType = jsonObject.get("type");
    if (jsonType != null && !jsonType.isJsonNull()) {
        String type = jsonType.getAsString();
        exploit.setType(type);
    }

    JsonElement jsonVersion = jsonObject.get("version");
    if (jsonVersion != null && !jsonVersion.isJsonNull()) {
        String version = jsonVersion.getAsString();
        exploit.setVersion(version);
    }

    JsonElement jsonPrivileged = jsonObject.get("privileged");
    if (jsonPrivileged != null && !jsonPrivileged.isJsonNull()) {
        boolean privileged = jsonPrivileged.getAsBoolean();
        exploit.setPrivileged(privileged);
    }

    JsonElement jsonPort = jsonObject.get("port");
    if (jsonPort != null && !jsonPort.isJsonNull()) {
        int port = jsonPort.getAsInt();
        exploit.setPort(port);
    }

    JsonArray jsonBid = jsonObject.getAsJsonArray("bid");
    if (jsonBid != null) {
        String[] bid = new String[jsonBid.size()];
        for (int i = 0; i < jsonBid.size(); i++) {
            bid[i] = jsonBid.get(i).getAsString();
        }
        exploit.setBid(bid);
    }

    JsonArray jsonCve = jsonObject.getAsJsonArray("cve");
    if (jsonCve != null) {
        String[] cve = new String[jsonCve.size()];
        for (int i = 0; i < jsonCve.size(); i++) {
            cve[i] = jsonCve.get(i).getAsString();
        }
        exploit.setCve(cve);
    }

    JsonArray jsonMsb = jsonObject.getAsJsonArray("msb");
    if (jsonMsb != null) {
        String[] msb = new String[jsonMsb.size()];
        for (int i = 0; i < jsonMsb.size(); i++) {
            msb[i] = jsonMsb.get(i).getAsString();
        }
        exploit.setMsb(msb);
    }

    JsonArray jsonOsvdb = jsonObject.getAsJsonArray("osvdb");
    if (jsonOsvdb != null) {
        String[] osvdb = new String[jsonOsvdb.size()];
        for (int i = 0; i < jsonOsvdb.size(); i++) {
            osvdb[i] = jsonOsvdb.get(i).getAsString();
        }
        exploit.setOsvdb(osvdb);
    }

    try {
        JsonArray jsonPlatform = jsonObject.getAsJsonArray("platform");
        if (jsonPlatform != null && jsonPlatform.isJsonArray()) {
            String[] platform = new String[jsonPlatform.size()];
            for (int i = 0; i < jsonPlatform.size(); i++) {
                platform[i] = jsonPlatform.get(i).getAsString();
            }
            exploit.setPlatform(platform);
        }
    } catch (ClassCastException err) {
        JsonElement platPrimitive = jsonObject.get("platform");
        if (platPrimitive != null && !platPrimitive.isJsonNull()) {
            exploit.setPlatform(new String[] { platPrimitive.getAsString() });
        }
    }

    exploit.setId(id);
    exploit.setDescription(desc);
    exploit.setSource(source);

    return exploit;
}

From source file:com.getperka.flatpack.codexes.BooleanCodex.java

License:Apache License

@Override
public Boolean readNotNull(JsonElement element, DeserializationContext context) {
    return element.getAsJsonPrimitive().isNumber() ? element.getAsNumber().intValue() != 0
            : element.getAsBoolean();
}

From source file:com.github.api.v2.services.impl.BaseGitHubService.java

License:Apache License

/**
 * Gets the gson builder./* ww w.j  a  va 2s.  c om*/
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(ApplicationConstants.DATE_FORMAT);
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.setFieldNamingStrategy(new FieldNamingStrategy() {
        @Override
        public String translateName(Field field) {
            if (field.getType().equals(Repository.Visibility.class)) {
                return "private";
            } else if (field.getType().equals(Gist.Visibility.class)) {
                return "public";
            } else {
                return field.getName();
            }
        }

    });
    builder.registerTypeAdapter(Issue.State.class, new JsonDeserializer<Issue.State>() {
        @Override
        public Issue.State deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Issue.State.fromValue(arg0.getAsString());
        }
    });
    builder.registerTypeAdapter(Repository.Visibility.class, new JsonDeserializer<Repository.Visibility>() {
        @Override
        public Repository.Visibility deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return (arg0.getAsBoolean()) ? Repository.Visibility.PRIVATE : Repository.Visibility.PUBLIC;
        }
    });
    builder.registerTypeAdapter(Gist.Visibility.class, new JsonDeserializer<Gist.Visibility>() {
        @Override
        public Gist.Visibility deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return (arg0.getAsBoolean()) ? Gist.Visibility.PUBLIC : Gist.Visibility.PRIVATE;
        }
    });
    builder.registerTypeAdapter(Language.class, new JsonDeserializer<Language>() {
        @Override
        public Language deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Language.fromValue(arg0.getAsString());
        }
    });
    builder.registerTypeAdapter(Tree.Type.class, new JsonDeserializer<Tree.Type>() {
        @Override
        public Tree.Type deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            return Tree.Type.fromValue(arg0.getAsString());
        }
    });
    return builder;
}

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabBoolean parseMatlabBoolean(JsonElement value) {
    return MatlabBoolean.fromBoolean(value.getAsBoolean());
}

From source file:com.github.kyriosdata.regras.infraestrutura.ValorDeserializer.java

License:Creative Commons License

@Override
public Valor deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    byte tipo = jsonObject.get("tipo").getAsByte();
    JsonElement valor = jsonObject.get("valor");

    if (tipo == Valor.STRING) {
        return new Valor(valor.getAsString());
    } else if (tipo == Valor.REAL) {
        return new Valor(valor.getAsFloat());
    } else if (tipo == Valor.LOGICO) {
        return new Valor(valor.getAsBoolean());
    } else if (tipo == Valor.DATA) {
        return Valor.dataFromString(valor.getAsString());
    }/*from   w ww.j  a v a  2 s .  c  o  m*/

    return null;
}

From source file:com.github.strawberry.util.Json.java

License:Open Source License

public static Object parsePrimitive(JsonElement e) {
    JsonPrimitive p = e.getAsJsonPrimitive();
    if (p.isString()) {
        return e.getAsString();
    }//from   w  w w  .  ja  v  a 2s .co m
    if (p.isBoolean()) {
        return e.getAsBoolean();
    }
    if (p.isNumber()) {
        return e.getAsInt();
    }
    return p.getAsString();
}

From source file:com.graphaware.module.es.Neo4jElasticVerifier.java

License:Open Source License

private void checkBooleanArray(JsonObject source, String key, Map<String, Object> properties) {
    assertTrue(source.get(key) instanceof JsonArray);
    JsonArray jsonArray = source.get(key).getAsJsonArray();
    TreeSet<Boolean> esSet = new TreeSet();
    for (JsonElement element : jsonArray) {
        esSet.add(element.getAsBoolean());
    }//from   w ww. ja v  a 2  s.  co m
    TreeSet<Boolean> nodeSet = new TreeSet();
    boolean[] propertyArray = (boolean[]) properties.get(key);
    for (boolean element : propertyArray) {
        nodeSet.add(element);
    }
    assertEquals(esSet, nodeSet);
}