Example usage for com.google.gson.reflect TypeToken TypeToken

List of usage examples for com.google.gson.reflect TypeToken TypeToken

Introduction

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

Prototype

@SuppressWarnings("unchecked")
protected TypeToken() 

Source Link

Document

Constructs a new type literal.

Usage

From source file:client.ClientEnd.java

public void transferRooms(JSONObject allRooms) {
    System.out.println("Recieved from server, rooms: " + allRooms);
    String rooms = allRooms.getString("AllRooms");
    Type type = new TypeToken<ArrayList<Room>>() {
    }.getType();/*w w w.j a  va2s .  com*/
    ArrayList<Room> roomsFromServer = gson.fromJson(rooms, type);

    DeleteRoomController.setRooms(roomsFromServer);
    //DeleteRoomController.displayRooms();
    //ClientController.usersBookedRooms = usersBookedRooms;
    //ClientController.switchView();
}

From source file:client.ClientEnd.java

private void transferModules(JSONObject allModules) {
    System.out.println("Recieved from server, rooms: " + allModules);
    String modules = allModules.getString("AllModules");
    Type type = new TypeToken<ArrayList<Module>>() {
    }.getType();// ww w  .j a v  a 2 s  .c  om
    ArrayList<Module> modulesFromServer = gson.fromJson(modules, type);

    DeleteModuleController.setModules(modulesFromServer);
}

From source file:client.ClientEnd.java

private void transferUsers(JSONObject jObject) {
    String message = jObject.getString("AllUsers");
    Type type = new TypeToken<ArrayList<User>>() {
    }.getType();//from   w w  w.  j a  v a2 s.co m
    ArrayList<User> usersRecieved = gson.fromJson(message, type);
    for (User user : usersRecieved) {
        System.out.println(user);
        //ClientController.bookings.getItems().add(room);
    }
    DeleteUserController.setUsers(usersRecieved);
}

From source file:client.communication.ClientCommunicator.java

/**
 * The Http Get Method. It sends a request to a server WITH PARAMETERS and
 * gets a response./*from   w ww .  ja v  a 2 s  .  c om*/
 *
 * @param commandName the name mapped to the corresponding handler in the
 * server on the other side.
 * @param params the parameters pass for the specified web call
 * @param playerID
 * @return the response generated during an http communication. The response
 * object is user defined and not part the java api.
 * @throws client.main.ClientException
 */
public HttpURLResponse doGet(String commandName, String params, int playerID) throws ClientException {

    assert commandName != null && commandName.length() > 0;

    HttpURLResponse result = new HttpURLResponse();

    try {
        URL url = new URL(URL_PREFIX + "/" + commandName + params);

        if (commandName.contains("model")) {
            //System.out.println(url);
        }
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(HTTP_GET);
        //set cookie
        if (playerID > -1) {
            if (playerIdThisOne != -1) {
                playerID = playerIdThisOne;
            }
            connection.setRequestProperty("Cookie", cookies.get(playerID));

        }
        connection.connect();

        result.setResponseCode(connection.getResponseCode());
        result.setResponseLength(connection.getContentLength());
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            if (connection.getContentLength() != -1) {
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String content = br.readLine();
                //only games/list
                if (commandName.equals("games/list")) {
                    //                    if (commandName.equals("games/list") || commandName.equals("game/commands")) {
                    Type listType = new TypeToken<ArrayList<GameInfo>>() {
                    }.getType();
                    result.setResponseBody(model.fromJson(content, listType));
                } else if (commandName.equals("game/commands")) {
                    Type listType = new TypeToken<ArrayList<GameInfo>>() {
                    }.getType();
                    result.setResponseBody(model.fromJson(content, listType));
                } else if (commandName.equals("game/listAI")) {
                    Type listType = new TypeToken<ArrayList<String>>() {
                    }.getType();
                    result.setResponseBody(model.fromJson(content, listType));
                } else {
                    if (content.equals("\"true\"")) {
                        result.setResponseBody(new GameInfo(content));
                    } else {
                        result.setResponseBody(model.fromJson(content, GameInfo.class));
                    }
                }
            }
        } else {
            String content = "Failed"; //br.readLine();
            result.setResponseBody(content);
        }
    } catch (IOException e) {
        throw new ClientException(String.format("doGet failed: %s", e.getMessage()), e);
    }
    return result;
}

From source file:client.model.Application.java

public static String toJson(List<Application> list) {
    Gson gson = new GsonBuilder().setDateFormat(Utility.DATE_FORMAT_STRING_SHORT).create();
    String gsonString = gson.toJson(list, new TypeToken<List<Application>>() {
    }.getType());// ww  w .j a v  a 2 s.  c  om
    return gsonString;
}

From source file:client.model.Application.java

public static List<Application> fromJson(String json) throws JsonSyntaxException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new Utility.JsonDateDeserializer()).create();
    List<Application> list = gson.fromJson(json, new TypeToken<List<Application>>() {
    }.getType());//from   w w w .j  ava 2  s .  co m
    return list;
}

From source file:client.model.User.java

public static String toJsonUser(List<User> list) {
    Gson gson = new GsonBuilder().setDateFormat(Utility.DATE_FORMAT_STRING_SHORT).create();
    String gsonString = gson.toJson(list, new TypeToken<List<User>>() {
    }.getType());//w ww.  j a  v a  2s .  c  o m
    return gsonString;
}

From source file:client.model.User.java

public static List<User> fromJsonUser(String json) throws JsonSyntaxException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new Utility.JsonDateDeserializer()).create();
    List<User> list = gson.fromJson(json, new TypeToken<List<User>>() {
    }.getType());// w w w .j a  va  2  s.co  m
    return list;
}

From source file:clienterest.ClienteProduto.java

public List<Produto> listar() throws ClientErrorException {
    WebTarget resource = webTarget;/*w  ww  .  j a  v a  2s.co m*/
    String json = resource.request(MediaType.APPLICATION_JSON).get(String.class);
    Gson gson = new Gson();
    Type type = new TypeToken<List<Produto>>() {
    }.getType();
    List<Produto> listaProdutos = (List<Produto>) gson.fromJson(json, type);
    return listaProdutos;
}

From source file:clienterest.ClienteUsuario.java

public List<Usuario> listar() throws ClientErrorException {
    WebTarget resource = webTarget;//from  www .j av a 2s.  c  o  m
    String json = resource.request(MediaType.APPLICATION_JSON).get(String.class);
    Gson gson = new Gson();
    Type type = new TypeToken<List<Usuario>>() {
    }.getType();
    List<Usuario> listaUsuarios = (List<Usuario>) gson.fromJson(json, type);
    return listaUsuarios;
}