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:app24.feedbook.hk.utils.Utils.java

License:Apache License

public UserFeedModel getUserFeedPreferences(Activity activity) {
    SharedPreferences sharedPref = activity
            .getSharedPreferences(activity.getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPref.getString(Constants.LATEST_FEED_MODEL, "");
    UserFeedModel feedsModel = gson.fromJson(json, UserFeedModel.class);
    return feedsModel;
}

From source file:appeng.services.version.github.ReleaseFetcher.java

License:Open Source License

public FormattedRelease get() {
    final Gson gson = new Gson();
    final Type type = new ReleasesTypeToken().getType();

    try {//from   w  w  w  .  j  a va2 s.  co  m
        final URL releasesURL = new URL(GITHUB_RELEASES_URL);
        final String rawReleases = this.getRawReleases(releasesURL);

        this.config.updateLastCheck();

        final List<Release> releases = gson.fromJson(rawReleases, type);
        final FormattedRelease latestFitRelease = this.getLatestFitRelease(releases);

        return latestFitRelease;
    } catch (final VersionCheckerException e) {
        AELog.debug(e);
    } catch (final MalformedURLException e) {
        AELog.debug(e);
    } catch (final IOException e) {
        AELog.debug(e);
    }

    return EXCEPTIONAL_RELEASE;
}

From source file:Application.Services.ServicoSX.java

@POST
@Path("/calcularValores")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED })
@Produces(MediaType.APPLICATION_JSON)/*from w ww .  j  a  v  a2  s. c om*/
public RetornoJsonDTO calcularValores(String lista, String problema) {
    try {
        Util u = new Util();
        List<List<String>> listal = new ArrayList<List<String>>();
        JSONObject request = new JSONObject(lista);
        JSONArray arr = request.getJSONArray("vetor");
        Gson gson = new Gson();
        TipoProblemaDTO tp = gson.fromJson(request.get("problema").toString(), TipoProblemaDTO.class);
        for (int i = 0; i < arr.length(); i++) {
            listal.add(u.JsonArrayStringToList(arr.getJSONArray(i)));
        }
        Simplex s = new Simplex();

        Problema p = new Problema(listal, tp.getId());
        s.executarSimplex(p);
        List result = s.imprimeResultado();

        RetornoJsonDTO retornojson = new RetornoJsonDTO();
        retornojson.setError(false);
        retornojson.setData(result);

        return retornojson;
    } catch (Exception e) {
        RetornoJsonDTO retornojson = new RetornoJsonDTO();
        retornojson.setError(true);
        retornojson.setErrorMessage(e.getMessage());
        return retornojson;
    }
}

From source file:apprenda.clientservices.tasks.ApprendaDeployTask.java

License:Apache License

private <T> T getObjectFromJson(String jsonString, Class<T> conversionType) {
    Gson gson = getGsonBuilder();
    T returnValue = gson.fromJson(jsonString, conversionType);

    return returnValue;
}

From source file:apprenda.clientservices.tasks.ApprendaDeployTask.java

License:Apache License

private <T> T getObjectFromJson(String jsonString, TypeToken<T> conversionType) {
    Gson gson = getGsonBuilder();
    Type collectionType = conversionType.getType();
    T returnValue = gson.fromJson(jsonString, collectionType);
    return returnValue;
}

From source file:AppUemWS.AppReservaUemWS.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/*from   w  w  w.j av a  2  s.c o  m*/
@Produces(MediaType.APPLICATION_JSON)
@Path("/departamento/cadastrarDepartamento")
public String cadastrarDepartamento(String encap) {
    Gson g = new Gson();
    Encapsular encapsular = g.fromJson(encap, Encapsular.class);
    Login login = g.fromJson(encapsular.getCampo1(), Login.class);
    Departamento departamento = g.fromJson(encapsular.getCampo2(), Departamento.class);
    int permissao = this.verificarPrioridadeLogin(login.getEmail(), login.getSenha());
    int resultado = -1; // -1 = sem permissao

    if (permissao > 1) {
        try {

            con = new ControladorDePersistencia();
            Boolean ok = con.cadastraDepartamento(departamento);
            if (ok) {
                resultado = 1; // sucesso
            } else {
                resultado = 0; // falha
            }
        } catch (Exception ex) {
            resultado = 0; // falha
        }
    }
    return g.toJson(resultado);
}

From source file:AppUemWS.AppReservaUemWS.java

