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:bean.EJBean.java

License:Open Source License

public void createProfesor(Profesor u) {

    final Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
    final String gson = prettyGson.toJson(u);

    System.out.println("create profesor: " + gson);

    client = ClientBuilder.newClient();/*from   w  w  w . ja va 2s. co  m*/
    Response response = client.target(baseURI).path("profesores").request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(gson, MediaType.APPLICATION_JSON), Response.class);
    client.close();

}

From source file:bean.EJBean.java

License:Open Source License

public void updateProfesor(Profesor u) {

    final Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
    final String gson = prettyGson.toJson(u);

    client = ClientBuilder.newClient();/*from  w  ww . j  a v a  2  s.c o  m*/
    Response response = client.target(baseURI).path("profesores").path(u.getIdprofesor())
            .request(MediaType.APPLICATION_JSON)
            .put(Entity.entity(gson, MediaType.APPLICATION_JSON), Response.class);
    client.close();

}

From source file:beans.PostBean.java

/**
 * Returns posts from a single user/*from w  w w .  ja  va 2  s. co m*/
 * @param userid id of user
 * @return resultcode
 */
public static List<PostBean> getPostsFromUser(int userid) {

    String result = RestHelper.getStringFromURL(
            "http://a.fredrikljung.com:8080/Twittbook/webresources/rest/feed?userId=" + userid);
    System.out.println(result);
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create();
    PostBean[] postarray = gson.fromJson(result, PostBean[].class);
    ArrayList<PostBean> list = new ArrayList<>();
    for (PostBean p : postarray) {
        list.add(p);
    }
    Collections.reverse(list);
    return list;
}

From source file:bi.sms_delivery.java

public String sendSms(SMSHolder sh) throws Exception {
    Responce res;/*from  ww w  . j  ava  2 s.c  o  m*/
    /*MobakDocBiulder mdb = MobakDocBiulder.getInstance("NerudTorg", "4Ro21sPc");
     Document doc = mdb.createDocument(sh);
     URLRequester urlreq = new URLRequester(MobakDocBiulder.URL, "utf8");
     urlreq.setPostMethod();
     urlreq.addPostParameter("xml", primXml.documentToString(doc));
     urlreq.create();*/

    DiscontDocBuilder ddb = DiscontDocBuilder.getInstance("330krasn0907", "134087");

    JSONObject requestBody = ddb.createDiscontJSONRequest(sh);

    URL url = new URL(DiscontDocBuilder.URL);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    con.setRequestMethod("POST");
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Accept", "application/json");

    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
    wr.write(requestBody.toString());
    wr.flush();

    String str;

    StringBuilder sb = new StringBuilder();
    int HttpResult = con.getResponseCode();
    if (HttpResult == HttpURLConnection.HTTP_OK) {
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }

        br.close();
        //System.out.println("" + sb.toString());
        str = sb.toString();
    } else {
        //System.out.println(con.getResponseMessage());
        addError(con.getResponseMessage());
        str = con.getResponseMessage();
    }

    URLRequester urlreq = new URLRequester(DiscontDocBuilder.URL, "utf8");
    //urlreq.setPostMethod();
    GsonBuilder gb = new GsonBuilder();
    Gson gson = gb.create();

    //map.put("mes", "330krasn0907");
    //String body="{\"login\":\"330krasn0907\",\"password\":\"134087\",\"clientId\":\"1234\",\"phone\":\"+79124850319\",\"text\":\"test\"}";
    //urlreq.addPostParameter("login", "330krasn0907");
    //urlreq.addPostParameter("password", "134087");
    //urlreq.addPostParameter("messages", requestBody);
    //urlreq.setUrlEncoding("UTF-8");
    //urlreq.addPostParameter("json",gson.toJson(map));
    //urlreq.create();
    String resp = urlreq.getResult();
    Type maptype = new TypeToken<HashMap<String, String>>() {
    }.getType();
    HashMap<String, String> urlres = gson.fromJson(resp, maptype);

    /*String str;
    if (urlreq.getErrors().isEmpty() && !urlres.get("status").equals("error")) {
    //str = "success " + urlreq.getResult();
    str = " ? ";
    //str+=" "+requestBody.toString()+"; res="+urlreq.getResult()+";";
    log(sh);
    } else {
    str = "error " + urlreq.getErrors().toString() + "; url_responce_description: " + urlres.get("description") + "; req_text=" + requestBody.toString() + "; res=";
    for (String s : urlres.keySet()) {
        str += s + " - " + urlres.get(s) + "; ";
    }
    }*/

    return str;

}

From source file:bisq.common.util.Utilities.java

License:Open Source License

public static String objectToJson(Object object) {
    Gson gson = new GsonBuilder().setExclusionStrategies(new AnnotationExclusionStrategy())
            /*.excludeFieldsWithModifiers(Modifier.TRANSIENT)*/
            /*  .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)*/
            .setPrettyPrinting().create();
    return gson.toJson(object);
}

From source file:bisq.common.util.Utilities.java

License:Open Source License

public static <T> T jsonToObject(String jsonString, Class<T> classOfT) {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).setPrettyPrinting()
            .create();//from  w  w  w  .  j  a v  a  2  s .  c  om
    return gson.fromJson(jsonString, classOfT);
}

From source file:bisq.desktop.util.GUIUtil.java

License:Open Source License

public static void exportJSON(String fileName, JsonElement data, Stage stage) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setInitialFileName(fileName);
    File file = fileChooser.showSaveDialog(stage);
    if (file != null) {
        try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file, false),
                Charsets.UTF_8)) {//from  w  ww.j  a v  a 2  s .  com
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            outputStreamWriter.write(gson.toJson(data));
        } catch (RuntimeException | IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            new Popup<>().error(Res.get("guiUtil.accountExport.exportFailed", e.getMessage()));
        }
    }
}

From source file:bitrefill.retrofit.Config.java

public static Config fromJson(Reader source) {
    return new Config(new GsonBuilder().create().fromJson(source, JsonObject.class));
}

From source file:bitrefill.retrofit.Config.java

public static Config fromJson(String source) {
    return new Config(new GsonBuilder().create().fromJson(source, JsonObject.class));
}

From source file:bittrex.BittrexProtocall.java

public static HashMap<String, String> getMapFromResponse(String response) {

    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    final HashMap<String, String> map = gson
            .fromJson(response.substring(response.lastIndexOf("\"result\"") + "\"result\"".length() + 2,
                    response.indexOf("}") + 1), new TypeToken<HashMap<String, String>>() {
                    }.getType()); // Sorry.

    return map;/*ww w .  j av a  2 s.  c om*/
}