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.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Returns the source json as a boolean if possible. Else returns the default boolean
 *
 * @param source the source json element
 * @param defaultBool the default boolean
 * @return the source json as a boolean/*from   w  ww  .  j  av a2  s  . co m*/
 */
public Boolean asBoolean(JsonElement source, Boolean defaultBool) {
    return isBoolean(source) ? (Boolean) source.getAsBoolean() : defaultBool;
}

From source file:com.bekwam.resignator.model.ConfigurationJSONAdapter.java

License:Apache License

private List<Profile> deserializeProfiles(JsonArray profiles) {

    List<Profile> ps = new ArrayList<>();

    for (JsonElement e : profiles) {

        JsonObject profileObj = (JsonObject) e;
        String profileName = profileObj.get("profileName").getAsString();

        Boolean rs = Boolean.FALSE;
        if (profileObj.get("replaceSignatures") != null) {
            rs = profileObj.get("replaceSignatures").getAsBoolean();
        }/*from   w ww  .  java2  s  .  c  om*/

        SigningArgumentsType argsType = SigningArgumentsType.JAR;
        if (profileObj.get("argsType") != null) {
            String at = profileObj.get("argsType").getAsString();
            if (StringUtils.equalsIgnoreCase(at, String.valueOf(SigningArgumentsType.FOLDER))) {
                argsType = SigningArgumentsType.FOLDER;
            }
        }

        Profile p = new Profile(profileName, rs, argsType);

        //
        // SourceFile part
        //
        JsonObject sourceObj = profileObj.getAsJsonObject("sourceFile");
        if (sourceObj != null) {
            JsonElement sfe = sourceObj.get("fileName");
            if (sfe != null) {
                SourceFile sf = new SourceFile(sfe.getAsString());
                p.setSourceFile(Optional.of(sf));
            }
        }

        //
        // TargetFile part
        //
        JsonObject targetObj = profileObj.getAsJsonObject("targetFile");
        if (sourceObj != null) {
            JsonElement tfe = targetObj.get("fileName");
            if (tfe != null) {
                TargetFile tf = new TargetFile(tfe.getAsString());
                p.setTargetFile(Optional.of(tf));
            }
        }

        //
        // JarsignerConfig part
        //
        JsonObject jcObj = profileObj.getAsJsonObject("jarsignerConfig");
        if (jcObj != null) {

            String alias = "";
            String storepass = "";
            String keypass = "";
            String keystore = "";
            Boolean verbose = false;

            JsonElement ae = jcObj.get("alias");
            if (ae != null) {
                alias = ae.getAsString();
            }

            JsonElement spe = jcObj.get("storepass");
            if (spe != null) {
                storepass = spe.getAsString();
            }

            JsonElement kpe = jcObj.get("keypass");
            if (kpe != null) {
                keypass = kpe.getAsString();
            }

            JsonElement kse = jcObj.get("keystore");
            if (kse != null) {
                keystore = kse.getAsString();
            }

            JsonElement ve = jcObj.get("verbose");
            if (ve != null) {
                verbose = ve.getAsBoolean();
            }

            JarsignerConfig jc = new JarsignerConfig(alias, "", "", keystore, verbose);
            jc.setEncryptedKeypass(keypass);
            jc.setEncryptedStorepass(storepass);

            p.setJarsignerConfig(Optional.of(jc));
        }

        ps.add(p);
    }

    return ps;
}

From source file:com.bynder.sdk.util.BooleanTypeAdapter.java

License:Open Source License

/**
 * Check {@link JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)} for
 * more information./*  w  w  w  . j  a v a  2 s . co m*/
 */
@Override
public Boolean deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    if (Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()).contains(json.toString())) {
        return json.getAsBoolean();
    } else {
        try {
            int code = json.getAsInt();

            if (code == 0) {
                return false;
            } else if (code == 1) {
                return true;
            } else {
                return null;
            }
        } catch (NumberFormatException e) {
            return null;
        }
    }
}

