Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:actions.ListeDemandes.java

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();
    List<DemandeEvenement> le = sm.obtenirDemandesFuturesNonComplet();

    if (le != null) {
        request.setAttribute("ListActivite", le);
    } else {//w  w  w.  j  a va 2s.  c  o m
        request.setAttribute("ListActivite", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    for (DemandeEvenement de : le) {

        JsonObject jsonActivite = new JsonObject();

        jsonActivite.addProperty("id", de.getId());
        jsonActivite.addProperty("denomination", de.getActivity().getDenomination());
        jsonActivite.addProperty("date", de.getDate().toString());
        jsonActivite.addProperty("moment", de.getDay_moment().toString());
        jsonActivite.addProperty("tarif", de.getActivity().getPayant());
        jsonActivite.addProperty("nb_participants", de.getListSize());

        jsonListe.add(jsonActivite);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("activites", jsonListe);

    //Serialisation & Ecriture sur le flux de sortie
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(container);

    return json;
}

From source file:actions.ListeEvenementsAction.java

@Override
public String execute(HttpServletRequest request) {
    ServiceMetier sm = new ServiceMetier();
    List<DemandeEvenement> le = sm.obtenirDemandesFuturesNonComplet(); // TODO : a modifier, ca doit etre le commande complete.

    if (le != null) {
        request.setAttribute("ListActivite", le);
    } else {//from   w  w w.j  a  v  a 2s  .  c o  m
        request.setAttribute("ListActivite", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    for (DemandeEvenement de : le) {

        JsonObject jsonActivite = new JsonObject();

        jsonActivite.addProperty("id", de.getId());
        jsonActivite.addProperty("denomination", de.getActivity().getDenomination());
        jsonActivite.addProperty("date", de.getDate().toString());
        jsonActivite.addProperty("moment", de.getDay_moment().toString());
        jsonActivite.addProperty("tarif", de.getActivity().getPayant());
        jsonActivite.addProperty("nb_participants", de.getListSize());
        jsonActivite.addProperty("nb_max", de.getActivity().getNbParticipants());
        jsonActivite.addProperty("payant", de.getActivity().getPayant());

        jsonListe.add(jsonActivite);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("activites", jsonListe);

    //Serialisation & Ecriture sur le flux de sortie
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(container);

    return json;

}

From source file:actions.ListeLieuxAction.java

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();
    List<Lieu> ll = sm.obtenirLieux();

    if (ll != null) {
        request.setAttribute("ListLieu", ll);
    } else {/* w  w  w .java 2  s . c  o  m*/
        request.setAttribute("ListLieu", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    for (Lieu lieu : ll) {

        JsonObject jsonActivite = new JsonObject();

        jsonActivite.addProperty("id", lieu.getId());
        jsonActivite.addProperty("denomination", lieu.getDenomination());
        jsonActivite.addProperty("lat", lieu.getLatitude());
        jsonActivite.addProperty("lng", lieu.getLongitude());
        jsonActivite.addProperty("adresse", lieu.getAdresse());

        jsonListe.add(jsonActivite);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("lieux", jsonListe);

    //Serialisation & Ecriture sur le flux de sortie
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(container);

    return json;
}

From source file:actions.MomentsAction.java

@Override
public String execute(HttpServletRequest request) {
    ServiceMetier sm = new ServiceMetier();

    List<Moment> lm = sm.obtenirMoments();

    if (lm != null) {
        request.setAttribute("ListMoment", lm);
    } else {/*from  w  w w . jav  a 2s.  c om*/
        request.setAttribute("ListMoment", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    for (Moment moment : lm) {
        JsonObject jsonActivite = new JsonObject();

        jsonActivite.addProperty("id", moment.ordinal());
        jsonActivite.addProperty("denomination", moment.name());

        jsonListe.add(jsonActivite);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("moments", jsonListe);

    //Serialisation & Ecriture sur le flux de sortie
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(container);

    return json;
}

From source file:alexandre.letteridentification.ListAction.java

@Override
public void execute(HttpServletRequest request, PrintWriter out) {
    try {//www.  j  a  v a 2 s  .com
        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.c  om
    }

    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  w w. ja  v a 2  s . co  m*/
    }

    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.ARBindingsResults.java

License:Open Source License

public ARBindingsResults(BindingsResults added, BindingsResults removed) {
    JsonObject nullResults = new JsonObject();
    JsonArray arr = new JsonArray();
    nullResults.add("bindings", arr);

    JsonObject nullHead = new JsonObject();
    nullHead.add("vars", new JsonArray());

    JsonObject addedResults = nullResults;
    JsonObject removedResults = nullResults;
    JsonObject head = nullHead;/*from  w w w .j  a  v a 2 s .c o m*/

    if (added != null) {
        head = added.toJson().get("head").getAsJsonObject();
        addedResults = added.toJson().get("results").getAsJsonObject();
    }

    if (removed != null) {
        head = removed.toJson().get("head").getAsJsonObject();
        removedResults = removed.toJson().get("results").getAsJsonObject();
    }

    results.add("addedresults", addedResults);
    results.add("removedresults", removedResults);
    results.add("head", head);
}

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  .j  a v a 2  s . com*/
    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);
}