Example usage for com.google.gson JsonObject getAsJsonArray

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

Introduction

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

Prototype

public JsonArray getAsJsonArray(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonArray.

Usage

From source file:net.minecrell.serverlistplus.core.favicon.FaviconHelper.java

License:Open Source License

public static BufferedImage fromSkin(ServerListPlusCore core, UUID uuid, boolean helm) throws IOException {
    JsonObject obj;

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(
            openConnection(core, new URL(SKIN_UUID_URL + toHexString(uuid)), "application/json")))) {
        obj = Helper.JSON.fromJson(reader, JsonObject.class);
    }/*from  ww  w . j  a v a2  s.c o m*/

    JsonArray arr = obj.getAsJsonArray("properties");
    for (JsonElement ele : arr) {
        if (ele instanceof JsonObject) {
            obj = (JsonObject) ele;
            if (!obj.getAsJsonPrimitive("name").getAsString().equals("textures"))
                continue;
            String json = new String(
                    BaseEncoding.base64().decode(obj.getAsJsonPrimitive("value").getAsString()),
                    StandardCharsets.UTF_8);
            obj = Helper.JSON.fromJson(json, JsonObject.class).getAsJsonObject("textures")
                    .getAsJsonObject("SKIN");
            return fromSkin(core, new URL(obj.getAsJsonPrimitive("url").getAsString()), helm);
        }
    }

    throw new IllegalStateException("Skin not found");
}

From source file:net.mrsquirrely.mcsh.MPController.java

License:Open Source License

