Example usage for com.google.gson Gson fromJson

List of usage examples for com.google.gson Gson fromJson

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException 

Source Link

Document

This method deserializes the Json read from the specified parse tree into an object of the specified type.

Usage

From source file:br.com.wbrmobile.wsrecicle.resources.PostagemResource.java

@POST
@Consumes(MediaType.APPLICATION_JSON)//from  w  ww.ja  va 2 s .  c o  m
@Path("/inserirpostagem")
public String inserirPostagem(String jsonPostagem) throws Exception {

    Gson gson = new Gson();
    Type postagemType = new TypeToken<Postagem>() {
    }.getType();
    Postagem p = gson.fromJson(jsonPostagem, postagemType);

    ejbFacade.create(p);

    HashMap<String, String> postagemResposta = new HashMap<String, String>();

    postagemResposta.put("estado", CODIGO_EXITO);
    postagemResposta.put("mensagem", "OK");
    postagemResposta.put("id_post", "3");
    JSONObject json = new JSONObject(postagemResposta);
    System.out.println("Json enviado;" + json.toString());
    return json.toString();

}

From source file:br.com.webserviceideias.servlet.CriarAtualizarProjetoServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/json");
    resp.setCharacterEncoding("UTF-8");
    Gson g = new Gson();
    Projeto p = g.fromJson(req.getParameter("projeto"), Projeto.class);
    dao.merge(p);//  w ww . j  a  v  a2  s . c o m
    try (PrintWriter w = resp.getWriter()) {
        w.print(g.toJson(new Informacao("Registro salvo com sucesso")));
        System.out.println(g.toJson(new Informacao("Registro salvo com sucesso")));
    }
}

From source file:br.edu.ifpb.node2.ConexSocket.java

private static Pessoa convertePessoa(String mensagem) {
    Gson g = new Gson();
    return g.fromJson(mensagem, Pessoa.class);
}

From source file:br.edu.ifpb.pdm.chat.eventbus.clients.ContactService.java

private List<Chat> getChats(String response, String responseType) {
    String json = response.replaceAll(responseType, "");
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<Chat>>() {
    }.getType();// w  w w .ja  v a2 s  .  c o  m
    return gson.fromJson(json, type);
}

From source file:br.edu.ifpb.pdm.chat.eventbus.domain.Chat.java

@Override
public Chat fromJson(String json) {
    Gson gson = new Gson();
    return gson.fromJson(json, this.getClass());
}

From source file:br.edu.ifpb.pdm.chat.eventbus.domain.Contact.java

@Override
public Contact fromJson(String json) {
    Gson gson = new Gson();
    return gson.fromJson(json, this.getClass());
}

From source file:br.edu.ifpb.pdm.chat.eventbus.domain.Credentials.java

@Override
public Credentials fromJson(String json) {
    Gson gson = new Gson();
    return gson.fromJson(json, Credentials.class);
}

From source file:br.edu.ifpb.pdm.chat.eventbus.domain.User.java

@Override
public User fromJson(String json) {
    Gson gson = new Gson();
    return gson.fromJson(json, User.class);
}

From source file:br.edu.ifpb.pdm.chat.eventbus.Message.java

@Override
public Message fromJson(String json) {
    Gson gson = new Gson();
    return gson.fromJson(json, this.getClass());
}

From source file:br.edu.ifpb.pos.command.CommandExecute.java

@Override
public void run() throws ResourceException {
    System.out.println("");

    if (line.hasOption("--help")) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.setGroup(options);
        helpFormatter.setDivider(" ");
        helpFormatter.getFullUsageSettings().remove(DisplaySetting.DISPLAY_GROUP_OUTER);
        helpFormatter.print();//w  w w .  ja v  a  2 s  .c  om
    }

    if (line.hasOption("--insert")) {
        Representation representation = new StringRepresentation((CharSequence) line.getValue("--insert"),
                MediaType.APPLICATION_JSON);
        ClientResource clientResource;
        if (line.getValue("--type").equals(TYPE_PERSON)) {
            clientResource = new ClientResource(URL + "/person");
        } else {
            clientResource = new ClientResource(URL + "/user");
        }
        try {
            clientResource.post(representation).write(System.out);
            System.out.println("");
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (line.hasOption("--update")) {
        String entity = (String) line.getValue("--update");
        Representation representation = new StringRepresentation(entity, MediaType.APPLICATION_JSON);
        ClientResource clientResource;
        Gson gson = new Gson();
        if (line.getValue("--type").equals(TYPE_PERSON)) {
            Key key = gson.fromJson(entity, Key.class);
            clientResource = new ClientResource(URL + "/person/" + key.getKey());
        } else {
            Key key = gson.fromJson(entity, Key.class);
            clientResource = new ClientResource(URL + "/user/" + key.getKey());
        }
        try {
            clientResource.put(representation).write(System.out);
            System.out.println("");
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (line.hasOption("--delete")) {
        ClientResource clientResource;
        String key = getKey(line.getValue("--delete").toString());

        if (line.getValue("--type").equals(TYPE_PERSON)) {
            clientResource = new ClientResource(URL + "/person/" + key);
        } else {
            clientResource = new ClientResource(URL + "/user/" + key);

        }
        try {
            clientResource.delete().write(System.out);
            System.out.println("");
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (line.hasOption("--select")) {
        ClientResource clientResource;
        String key = getKey(line.getValue("--select").toString());
        if (line.getValue("--type").equals(TYPE_PERSON)) {
            clientResource = new ClientResource(URL + "/person/" + key);
        } else {
            clientResource = new ClientResource(URL + "/user/" + key);
        }
        Representation representation = clientResource.get();
        try {
            System.out.println(representation.getText());
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}