Example usage for com.google.gson JsonArray add

List of usage examples for com.google.gson JsonArray add

Introduction

In this page you can find the example usage for com.google.gson JsonArray add.

Prototype

public void add(JsonElement element) 

Source Link

Document

Adds the specified element to self.

Usage

From source file:alexandre.letteridentification.ListAction.java

@Override
public void execute(HttpServletRequest request, PrintWriter out) {
    try {/*  w  w  w.ja v a  2 s .  c o m*/
        String type = request.getParameter("type");

        JsonArray list = new JsonArray();

        switch (type) {
        case "images_to_be_analyzed":

            String path = ActionServlet.path + "\\Images to be analyzed";

            File[] letters = new File(path).listFiles();

            for (int i = 0; i < letters.length; i++) {
                JsonObject letter_obj = new JsonObject();
                letter_obj.addProperty("letter", letters[i].getName());

                File[] drawings = new File(letters[i].getAbsolutePath() + "\\Drawings").listFiles();

                JsonArray list_drawings = new JsonArray();

                for (int j = 0; j < drawings.length; j++) {
                    BufferedImage im = ImageIO.read(drawings[j]);

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();

                    ImageIO.write(im, "png", baos);
                    baos.flush();
                    byte[] data = baos.toByteArray();

                    baos.close();

                    String encodedImage = "data:image/png;base64," + Base64.getEncoder().encodeToString(data);

                    list_drawings.add(encodedImage);
                }

                letter_obj.add("drawings", list_drawings);

                File[] computer_vision = new File(letters[i].getAbsolutePath() + "\\Computer Vision")
                        .listFiles();

                JsonArray list_computer = new JsonArray();

                for (int j = 0; j < computer_vision.length; j++) {
                    BufferedImage im = ImageIO.read(computer_vision[j]);

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();

                    ImageIO.write(im, "png", baos);
                    baos.flush();
                    byte[] data = baos.toByteArray();

                    baos.close();

                    String encodedImage = "data:image/png;base64," + Base64.getEncoder().encodeToString(data);

                    list_computer.add(encodedImage);
                }

                letter_obj.add("computer_vision", list_computer);

                list.add(letter_obj);
            }

            break;
        }

        JsonObject container = new JsonObject();
        container.add("list", list);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(container);
        out.println(json);
    } catch (IOException ex) {
        Logger.getLogger(ListAction.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:allout58.mods.techtree.tree.TechNodeGSON.java

License:Open Source License

@Override
public JsonElement serialize(TechNode src, Type typeOfSrc, JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("id", src.getId());
    jsonObject.addProperty("name", src.getName());
    jsonObject.addProperty("scienceRequired", src.getScienceRequired());
    jsonObject.addProperty("description", src.getDescription());

    final JsonArray jsonParents = new JsonArray();
    for (final TechNode parent : src.getParents()) {
        final JsonPrimitive jsonParent = new JsonPrimitive(parent.getId());
        jsonParents.add(jsonParent);
    }//from w  w w .j  a  v a2 s. co  m

    jsonObject.add("parents", jsonParents);

    final JsonArray jsonItems = new JsonArray();
    for (final ItemStack item : src.getLockedItems()) {
        jsonItems.add(context.serialize(item));
    }

    jsonObject.add("lockedItems", jsonItems);

    return jsonObject;
}

From source file:api.PersonEndpoint.java

@GET
@Produces("application/json")
public String getPersons() {
    List<Person> persons = pf.getPersons();
    JsonArray personArray = new JsonArray();
    for (Person p : persons) {

        JsonObject job = JsonBuilder.getCompletePerson(p);
        personArray.add(job);
    }/*from  w ww. j av  a  2  s.  c  om*/

    return gson.toJson(personArray);
}

From source file:arces.unibo.SEPA.commons.request.RegistrationRequest.java

License:Open Source License

public RegistrationRequest(String id) {
    json.add("client_identity", new JsonPrimitive(id));
    JsonArray grants = new JsonArray();
    grants.add("client_credentials");
    json.add("grant_types", grants);
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

public BindingsResults(Set<String> varSet, List<Bindings> solutions) {
    results = new JsonObject();

    JsonObject vars = new JsonObject();
    JsonArray varArray = new JsonArray();
    if (varSet != null)
        for (String var : varSet) {
            varArray.add(var);
        }//from   w w  w. jav  a  2  s  .  c  o m
    vars.add("vars", varArray);
    results.add("head", vars);

    JsonArray bindingsArray = new JsonArray();
    if (solutions != null)
        for (Bindings solution : solutions)
            bindingsArray.add(solution.toJson());
    JsonObject bindings = new JsonObject();
    bindings.add("bindings", bindingsArray);
    results.add("results", bindings);
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

public BindingsResults(BindingsResults newBindings) {
    results = new JsonObject();

    JsonObject vars = new JsonObject();
    JsonArray varArray = new JsonArray();
    if (newBindings != null)
        for (String var : newBindings.getVariables()) {
            varArray.add(var);
        }//  ww  w  . ja  v a 2 s .  c o  m
    vars.add("vars", varArray);
    results.add("head", vars);

    JsonArray bindingsArray = new JsonArray();
    if (newBindings != null)
        for (Bindings solution : newBindings.getBindings())
            bindingsArray.add(solution.toJson());
    JsonObject bindings = new JsonObject();
    bindings.add("bindings", bindingsArray);
    results.add("results", bindings);
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

public void add(Bindings binding) {
    if (binding == null)
        return;//from  w  ww.  j  ava  2s  .  c  o m
    JsonArray bindings = getBindingsArray();
    if (bindings == null)
        return;

    bindings.add(binding.toJson());
}

From source file:assignment3.Assignment3.java

public static String getJSON() {
    String output = "";
    try {/*from ww w  . j  ava 2 s . co m*/
        JsonArray arr = new JsonArray();
        Connection conn = getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM students");
        while (rs.next()) {
            JsonObject obj = new JsonObject();
            obj.addProperty("id", rs.getInt("id"));
            obj.addProperty("fName", rs.getString("fName"));
            obj.addProperty("lName", rs.getString("lName"));
            obj.addProperty("bio", rs.getString("bio"));
            arr.add(obj);
        }
        output = arr.toString();
        conn.close();
    } catch (SQLException ex) {
        output = "SQL Exception Error: " + ex.getMessage();
    }
    return output;
}

From source file:assignment3.Assignment3.java

public static String getJSON(int id) {
    String output = "";
    try {/*from  ww  w. j a v  a 2 s  .com*/
        JsonArray arr = new JsonArray();
        Connection conn = getConnection();
        String query = "SELECT * FROM students WHERE id = ?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            JsonObject obj = new JsonObject();
            obj.addProperty("id", rs.getInt("id"));
            obj.addProperty("fName", rs.getString("fName"));
            obj.addProperty("lName", rs.getString("lName"));
            obj.addProperty("bio", rs.getString("bio"));
            arr.add(obj);
        }
        output = arr.toString();
        conn.close();
    } catch (SQLException ex) {
        output = "SQL Exception Error: " + ex.getMessage();
    }
    return output;
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonListTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(List<?> object, TypeMetadata<JsonElement> typeMetadata) {
    JsonArray jsonArray = new JsonArray();
    TypeAdapter typeAdapter = typeMetadata.getTypeParameterTypeAdapters().get(0);
    for (Object item : object) {
        JsonElement jsonElement = (JsonElement) typeAdapter.serialize(item, typeMetadata);
        jsonArray.add(jsonElement);
    }//w w  w  .ja v  a 2  s.c  o  m
    return jsonArray;
}