Example usage for com.google.gson GsonBuilder GsonBuilder

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

Introduction

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

Prototype

public GsonBuilder() 

Source Link

Document

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings.

Usage

From source file:abtlibrary.utils.as24ApiClient.JSON.java

License:Apache License

/**
 * JSON constructor.// w  w  w  .  j a  v  a 2s.  c  o m
 *
 * @param apiClient An instance of ApiClient
 */
public JSON(ApiClient apiClient) {
    this.apiClient = apiClient;
    gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateAdapter(apiClient))
            .registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
            .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter()).create();
}

From source file:actions.ActivitesAction.java

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();

    List<Activite> la = null;
    if (sm != null) {
        la = sm.obtenirActivites();//from   w w  w. java  2s .c  om
    }
    if (la != null) {
        request.setAttribute("ListActivite", la);
    } else {
        request.setAttribute("ListActivite", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    for (Activite act : la) {
        JsonObject jsonActivite = new JsonObject();

        jsonActivite.addProperty("id", act.getId());
        jsonActivite.addProperty("denomination", act.getDenomination());

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

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();
    Adherent adherent = sm.connexion(request.getParameter("email"));

    JsonObject jsonResponse = new JsonObject();
    if (adherent == null) { // Connexion chou
        jsonResponse.addProperty("Connexion", "KO");
    } else {//from  w  ww  .  j av  a  2s. co m
        jsonResponse.addProperty("Connexion", "OK");
        HttpSession session = request.getSession();
        session.setAttribute("Id", adherent.getId());
        session.setAttribute("Email", adherent.getMail());
    }

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

From source file:actions.DetailsAction.java

@Override
public String execute(HttpServletRequest request) {
    ServiceMetier sm = new ServiceMetier();
    HttpSession session = request.getSession();
    Adherent adherent = sm.connexion((String) session.getAttribute("Email"));
    List<DemandeEvenement> lde = sm.obtenirDemandesPerso(adherent, true);
    DemandeEvenement demande = null;/*  ww  w  .  j  av  a2 s. co  m*/

    JsonObject jsonResponse = new JsonObject();

    for (DemandeEvenement de : lde) {
        long id = Long.parseLong(request.getParameter("id"));
        if (de.getId() == id) {
            demande = de;
            break;
        }
    }

    if (demande == null) {
        return "";
    }

    Evenement event = sm.obtenirEvenement(demande);
    if (event == null) {
        return "";
    }

    if (event.getLieu() != null) {
        jsonResponse.addProperty("lieu", event.getLieu().getDenomination());
    } else {
        jsonResponse.addProperty("lieu", "-");
    }

    if (event instanceof EvenementPayant) {
        EvenementPayant ep = (EvenementPayant) event;
        jsonResponse.addProperty("prix", ep.getPAF());
    } else {
        jsonResponse.addProperty("prix", "-");
    }

    jsonResponse.addProperty("activite", demande.getActivity().getDenomination());
    jsonResponse.addProperty("date", demande.getDate().toString());
    jsonResponse.addProperty("moment", demande.getDay_moment().toString());

    //demande.getList_adher();
    JsonArray jsonListe = new JsonArray();

    for (Adherent ad : demande.getList_adher()) {
        JsonObject jsonPersonne = new JsonObject();
        jsonPersonne.addProperty("id", ad.getId());
        jsonPersonne.addProperty("nom", ad.getNom());
        jsonPersonne.addProperty("prenom", ad.getPrenom());
        jsonListe.add(jsonPersonne);
    }

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

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

    return json;
}

From source file:actions.HistoriqueAction.java

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

    HttpSession session = request.getSession();
    Adherent adherent = sm.connexion((String) session.getAttribute("Email"));
    List<DemandeEvenement> lde = sm.obtenirDemandesPerso(adherent, false);

    JsonArray jsonListe = new JsonArray();

    for (DemandeEvenement de : lde) {
        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());
        if (de.getEvent() != null) {
            jsonActivite.addProperty("etat", de.getEvent().isValidated());
        } else {//  ww  w . j  a  v  a2  s.  c  o m
            jsonActivite.addProperty("etat", "NULL");
        }
        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.InscriptionAction.java

@Override
public String execute(HttpServletRequest request) {
    ServiceMetier sm = new ServiceMetier();
    Adherent adherent = new Adherent(request.getParameter("nom"), request.getParameter("prenom"),
            request.getParameter("email"), request.getParameter("adresse"));
    Adherent result = sm.inscription(adherent);
    JsonObject jsonResponse = new JsonObject();

    if (result == null) { // Inscription pas OK
        jsonResponse.addProperty("Inscription", "KO");
    } else {//ww w.  ja va  2s . c  o  m
        jsonResponse.addProperty("Inscription", "OK");
        HttpSession session = request.getSession();
        session.setAttribute("Id", adherent.getId());
        session.setAttribute("Email", adherent.getMail());
    }

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

From source file:actions.InsertDemandeAction.java

@Override
public String execute(HttpServletRequest request) {
    System.out.println("\n\n-----------------Demande-------------\n\n");
    ServiceMetier sm = new ServiceMetier();

    HttpSession session = request.getSession();
    Adherent adherent = sm.connexion((String) session.getAttribute("Email"));
    DemandeEvenement result = null;/*from  ww w  .j  a va  2  s.co  m*/
    JsonObject jsonResponse = new JsonObject();
    if (adherent != null) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        Date date = null;
        try {
            System.out.println("DATE:" + (String) request.getParameter("date"));
            date = formatter.parse((String) request.getParameter("date"));
            System.out.println("-----------------DATE-------------");
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        long activite_id = Long.parseLong((String) request.getParameter("activite"));
        List<Activite> activites = sm.obtenirActivites();
        Activite activite = null;
        for (Activite act : activites) {
            if (act.getId() == activite_id) {
                activite = act;
                break;
            }
        }

        int int_moment = Integer.parseInt((String) request.getParameter("moment"));
        Moment moment = null;
        switch (int_moment) {
        case 0:
            moment = Moment.matin;
            break;
        case 1:
            moment = Moment.apres_midi;
            break;
        case 2:
            moment = Moment.soir;
            break;
        default:
            moment = Moment.matin;
            break;
        }
        System.out.println("-----RESUME-------");
        System.out.println(date);
        System.out.println(moment);
        System.out.println(activite);
        System.out.println(adherent);
        result = sm.nouvelleDemandeEvenement(date, moment, activite, adherent);
    }

    if (result == null) { // Demande pas OK
        System.out.println("KO");
        jsonResponse.addProperty("Demande", "KO");
    } else {
        System.out.println("OK");
        jsonResponse.addProperty("Demande", "OK");
    }

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

From source file:actions.ListeAdherentsEvenement.java

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

    if (le != null) {
        request.setAttribute("ListActivite", le);
    } else {/*from w ww  .  java  2 s . co  m*/
        request.setAttribute("ListActivite", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    int id = Integer.parseInt(request.getParameter("id"));

    //Trouve la liste des adherents depuis l'id de l'event
    for (Evenement de : le) {
        if (de.getId() == id) {
            la = de.getDemandeAboutie().getList_adher();
            break;
        }
    }

    for (Adherent a : la) {
        JsonObject jsonAdherent = new JsonObject();

        jsonAdherent.addProperty("id", a.getId());
        jsonAdherent.addProperty("prenom", a.getPrenom());
        jsonAdherent.addProperty("nom", a.getNom());
        jsonAdherent.addProperty("latitude", a.getLatitude());
        jsonAdherent.addProperty("longitude", a.getLongitude());

        jsonListe.add(jsonAdherent);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("adherents", 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.ListeAdminAction.java

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();
    List<Evenement> le = sm.obtenirEvenementAValider(); // TODO : a modifier, ca doit etre le commande complete.

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

    JsonArray jsonListe = new JsonArray();

    int id = -1;
    if (request.getParameter("id") != "")
        id = Integer.parseInt(request.getParameter("id"));

    if (id == -1) {
        for (Evenement de : le) {
            JsonObject jsonActivite = new JsonObject();

            jsonActivite.addProperty("id", de.getId());
            jsonActivite.addProperty("denomination", de.getDemandeAboutie().getActivity().getDenomination());
            jsonActivite.addProperty("date", de.getDemandeAboutie().getDate().toString());
            jsonActivite.addProperty("moment", de.getDemandeAboutie().getDay_moment().toString());
            jsonActivite.addProperty("nb_participants", de.getDemandeAboutie().getListSize());
            jsonActivite.addProperty("payant", de.estPayant());
            jsonListe.add(jsonActivite);
        }
    } else {
        JsonObject jsonActivite = new JsonObject();

        for (Evenement de : le) {
            if (de.getId() == id) {

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

                jsonListe.add(jsonActivite);
                break;
            }
        }
    }

    //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.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 {//from  w  ww. ja 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;
}