Example usage for com.google.gson JsonObject addProperty

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

Introduction

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

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

From source file:br.com.bean.Utilitarios.CriadorJson.java

public static String criaJsonErro(Exception e, String mensagem) {
    JsonObject jsonMensagemErro = new JsonObject();
    jsonMensagemErro.addProperty("successo", "false");
    jsonMensagemErro.addProperty("mensagem", mensagem);
    jsonMensagemErro.addProperty("exception", e.getMessage());
    Gson gsonCorreto = new Gson();
    return gsonCorreto.toJson(jsonMensagemErro);
}

From source file:br.com.bean.Utilitarios.CriadorJson.java

public static String criaJsonSucesso(String mensagem) {
    JsonObject jsonMensagemSucesso = new JsonObject();
    jsonMensagemSucesso.addProperty("successo", "true");
    jsonMensagemSucesso.addProperty("mensagem", mensagem);
    Gson gsonCorreto = new Gson();
    return gsonCorreto.toJson(jsonMensagemSucesso);
}

From source file:br.com.caelum.vraptor.serialization.gson.MessageGsonConverter.java

License:Open Source License

@Override
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("category", src.getCategory());
    json.addProperty("message", src.getMessage());

    return json;/*from   ww  w.j av  a 2s. com*/
}

From source file:br.com.saude.api.social.gcm.GcmServer.java

License:Open Source License

/**
 * Send messages to recipient via GCM./* ww w .j a  v a  2  s  .  c  o m*/
 *
 * @param to Message recipient.
 * @param message Message to be sent.
 */
public void send(String to, JsonObject message) {
    message.addProperty("to", to);
    /**
     * Message ID generated as a remainder of current time in milliseconds. You could use any
     * method of unique ID generation here.
     */
    message.addProperty("message_id", (Calendar.getInstance().getTimeInMillis()) + "");

    final String payload = gson.toJson(message);
    Stanza stanza = new Stanza() {
        @Override
        public CharSequence toXML() {
            return wrapWithXML(payload);
        }
    };

    logger.info("sending msg: " + stanza);
    smackCcsClient.sendStanza(stanza);
}

From source file:br.com.saude.api.social.gcm.GcmServer.java

License:Open Source License

/**
 * Send Ack message back to CCS to acknowledged the receipt of the message with ID msg_id.
 *
 * @param to Registration token of the sender of the message being acknowledged.
 * @param msg_id ID of message being acknowledged.
 *///  w  w w.  j  av  a  2 s .c  o m
private void sendAck(String to, String msg_id) {
    JsonObject jPayload = new JsonObject();
    jPayload.addProperty("to", to);
    jPayload.addProperty("message_id", msg_id);
    jPayload.addProperty("message_type", "ack");

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final String payload = gson.toJson(jPayload);
    Stanza stanza = new Stanza() {
        @Override
        public CharSequence toXML() {
            return wrapWithXML(payload);
        }
    };

    logger.info("sending ack: " + stanza);
    smackCcsClient.sendStanza(stanza);
}

From source file:br.edu.ifc.fraiburgo.rubble.servlets.StatusServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setCharacterEncoding("utf8");

    String start = request.getParameter("start");
    String limit = request.getParameter("limit");

    PrintWriter out = response.getWriter();
    response.setContentType("application/json");
    response.setHeader("Cache-control", "no-cache, no-store");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "-1");
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    response.setHeader("Access-Control-Max-Age", "86400");

    boolean sucess = false;
    JsonArray arrayObj = new JsonArray();
    JsonObject myObj = new JsonObject();
    List<Comentario> comments = new ArrayList();
    if (request.getParameter("action").equals("change")) {
        try {/*from  w  ww . ja v  a 2s .c  o  m*/
            Postagem p = new PostagemController()
                    .getPostagemById(Integer.parseInt(request.getParameter("idPostagem")));
            Usuario u = new UsuarioController()
                    .getUsuarioById(Integer.parseInt(request.getParameter("idUsuario")));
            Status s = new Status();
            s.setAtivo(true);
            s.setData(new Date());
            s.setIdPostagem(p);
            s.setIdUsuario(u);
            s.setStatus(request.getParameter("status"));
            s = new StatusController().mergeStatus(s);
            new StatusController().setAllStatusInactive(s.getIdStatus(), p);
            s.setIdPostagem(null);
            s.setIdUsuario(null);
            Gson gson = new Gson();
            JsonElement statusObj = gson.toJsonTree(s);
            myObj.add("status", statusObj);
            sucess = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    myObj.addProperty("success", sucess);
    out.println(myObj.toString());
    out.close();
}

From source file:br.edu.ifpe.garanhuns.pos.sysadvogacia.controladores.ControladorAdvogadoServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from  w  w  w .  jav  a2s.c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userPath = request.getServletPath();
    NegocioAdvogado negocioAdvogado = new NegocioAdvogado();
    Advogado advogado;

    if (userPath.equals("/SalvarAdvogado")) {
        advogado = new Advogado();
        if (!request.getParameter("codigo").isEmpty()) {
            advogado.setCodigo(Integer.parseInt(request.getParameter("codigo")));
        } else {
            advogado.setCodigo(0);
        }
        advogado.setNome(request.getParameter("nome"));
        advogado.setCpf(request.getParameter("cpf"));
        advogado.setOab(request.getParameter("oab"));
        advogado.setEndereco(request.getParameter("endereco"));
        advogado.setTelefone(request.getParameter("telefone"));

        String json = new Gson().toJson(negocioAdvogado.salvar(advogado));
        response.getWriter().print(json);

    }

    if (userPath.equals("/ListarAdvogados")) {
        String json = new Gson().toJson(negocioAdvogado.listarAdvogados());
        response.getWriter().print(json);
    }

    if (userPath.equals("/RemoverAdvogado")) {
        String id = request.getParameter("id");
        advogado = negocioAdvogado.advogadoPorCodigo(Integer.parseInt(id));
        negocioAdvogado.remover(advogado);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("success", "true");
        response.getWriter().print(new Gson().toJson(jsonObject));
    }

}