From source file:com.ccc.crest.core.cache.crest.schema.option.CcpType.java

License:Open Source License

@Override
public CcpType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (DescriptionKey.equals(key))
            description = checkNull(value);
        else if (OptionalKey.equals(key))
            optional = value.getAsBoolean();
        else if (ExtraKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (objectElement.isJsonArray()) {
                int size = ((JsonArray) objectElement).size();
                for (int i = 0; i < size; i++) {
                    JsonElement childElement = ((JsonArray) objectElement).get(i);
                    extraData.add(childElement.getAsString());
                }/*  w ww. j  a v a 2  s.c  om*/
            } else
                extraData.add(checkNull(value));

        } else if (SubContentKey.equals(key)) {
            if (!value.isJsonNull()) {
                Iterator<Entry<String, JsonElement>> varsIter = ((JsonObject) value).entrySet().iterator();
                do {
                    if (!varsIter.hasNext())
                        break;
                    Entry<String, JsonElement> varsEntry = varsIter.next();
                    CcpType subContent = new CcpType(varsEntry.getKey());
                    subContent = subContent.deserialize(varsEntry.getValue(), typeOfT, context);
                    children.add(subContent);
                } while (true);
            }
        } else if (PrettyTypeKey.equals(key))
            typePrettyName = checkNull(value);
        else if (TypeKey.equals(key))
            type = checkNull(value);
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.schema.option.Representation.java

License:Open Source License

@Override
public Representation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (AcceptVersionStrKey.equals(key))
            versionStr = value.getAsString();
        else if (AcceptTypeKey.equals(key)) {
            acceptType = new AcceptType();
            acceptType.deserialize(value, typeOfT, context);

        } else if (VerbKey.equals(key))
            verb = value.getAsString();//from  www  .j a va2 s. c o m
        else if (VersionKey.equals(key))
            version = value.getAsLong();
        else if (ThirdPartyKey.equals(key))
            thirdParty = value.getAsBoolean();
        else if (ContentTypeKey.equals(key)) {
            ContentType contentType = new ContentType();
            contentType.deserialize(value, typeOfT, context);
        } else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.tournament.Matches.java

License:Open Source License

@Override
public Matches deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (WinnerKey.equals(key)) {
            winner = new ExternalRef();
            winner.deserialize(value, typeOfT, context);
        } else if (StatsKey.equals(key)) {
            stats = new Stats();
            stats.deserialize(json, typeOfT, context);
        } else if (RedTeamKey.equals(key)) {
            redTeam = new Team();
            redTeam.deserialize(value, typeOfT, context);
        } else if (BansKey.equals(key)) {
            bans = new Bans();
            bans.deserialize(value, typeOfT, context);
        } else if (FinalizedKey.equals(key))
            finalized = value.getAsBoolean();
        else if (SeriesKey.equals(key)) {
            series = new ExternalRef();
            series.deserialize(value, typeOfT, context);
        } else if (TournamentKey.equals(key)) {
            tournament = new ExternalRef();
            tournament.deserialize(value, typeOfT, context);
        } else if (StaticSceneKey.equals(key)) {
            staticSceneData = new ExternalRef();
            staticSceneData.deserialize(value, typeOfT, context);
        } else if (FirstReplayKey.equals(key)) {
            firstReplayFrame = new ExternalRef();
            firstReplayFrame.deserialize(value, typeOfT, context);
        } else if (LastReplayKey.equals(key)) {
            lastReplayFrame = new ExternalRef();
            lastReplayFrame.deserialize(value, typeOfT, context);
        } else if (BlueTeamKey.equals(key)) {
            blueTeam = new Team();
            blueTeam.deserialize(value, typeOfT, context);
        } else if (InProgressKey.equals(key))
            inProgress = value.getAsBoolean();
        else if (ScoreKey.equals(key)) {
            score = new Score();
            score.deserialize(value, typeOfT, context);
        } else//  www .  jav a2 s .  c  o  m
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.tournament.TeamWrapper.java

