Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:cd.go.authentication.ldap.model.Credentials.java

License:Apache License

public static Credentials fromJSON(String requestBody) {
    JsonObject jsonObject = Util.GSON.fromJson(requestBody, JsonObject.class);
    return Util.GSON.fromJson(jsonObject.get("credentials").toString(), Credentials.class);
}

From source file:cd.go.authentication.passwordfile.executor.IsValidUserRequestExecutor.java

License:Apache License

@Override
public GoPluginApiResponse execute() throws Exception {
    JsonObject jsonObject = GSON.fromJson(request.requestBody(), JsonObject.class);
    Type type = new TypeToken<AuthConfig>() {
    }.getType();//from w w  w.j  a v  a  2 s . c o  m

    String username = jsonObject.get("username").getAsString();
    AuthConfig authConfig = GSON.fromJson(jsonObject.get("auth_config").toString(), type);

    final User found = find(username, authConfig);

    if (found != null) {
        return new DefaultGoPluginApiResponse(200);
    }

    return new DefaultGoPluginApiResponse(404);
}

From source file:cf.adriantodt.David.modules.cmds.FeedCmd.java

License:LGPL

@PostReady
private static void lazyLoad() {
    logger.trace("Loading...");
    PushCmd.registerDynamicTypes(() -> ALL_TYPES, "feeds");

    DBModule.onDB(r -> r.table("feeds")).run().cursorExpected().forEach(json -> {
        JsonObject feed = json.getAsJsonObject();
        Subscription s = new Subscription(feed.get("pushName").getAsString(),
                newURL(EncodingUtil.decodeURIComponent(feed.get("url").getAsString())),
                feed.get("id").getAsString());

        if (feed.has("lastHashCode") && ConfigUtils.isJsonNumber(feed.get("lastHashCode")))
            s.setLastHashCode(feed.get("lastHashCode").getAsInt());
    });/*  w  ww  . j  ava  2s .c om*/

    i = 1;
    logger.trace("k");

    TaskManager.startAsyncTask("Feed Loop", FeedCmd::loop, 5);
}

From source file:cf.adriantodt.David.modules.cmds.FunnyCmd.java

License:LGPL

@OnEnabled
private static void enabled() {
    try {/*from w w w . j  a  v a  2s .com*/
        TESV_GUARDS = skyrim_guards_txt.split("\\r?\\n");
        TESV_GUARDS_LOADED = true;
    } catch (Exception e) {
        logger.error("Error while parsing \"skyrim_guards.txt\" resource.", e);
    }

    try {
        TESV_LYDIA = skyrim_lydia_txt.split("\\r?\\n");
        TESV_LYDIA_LOADED = true;
    } catch (Exception e) {
        logger.error("Error while parsing \"skyrim_lydia.txt\" resource.", e);
    }

    try {
        JsonObject object = stevenuniverse_theories_json.getAsJsonObject();
        List<List<List<String>>> SU_THEORIES_BUILD = Arrays.asList(
                Arrays.asList(new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()),
                Arrays.asList(new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()));
        object.get("characters").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(0).add(element.getAsString());
            SU_THEORIES_BUILD.get(0).get(2).add(element.getAsString());
        });

        object.get("places").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(1).get(0).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(2).add(element.getAsString());
        });

        object.get("objects").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(0).add(element.getAsString());
            SU_THEORIES_BUILD.get(0).get(2).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(0).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(2).add(element.getAsString());
        });

        object.get("gems").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(0).add(element.getAsString());
            SU_THEORIES_BUILD.get(0).get(2).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(0).add(element.getAsString() + "'s room");
            SU_THEORIES_BUILD.get(1).get(2).add(element.getAsString() + "'s room");
        });

        object.get("fusionGems").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(0).add(element.getAsString());
            SU_THEORIES_BUILD.get(0).get(2).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(0).add(element.getAsString() + "'s room");
            SU_THEORIES_BUILD.get(1).get(2).add(element.getAsString() + "'s room");
            SU_THEORIES_BUILD.get(1).get(0).add(element.getAsString() + "'s fusion realm");
            SU_THEORIES_BUILD.get(1).get(2).add(element.getAsString() + "'s fusion realm");
        });

        object.get("verb").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(1).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(1).add(element.getAsString());
        });
        object.get("revelation4characters").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(2).add(element.getAsString());
        });
        object.get("revelation4places").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(1).get(2).add(element.getAsString());
        });
        object.get("post").getAsJsonArray().forEach(element -> {
            SU_THEORIES_BUILD.get(0).get(3).add(element.getAsString());
            SU_THEORIES_BUILD.get(1).get(3).add(element.getAsString());
        });

        SU_THEORIES = SU_THEORIES_BUILD.stream()
                .map(ll -> ll.stream().map(l -> l.stream().toArray(String[]::new)).toArray(String[][]::new))
                .toArray(String[][][]::new);
        SU_THEORIES_LOADED = true;
    } catch (Exception e) {
        logger.error("Error while parsing \"stevenuniverse_theories.json\" resource.", e);
    }

    try {
        SU_STEVONNIE = stevenuniverse_stevonnie_txt.split("\\r?\\n");
        SU_STEVONNIE_LOADED = true;
    } catch (Exception e) {
        logger.error("Error while parsing \"stevenuniverse_stevonnie.txt\" resource.", e);
    }
}