From source file:br.edu.ifpe.garanhuns.pos.sysadvogacia.controladores.ControladorClienteServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* w  ww.j  a v  a 2s  .c o m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userPath = request.getServletPath();
    NegocioCliente negocioCliente = new NegocioCliente();
    Cliente cliente;

    if (userPath.equals("/SalvarCliente")) {
        cliente = new Cliente();
        if (!request.getParameter("codigo").isEmpty()) {
            cliente.setCodigo(Integer.parseInt(request.getParameter("codigo")));
        } else {
            cliente.setCodigo(0);
        }
        cliente.setNome(request.getParameter("nome"));
        cliente.setCpfCnpj(request.getParameter("cpfCnpj"));
        cliente.setEndereco(request.getParameter("endereco"));
        cliente.setTelefone(request.getParameter("telefone"));

        String json = new Gson().toJson(negocioCliente.salvar(cliente));
        response.getWriter().print(json);

    }

    if (userPath.equals("/ListarClientes")) {
        Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {

            public boolean shouldSkipClass(Class<?> clazz) {
                return (clazz == Processo.class);
            }

            /**
             * Custom field exclusion goes here
             */
            public boolean shouldSkipField(FieldAttributes f) {
                return false;
            }

        })
                /**
                 * Use serializeNulls method if you want To serialize null
                 * values By default, Gson does not serialize null values
                 */
                .serializeNulls().create();

        String json = gson.toJson(negocioCliente.listarClientes());
        response.getWriter().print(json);
    }

    if (userPath.equals("/RemoverCliente")) {
        JsonObject jsonObject = new JsonObject();
        try {
            String id = request.getParameter("id");
            cliente = negocioCliente.clientePorCodigo(Integer.parseInt(id));
            negocioCliente.remover(cliente);
            jsonObject.addProperty("success", "true");
            response.getWriter().print(new Gson().toJson(jsonObject));
        } catch (RemoverClienteComProcessosException e) {
            jsonObject.addProperty("errorMsg", e.mensagem());
            response.getWriter().print(new Gson().toJson(jsonObject));
        }

    }

}

From source file:br.edu.ifpe.garanhuns.pos.sysadvogacia.controladores.ControladorLeiServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w ww. ja v a  2 s . c o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userPath = request.getServletPath();

    NegocioLei negocioLei = new NegocioLei();

    Processo proc = new Processo();
    proc.setCodigo(1);

    Lei lei;

    if (userPath.equals("/SalvarLei")) {

        lei = new Lei();

        if (!request.getParameter("codigo").isEmpty()) {
            lei.setCodigo(Integer.parseInt(request.getParameter("codigo")));
        } else {
            lei.setCodigo(0);
        }
        lei.setArtigo(Integer.parseInt(request.getParameter("artigo")));
        lei.setDescricao(request.getParameter("descricao"));
        lei.setTipo(request.getParameter("tipo"));
        lei.setCapitulo(Integer.parseInt(request.getParameter("capitulo")));

        lei.setProcessoCodigo(proc);

        String json = new Gson().toJson(negocioLei.salvar(lei));

        response.getWriter().print(json);
    }

    if (userPath.equals("/ListarLeis")) {
        String json = new Gson().toJson(negocioLei.listarLeis());
        response.getWriter().print(json);
    }
    if (userPath.equals("/RemoverLei")) {
        String id = request.getParameter("id");
        lei = negocioLei.LeiPorCodigo(Integer.parseInt(id));
        negocioLei.remover(lei);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("sucess", "true");
        response.getWriter().print(new Gson().toJson(jsonObject));
    }

}

From source file:br.edu.ifpe.garanhuns.pos.sysadvogacia.controladores.ControladorProcessoServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   ww  w . j  a v  a  2  s  .  c o m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userPath = request.getServletPath();
    NegocioProcesso negocioProcesso = new NegocioProcesso();
    Processo processo;

    if (userPath.equals("/SalvarProcesso")) {
        processo = new Processo();
        if (!request.getParameter("codigo").isEmpty()) {
            processo.setCodigo(Integer.parseInt(request.getParameter("codigo")));
        }

        processo.setDataAbertura(new Date(request.getParameter("dataAbertura")));
        processo.setInstanciaAtual(request.getParameter("instanciaAtual"));
        processo.setStatus(Integer.parseInt(request.getParameter("status")));
        processo.setDecisaoFinal(request.getParameter("decisaoFinal"));
        processo.setDescricao(request.getParameter("descricao"));

        String json = new Gson().toJson(negocioProcesso.salvar(processo));
        response.getWriter().print(json);

    }

    if (userPath.equals("/ListarProcessos")) {
        String json = new Gson().toJson(negocioProcesso.listarProcessos());
        response.getWriter().print(json);
    }

    if (userPath.equals("/RemoverProcesso")) {
        String id = request.getParameter("id");
        processo = negocioProcesso.processoPorCodigo(Integer.parseInt(id));
        negocioProcesso.remover(processo);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("success", "true");
        response.getWriter().print(new Gson().toJson(jsonObject));
    }

}