@PUT
@Consumes(MediaType.APPLICATION_JSON)/*from w w  w.  j  a v a  2s .c o  m*/
@Produces(MediaType.APPLICATION_JSON)
@Path("/departamento/alterarDepartamento")
public String alterarDepartamento(String encap) {
    Gson g = new Gson();
    Encapsular encapsular = g.fromJson(encap, Encapsular.class);
    Login login = g.fromJson(encapsular.getCampo1(), Login.class);
    Departamento departamento = g.fromJson(encapsular.getCampo2(), Departamento.class);
    int permissao = this.verificarPrioridadeLogin(login.getEmail(), login.getSenha());
    int resultado = -1; // -1 = sem permissao
    if (permissao > 1) {
        try {

            con = new ControladorDePersistencia();
            Boolean ok = con.alteraDepartamento(departamento);
            if (ok) {
                resultado = 1; // sucesso
            } else {
                resultado = 0; // falha
            }
        } catch (SQLException ex) {
            resultado = 0; // falha
        }
    }
    return g.toJson(resultado);
}

From source file:AppUemWS.AppReservaUemWS.java

@PUT
@Consumes(MediaType.APPLICATION_JSON)//w ww  .  j a  va2 s  .  c o  m
@Produces(MediaType.APPLICATION_JSON)
@Path("/departamento/removeDepartamento")
public String removeDepartamento(String encap) {
    Gson g = new Gson();
    Encapsular encapsular = g.fromJson(encap, Encapsular.class);
    Login login = g.fromJson(encapsular.getCampo1(), Login.class);
    Departamento departamento = g.fromJson(encapsular.getCampo2(), Departamento.class);
    int permissao = this.verificarPrioridadeLogin(login.getEmail(), login.getSenha());
    int resultado = -1; // -1 = sem permissao

    if (permissao > 1) {
        try {
            con = new ControladorDePersistencia();
            Boolean ok = con.removeDepartamento(departamento.getId());
            if (ok) {
                resultado = 1; // sucesso
            } else {
                resultado = 0; // falha
            }
        } catch (SQLException ex) {
            resultado = 0; // falha
        }
    }
    return g.toJson(resultado);
}

From source file:AppUemWS.AppReservaUemWS.java

@POST
@Consumes(MediaType.APPLICATION_JSON)//ww  w .j  a  va2  s .  c o m
@Path("/usuario/cadastrarUsuario")
public String cadastrarUsuario(String encap) {
    Gson g = new Gson();
    Encapsular encapsular = g.fromJson(encap, Encapsular.class);
    Login login = g.fromJson(encapsular.getCampo1(), Login.class);
    Usuario usuario = g.fromJson(encapsular.getCampo2(), Usuario.class);
    int permissao = this.verificarPrioridadeLogin(login.getEmail(), login.getSenha());
    int resultado = -1; // -1 = sem permissao

    if ((permissao == 4) || (permissao > usuario.getPermissao())) {
        try {
            if (emailUsado(usuario)) {
                resultado = -2;
            } else {
                con = new ControladorDePersistencia();
                Boolean ok = con.cadastraUsuario(usuario);
                if (ok) {
                    resultado = 1; // sucesso
                } else {
                    resultado = 0; // falha
                }
            }
        } catch (Exception ex) {
            resultado = 0; // falha
        }
    }
    return g.toJson(resultado);
}

From source file:AppUemWS.AppReservaUemWS.java

@PUT
@Consumes(MediaType.APPLICATION_JSON)/* w  w  w .j av a2  s  .  c  om*/
@Produces(MediaType.APPLICATION_JSON)
@Path("/usuario/alterarUsuario")
public String alterarUsuario(String encap) {
    Gson g = new Gson();
    Encapsular encapsular = g.fromJson(encap, Encapsular.class);
    Login login = g.fromJson(encapsular.getCampo1(), Login.class);
    Usuario usuario = g.fromJson(encapsular.getCampo2(), Usuario.class);
    int permissao = this.verificarPrioridadeLogin(login.getEmail(), login.getSenha());
    int resultado = -1; // -1 = sem permissao
    login.clonar(buscarLogin(login));

    if ((permissao == 4) || (permissao > usuario.getPermissao()) || login.getId() == usuario.getId()) {
        try {
            if (emailUsadoPorOutroUsuario(usuario)) {
                resultado = -2;
            } else {
                con = new ControladorDePersistencia();
                Boolean ok = con.alteraUsuario(usuario);
                if (ok) {
                    resultado = 1; // sucesso
                } else {
                    resultado = 0; // falha
                }
            }
        } catch (SQLException ex) {
            resultado = 0; // falha
        }
    }
    return g.toJson(resultado);
}