/**
 * Initializes the controller class./*from w  w  w.ja va 2  s.co  m*/
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // -- Setup Tooltips -------------------------------//
    VersionLabel.setTooltip(new Tooltip("Opens to Github repo."));
    StartServerButton.setTooltip(new Tooltip("Starts the selected server version."));
    DownloadServerButton.setTooltip(new Tooltip("Downloads the selected server version."));
    SettingsButton.setTooltip(
            new Tooltip("Opens the settings page to modify server properties and application settings."));
    PortForwardButton.setTooltip(new Tooltip("This is a beta feature and might not work."));
    ModsButton.setTooltip(new Tooltip("Opens mod manager to lod mods/plugins."));
    ResourcePackButton.setTooltip(new Tooltip("Opens world manager to load a world and a resource pack."));
    MCVersionCombo.setTooltip(new Tooltip("Select a Minecraft server version."));

    // -- Populate MC Version Combo box -----------------//
    try {
        //new FileReader("net/mrsquirrely/mcsh/res/MCVersions.json")
        InputStream in = MCSH.class.getResourceAsStream("res/MCVersions.json");
        Reader fr = new InputStreamReader(in, "utf-8");
        JsonElement JElement = new JsonParser().parse(fr);
        JsonObject JObject = JElement.getAsJsonObject();
        JsonArray JArray = JObject.getAsJsonArray("Versions");
        for (int i = 0; i < JArray.size(); i++) {
            MCVersionCombo.getItems().add(JArray.get(i).getAsString());
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(MPController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:net.osten.watermap.convert.PCTReport.java

License:Open Source License

private Set<WaterReport> parseDocument(JsonObject reportJson) {
    Set<WaterReport> results = new HashSet<WaterReport>();

    log.info("json children=" + reportJson.getAsJsonArray("values").size());

    JsonArray currentRow = null;//from w w  w .j  a  v a 2s .  c om

    for (JsonElement row : reportJson.getAsJsonArray("values")) {
        if (row.isJsonArray()) {
            currentRow = row.getAsJsonArray();
            log.info("row has " + currentRow.size() + "elements");
            if (currentRow.size() >= 6) {
                try {
                    String waypoint = currentRow.get(2).getAsJsonPrimitive().getAsString();
                    String desc = currentRow.get(3).getAsJsonPrimitive().getAsString();
                    String rpt = currentRow.get(4).getAsJsonPrimitive().getAsString();
                    String date = currentRow.get(5).getAsJsonPrimitive().getAsString();

                    log.info("waypoint=" + waypoint);

                    String[] names = waypoint.split(",");
                    for (String name : names) {
                        name = name.trim();

                        WaterReport report = processWaypoint(name, desc, date, rpt);
                        if (report.getLat() == null) {
                            // DEO try prefixing the name (this is for split names: "WR127,B")
                            name = names[0] + name;
                            report = processWaypoint(name, desc, date, rpt);
                            if (report.getLat() == null) {
                                log.info("cannot find coords for " + waypoint);
                            } else {
                                log.info("Adding " + report.toString());
                                results.add(report);
                            }
                        } else {
                            log.info("Adding " + report.toString());
                            results.add(report);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                log.info("skipping row " + row.toString());
            }
        } else {
            log.warning("row is not json array but " + row.getClass().getName());
        }
    }

    log.info("returning " + results.size() + " pct reports");
    return results;
}

From source file:net.rainbowcode.jpixelface.profile.ProfileManager.java

public static Profile getProfileFromUUID(UUID uuid) throws MojangException, IOException {
    String key = RedisKey.PROFILE_UUID.buildKey(uuid.toString());
    if (RedisUtils.exists(key)) {
        return new Profile(RedisUtils.getAsJson(key));
    } else {/*from www.  j  ava 2  s.  co  m*/
        String url = "https://sessionserver.mojang.com/session/minecraft/profile/"
                + uuid.toString().replaceAll("-", "");

        getTicket();

        HttpStringResponse response = HttpUtil.getAsString(url);

        String string = response.getResponse();
        if (response.getCode() != 200) {
            throw new MojangException(url, response.getCode());
        } else {
            JsonParser parser = new JsonParser();
            JsonObject object = parser.parse(string).getAsJsonObject();
            JsonArray properties = object.getAsJsonArray("properties");
            String name = object.getAsJsonPrimitive("name").getAsString();
            JsonObject textures = properties.get(0).getAsJsonObject();
            String value = textures.get("value").getAsString();
            String decoded = new String(Base64.getDecoder().decode(value), "UTF-8");
            JsonObject parse = parser.parse(decoded).getAsJsonObject();
            JsonObject texturesOb = parse.getAsJsonObject("textures");
            final String[] skinUrl = { null };
            final String[] capeUrl = { null };
            if (texturesOb != null) {
                resolve(() -> texturesOb.getAsJsonObject("SKIN").getAsJsonPrimitive("url").getAsString())
                        .ifPresent(x -> skinUrl[0] = x);
                resolve(() -> texturesOb.getAsJsonObject("CAPE").getAsJsonPrimitive("url").getAsString())
                        .ifPresent(x -> capeUrl[0] = x);
            }
            Profile profile = new Profile(name, uuid, skinUrl[0], capeUrl[0]);
            commitProfile(profile);
            return profile;

        }

    }
}

From source file:net.uiqui.couchdb.json.impl.IDListDeserializer.java

License:Apache License

@Override
public ListOf deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    final JsonArray jsonArray = jsonObject.getAsJsonArray("rows");
    final List<JsonElement> elements = new ArrayList<>();

    if (jsonArray != null) {
        for (final JsonElement jsonElement : jsonArray) {
            final JsonObject row = jsonElement.getAsJsonObject();
            final JsonElement id = row.get("id");

            if (id != null) {
                elements.add(id);// www  .  j a v  a  2  s  .  co m
            }
        }
    }

    return new ListOf(elements);
}

From source file:net.uiqui.couchdb.json.impl.QueryResultDeserializer.java

License:Apache License

@Override
public QueryResult deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    final JsonArray jsonArray = jsonObject.getAsJsonArray("docs");
    final List<JsonElement> results = new ArrayList<>();

    if (jsonArray != null) {
        for (final JsonElement jsonElement : jsonArray) {
            results.add(jsonElement);/*from   w w  w .j  av  a 2  s.  co m*/
        }
    }

    return new QueryResult(results);
}

From source file:nl.uva.contextualsuggestion.Ranker.java

public User GetUserInfo(String Line) throws FileNotFoundException, IOException {

    UserInfo ui = new UserInfo(Line);
    String uId = ui.getProfileID();
    String reqId = ui.getRequestID();
    JsonArray jarray1 = ui.getSuggestionCandidates();
    HashSet<String> suggestionCandidatesArray = new HashSet<>();
    for (int i = 0; i < jarray1.size(); i++) {
        String docID = jarray1.get(i).toString().replaceAll("\"", "").trim();
        //Filter non-crowled Docs
        if (iInfo.getIndexId(docID) == null) {
            System.out.println(docID);
            continue;
        }//w  w  w.  j  av  a 2 s .c o  m
        suggestionCandidatesArray.add(docID);
    }

    HashSet<Prefrence> Prefrences = new HashSet<>();
    JsonArray jarray2 = ui.getPreferences();
    for (int i = 0; i < jarray2.size(); i++) {
        Double rating = null;
        JsonObject jobjectIterator = jarray2.get(i).getAsJsonObject();
        String docID = jobjectIterator.get("documentId").toString().replaceAll("\"", "").trim();

        //Filter non-crowled Docs
        if (iInfo.getIndexId(docID) == null) {
            System.out.println(docID);
            continue;
        }

        if (jobjectIterator.get("rating") != null) {
            rating = Double.parseDouble(jobjectIterator.get("rating").toString());
        } else {
            System.out.println("ERR: No rating! for docID: " + docID);
        }

        Prefrence pr = null;
        if (jobjectIterator.getAsJsonArray("tags") == null) {
            pr = new Prefrence(docID, rating);
        } else {
            JsonArray jarray3 = jobjectIterator.getAsJsonArray("tags");
            HashSet<String> tags = new HashSet<>();
            for (int j = 0; j < jarray3.size(); j++) {
                tags.add(jarray3.get(j).toString());
            }
            pr = new Prefrence(docID, rating, tags);
        }
        Prefrences.add(pr);
    }
    User user = new User(reqId, uId, Prefrences, suggestionCandidatesArray);
    return user;
}

From source file:org.aksw.gerbil.bat.annotator.WATAnnotator.java

License:Open Source License

public HashSet<Annotation> solveD2WParams(String text, HashSet<Mention> mentions) throws AnnotationException {
    HashSet<Annotation> res = new HashSet<>();
    JsonObject obj;

    try {//from  w w w  . j  a va  2 s.c o  m
        obj = queryJson(text, mentions, urlD2W);
    } catch (Exception e) {
        throw new AnnotationException("An error occurred while querying WAT API. Message: " + e.getMessage());
    }

    JsonArray jsAnnotations = obj.getAsJsonArray("annotations");
    for (int i = 0; i < jsAnnotations.size(); i++) {
        JsonObject js_ann = jsAnnotations.get(i).getAsJsonObject();

        int start = js_ann.get("start").getAsInt();
        int end = js_ann.get("end").getAsInt();
        int id = js_ann.get("id").getAsInt();

        Mention m = new Mention(start, end - start);
        if (mentions.contains(m))
            res.add(new Annotation(m.getPosition(), m.getLength(), id));
    }
    return res;
}

From source file:org.aksw.gerbil.bat.annotator.WATAnnotator.java

License:Open Source License

@Override
public HashSet<ScoredTag> solveSc2W(String text) throws AnnotationException {
    HashSet<ScoredTag> res = new HashSet<>();
    JsonObject obj;

    try {/* w w w  . j a v a  2s  .  com*/
        obj = queryJson(text, null, urlTag);
    } catch (Exception e) {
        throw new AnnotationException("An error occurred while querying WAT API. Message: " + e.getMessage());
    }

    try {
        JsonArray jsAnnotations = obj.getAsJsonArray("annotations");
        for (int i = 0; i < jsAnnotations.size(); i++) {
            JsonObject js_ann = jsAnnotations.get(i).getAsJsonObject();
            JsonArray jsRanking = js_ann.getAsJsonArray("ranking");
            for (int j = 0; j < jsRanking.size(); j++) {
                JsonObject jsCand = jsRanking.get(j).getAsJsonObject();
                int id = jsCand.get("id").getAsInt();
                double rho = jsCand.get("score").getAsDouble();
                res.add(new ScoredTag(id, (float) rho));
            }
        }
    } catch (Exception e) {
        throw new AnnotationException(e.getMessage());
    }

    return res;
}

From source file:org.aksw.gerbil.bat.annotator.WATAnnotator.java

License:Open Source License

@Override
public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException {
    HashSet<ScoredAnnotation> res = new HashSet<>();
    JsonObject obj;

    try {/*from  w  w  w  .j  a v a2  s  .  c  o m*/
        obj = queryJson(text, null, urlTag);
    } catch (Exception e) {
        throw new AnnotationException("An error occurred while querying WAT API. Message: " + e.getMessage());
    }

    try {
        JsonArray jsAnnotations = obj.getAsJsonArray("annotations");
        for (int i = 0; i < jsAnnotations.size(); i++) {
            JsonObject js_ann = jsAnnotations.get(i).getAsJsonObject();

            int start = js_ann.get("start").getAsInt();
            int end = js_ann.get("end").getAsInt();
            int id = js_ann.get("id").getAsInt();
            double rho = js_ann.get("rho").getAsDouble();

            // Mention m = new Mention(start, end - start);
            res.add(new ScoredAnnotation(start, end - start, id, (float) rho));
        }
    } catch (Exception e) {
        throw new AnnotationException(e.getMessage());
    }
    return res;
}