License:Open Source License

@Override
public TeamWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (TeamKey.equals(key)) {
            team = new Team();
            team.deserialize(value, typeOfT, context);
        } else if (DecidedKey.equals(key))
            decided = value.getAsBoolean();
        else if (ByeKey.equals(key))
            bye = value.getAsBoolean();//from w  w w  .j  av  a  2s  .  c  om
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.ParserUtil.java

License:Apache License

public static boolean optionalJsonBoolean(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if ((element == null) || (element.isJsonNull()))
        return false;
    return element.getAsBoolean();
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public boolean getAsBoolean(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsBoolean();
}

From source file:com.confighub.api.util.GsonHelper.java

License:Open Source License

public static JsonObject fileAuditToJSON(RepoFile configFile, SecurityProfile ep, Store store, UserAccount user,
        Repository repository, Long ts) {
    JsonObject json = new JsonObject();
    json.addProperty("fileName", configFile.getAbsFilePath().getFilename());
    json.addProperty("absPath", configFile.getAbsPath());

    boolean diffEncrypted = false, entryEncrypted = configFile.isEncrypted();

    boolean showContent = true; //entryEncrypted;

    String diff = configFile.getDiffJson();

    if (Utils.isBlank(diff))
        json.add("diff", null);
    else {/*from   w  ww.  ja  v a  2s . c om*/
        JsonObject diffJson = new Gson().fromJson(diff, JsonObject.class);
        JsonElement encEl = diffJson.get("encrypted");
        diffEncrypted = null != encEl && encEl.getAsBoolean();

        showContent = (!diffEncrypted && !entryEncrypted) || !entryEncrypted;

        if (!showContent) {
            diffJson.remove("content");
            diffJson.addProperty("content", "");
            diffJson.addProperty("encryptionState", 1);
        } else {
            if (diffEncrypted) {

                String oldSpName = diffJson.get("spName").getAsString().trim();

                Date dateObj = DateTimeUtils.dateFromTs(ts, null);
                SecurityProfile oldSp = store.getSecurityProfile(user, repository, dateObj, oldSpName);

                String oldDecrypted = Encryption.decrypt(oldSp.getCipher(),
                        diffJson.get("content").getAsString().trim(), oldSp.getDecodedPassword());

                diffJson.addProperty("content", oldDecrypted);
                diffJson.remove("encrypted");
            }
        }

        json.add("diff", diffJson);
    }

    json.addProperty("spName",
            null == configFile.getSecurityProfile() ? "" : configFile.getSecurityProfile().getName());

    json.addProperty("score", configFile.getContextWeight());
    json.add("levels", configFile.getContextJsonObj());

    json.addProperty("id", configFile.getId());
    json.addProperty("active", configFile.isActive());

    if (entryEncrypted) {
        int encryptionState;
        if (null != ep) {
            try {
                if (ep.encryptionEnabled() && !Utils.isBlank(ep.sk)) {
                    configFile.decryptFile(ep.sk);
                    json.addProperty("content", configFile.getContent());
                    encryptionState = 2;
                } else {
                    json.addProperty("content", "");
                    encryptionState = 1;
                }
            } catch (ConfigException e) {
                e.printStackTrace();
                log.error("Unable to get contents of file: " + configFile);
                json.addProperty("content", "");
                encryptionState = 1;
            }
        } else if (diffEncrypted || entryEncrypted) {
            encryptionState = 1;
            json.addProperty("content", "");
        } else {
            encryptionState = 0;
            json.addProperty("content", configFile.getContent());
        }

        json.addProperty("encryptionState", encryptionState);
        json.addProperty("secure", configFile.isSecure());
    } else
        json.addProperty("content", !showContent ? "" : configFile.getContent());

    return json;
}