From source file:cf.adriantodt.David.modules.db.DBModule.java

License:LGPL

private DBModule() {
    mainConfig = ConfigUtils.get("main",
            ImmutableMap.<String, java.util.function.Predicate<JsonElement>>builder()
                    .put("ownerID", ConfigUtils::isJsonString).put("token", ConfigUtils::isJsonString).build(),
            () -> {/*from  ww  w .j  a  va 2  s .c om*/
                JsonObject object = new JsonObject();
                object.add("ownerID", null);
                object.add("token", null);
                return object;
            }, false, true);

    JsonObject dbConfig = ConfigUtils.get("db",
            ImmutableMap.<String, java.util.function.Predicate<JsonElement>>builder()
                    .put("hostname", ConfigUtils::isJsonString)
                    .put("port", element -> ConfigUtils.isJsonNumber(element) && element.getAsInt() != 0)
                    .build(),
            () -> {
                JsonObject object = new JsonObject();
                object.addProperty("hostname", "localhost");
                object.addProperty("port", 28015);
                return object;
            }, true, true);

    conn = r.connection().hostname(dbConfig.get("hostname").getAsString()).port(dbConfig.get("port").getAsInt())
            .db("bot").connect();
}

From source file:cf.adriantodt.David.modules.db.GuildModule.java

License:LGPL

private static Data unpack(JsonElement element) {
    JsonObject object = element.getAsJsonObject();
    Data data = all.stream().filter(dataPredicate -> object.get("id").getAsString().equals(dataPredicate.id))
            .findFirst().orElseGet(Data::new);
    data.id = object.get("id").getAsString();
    data.name = object.get("name").getAsString();
    data.cmdPrefixes.clear();// w w w. j  ava2  s. c o  m
    object.get("cmdPrefixes").getAsJsonArray()
            .forEach(jsonElement -> data.cmdPrefixes.add(jsonElement.getAsString()));
    data.lang = object.get("lang").getAsString();
    data.flags.clear();
    object.get("flags").getAsJsonObject().entrySet()
            .forEach(entry -> data.flags.put(entry.getKey(), entry.getValue().getAsBoolean()));
    object.get("userPerms").getAsJsonObject().entrySet()
            .forEach(entry -> data.userPerms.put(entry.getKey(), entry.getValue().getAsLong()));
    return data;
}

From source file:cf.adriantodt.David.modules.db.I18nModule.java

License:LGPL

@PostReady
private static void load() {
    localizeLocal("botname", jda.getSelfUser().getName());
    localizeLocal("mention", jda.getSelfUser().getAsMention());

    JsonObject mainFile = new JsonParser().parse(i18nMain).getAsJsonObject();
    mainFile.entrySet().forEach(entry -> {
        //Before Load, Parse Contents
        JsonObject def = entry.getValue().getAsJsonObject();
        if (def.has("parent"))
            setParent(entry.getKey(), def.get("parent").getAsString());

        String resource = manager.get(entry.getKey() + ".json");
        if (resource == null)
            return;

        loadFile(entry.getKey(), new JsonParser().parse(resource));
    });/*from   ww  w  .  java  2  s .  c o  m*/
}

From source file:cf.adriantodt.David.modules.db.I18nModule.java

License:LGPL

private static void loadFile(String lang, JsonElement src) {
    if (!src.isJsonObject())
        return;/* w  ww.j a v  a2 s.c  o  m*/
    JsonObject file = src.getAsJsonObject();

    List<Exception> post = new ArrayList<>();

    try {
        if (file.has("translations")) {
            loadTranslation(lang, "", file.get("translations"), post);
        }
    } catch (Exception e) {
        post.add(e);
    }

    try {
        if (file.has("commands")) {
            JsonElement metaSrc = file.get("meta");

            JsonObject meta = metaSrc.isJsonObject() ? metaSrc.getAsJsonObject() : null;

            file.get("commands").getAsJsonObject().entrySet()
                    .forEach(entry -> loadCommand(lang, entry.getKey(), entry.getValue(), meta, post));
        }
    } catch (Exception e) {
        post.add(e);
    }

    if (post.size() > 0) {
        logger().info("Errors occurred while loading I18nModule:");
        post.forEach(e -> logger().error(e));
    }
}

From source file:cf.adriantodt.David.modules.db.I18nModule.java

License:LGPL

private static void loadCommand(String lang, String name, JsonElement src, JsonObject metadata,
        List<Exception> post) {
    logger().trace(lang + " - " + name + " - " + src.toString());
    if (!src.isJsonObject())
        return;/* w w w.  j  a  v  a 2 s. co  m*/
    JsonObject cmd = src.getAsJsonObject();
    try {
        if (cmd.has("desc") || cmd.has("params") || cmd.has("info")) {
            String desc = cmd.has("desc") ? cmd.get("desc").getAsString()
                    : metadata.get("noDesc").getAsString();
            String params = cmd.has("params") ? cmd.get("params").getAsString()
                    : metadata.get("noParams").getAsString();
            String info = cmd.has("info") ? "\n  " + cmd.get("info").getAsString().replace("\n", "\n  ") : "";
            localize(lang, name + ".usage",
                    desc + "\n" + metadata.get("params").getAsString() + ": " + params + info);
        }
    } catch (Exception e) {
        post.add(e);
    }

    try {
        if (cmd.has("translations")) {
            loadTranslation(lang, name + ".", cmd.get("translations"), post);
        }
    } catch (Exception e) {
        post.add(e);
    }

    try {
        if (cmd.has("subs") && cmd.get("subs").isJsonObject()) {
            cmd.get("subs").getAsJsonObject().entrySet().forEach(
                    entry -> loadCommand(lang, name + "." + entry.getKey(), entry.getValue(), metadata, post));
        }
    } catch (Exception e) {
        post.add(e);
    }
}

From source file:cf.adriantodt.David.modules.db.UserModule.java

License:LGPL

private static Data unpack(JsonElement element) {
    JsonObject object = element.getAsJsonObject();
    Data data = all.stream().filter(dataPredicate -> object.get("id").getAsString().equals(dataPredicate.id))
            .findFirst().orElseGet(Data::new);
    data.id = object.get("id").getAsString();
    data.lang = ConfigUtils.isJsonString(object.get("lang")) ? object.get("lang").getAsString() : null;
    userMap.put(data.getUser(jda), data);
    if (data.getUser(jda) == null) {
        timeoutUntilDbRemoval.put(data, 5);
    }//from w ww .  jav  a  2  s  .  c o  m
    return data;
}