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:at.tugraz.kmi.medokyservice.json.GsonHandler.java

License:Open Source License

private Gson gson() {
    if (gson == null)
        gson = new GsonBuilder().setExclusionStrategies(new AnnotationExclusionStrategy()).create();
    return gson;/*w  ww.j  a v a 2  s  . com*/
}

From source file:at.tugraz.kmi.medokyservice.json.JSONMapper.java

License:Open Source License

/**
 * /*  w  w w.  ja  v a  2  s  . c  o m*/
 * @return Gson instance
 */
public static Gson getInstance() {
    if (instance == null)
        instance = new GsonBuilder().setExclusionStrategies(new AnnotationExclusionStrategy()).create();
    return instance;
}

From source file:ATDataGrid.servlets.DataObjectProfileManager.java

private boolean doAccessTokenValidation(HttpSession session, String access_token, int issued_at, int expires_at)
        throws Exception {
    //String access_token="ya29.XgHOFfMKYSSkT6oePTOP8PHuoa8vFlEm65Y3mmIJAgwhTBNL6zEQva2RFMIOl_joxzn2j_L-Jt6HbA";
    String USER_AGENT = "Mozilla/5.0";
    String client_id = "815038451936-611r1ll7e9tkdl1kvhhvc9dokp5e9176.apps.googleusercontent.com";
    String url = "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + access_token;
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    request.addHeader("User-Agent", USER_AGENT);
    System.out.println("Sending 'GET' request to URL : " + url);
    HttpResponse response = client.execute(request);
    int reqRC = response.getStatusLine().getStatusCode();
    System.out.println("Response Code : " + reqRC);
    if (reqRC != 200)
        return false;
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/*from   w w  w  .j  a  va  2s .  c om*/
    }
    Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
    GooglePlusSignInTokenInfo reqRec = gson.fromJson(result.toString(), GooglePlusSignInTokenInfo.class);
    System.out.println("Token issued to : " + reqRec.issued_to);
    System.out.println("Token audience : " + reqRec.audience);
    System.out.println("Token user id : " + reqRec.user_id);
    System.out.println("Token scope : " + reqRec.scope);
    System.out.println("Token expires in : " + reqRec.expires_in + " seconds");
    System.out.println("Token email : " + reqRec.email);
    System.out.println("Token verified_email : " + reqRec.verified_email);
    System.out.println("Token access type : " + reqRec.access_type);
    if (!client_id.equals(reqRec.audience))
        return false;
    if (reqRec.expires_in < 1)
        return false;
    session.setAttribute("access_token", access_token);
    session.setAttribute("access_token_user_email", reqRec.email);
    session.setAttribute("access_token_issued_at", issued_at);
    session.setAttribute("access_token_expires_at", expires_at);
    return true;
}

From source file:ATDataGrid.servlets.GoodsOperations.java

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // Get the initialisation parameters
    connectionURL = getInitParameter("connectionURL");
    connectionUser = getInitParameter("connectionUser");
    connectionPass = getInitParameter("connectionPass");
    // prepare the GSON factory
    gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
    ///*from  w  w  w . j a v  a  2  s .com*/
    try {
        // Load (and therefore register) the MySQL driver
        System.out.println("Loading driver ....");
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Loaded driver !");
        System.out.println("Starting connection " + connectionURL + " ....");
        con = DriverManager.getConnection(connectionURL, connectionUser, connectionPass);
        System.out.println("Connected OK !");
        System.out.println("Catalogue: " + con.getCatalog());
    } catch (ClassNotFoundException e) {
        System.out.println("Could not load MySQL database driver");
    } catch (SQLException e) {
        System.out.println("Couldn't get MySQL database connection");
    }
}

From source file:ATWebAuth.HttpClientExample.java

private boolean doAccessTokenValidation() throws Exception {
    String access_token = "ya29.XgHOFfMKYSSkT6oePTOP8PHuoa8vFlEm65Y3mmIJAgwhTBNL6zEQva2RFMIOl_joxzn2j_L-Jt6HbA";
    String client_id = "815038451936-611r1ll7e9tkdl1kvhhvc9dokp5e9176.apps.googleusercontent.com";
    String url = "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + access_token;
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    request.addHeader("User-Agent", USER_AGENT);
    System.out.println("Sending 'GET' request to URL : " + url);
    HttpResponse response = client.execute(request);
    int reqRC = response.getStatusLine().getStatusCode();
    System.out.println("Response Code : " + reqRC);
    if (reqRC != 200)
        return false;
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/*from w  w w . j a  va  2 s  .  c  o  m*/
    }
    Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
    GooglePlusSignInTokenInfo reqRec = gson.fromJson(result.toString(), GooglePlusSignInTokenInfo.class);
    System.out.println("Token issued to : " + reqRec.issued_to);
    System.out.println("Token audience : " + reqRec.audience);
    System.out.println("Token user id : " + reqRec.user_id);
    System.out.println("Token scope : " + reqRec.scope);
    System.out.println("Token expires in : " + reqRec.expires_in + " seconds");
    System.out.println("Token email : " + reqRec.email);
    System.out.println("Token verified_email : " + reqRec.verified_email);
    System.out.println("Token access type : " + reqRec.access_type);
    if (!client_id.equals(reqRec.audience))
        return false;
    if (reqRec.expires_in < 1)
        return false;
    return true;
}

From source file:au.com.breakpoint.hedron.core.JsonUtil.java

License:Apache License

/**
 * Use when converting objects to JSON, so formatting policy is respected.
 *//*  w w  w  .  jav  a 2s.c o m*/
private static Gson getGsonCreator() {
    return m_shouldFormatGson ? new GsonBuilder().setPrettyPrinting().create() : new Gson();
}

From source file:au.com.iglooit.shar.model.vo.ClientFile.java

License:Apache License

/**
 * Construct a new ClientFile from its JSON representation.
 *
 * @param in Reader of JSON string to parse.
 *//*from  w  ww. j ava2  s  .  c  om*/
public ClientFile(Reader in) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    ClientFile other = gson.fromJson(in, ClientFile.class);
    if (StringUtils.isNotBlank(other.resourceId)) {
        this.resourceId = other.resourceId;
    }
    this.title = other.title;
    this.description = other.description;
    this.mimeType = other.mimeType;
    this.content = other.content;
    this.labels = other.labels;
    this.editable = other.editable;

}

From source file:au.com.iglooit.shar.model.vo.State.java

License:Apache License

/**
 * Create a new State given its JSON representation.
 *
 * @param json Serialized representation of a State.
 *//*from  w  ww .  j a  v a 2s .c o m*/
public State(String json) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    State other = gson.fromJson(json, State.class);
    this.action = other.action;
    this.ids = other.ids;
    this.folderId = other.folderId;
}

From source file:au.edu.flinders.ehl.filmweekly.debug.JsonList.java

License:BSD License

/**
 * undertake the task of creating a unique list basic JSON objects
 * /*from   w w  w.  j  a va  2 s  . c o m*/
 * @throws IOException if either a read or write operation fails
 */
public void doTask() throws IOException {

    logger.debug("undertaking the task");

    // create the CSVReader objects
    CSVReader reader = new CSVReader(inputFile, ',', '"', 1);

    //declare other helper variables
    String[] dataElems;
    String coordinate;
    ArrayList<String> coordList = new ArrayList<String>();

    ArrayList<Marker> markerList = new ArrayList<Marker>();
    Marker marker;

    // loop through the file processing each line
    try {

        while ((dataElems = reader.readNext()) != null) {

            coordinate = dataElems[3] + "," + dataElems[4];

            if (coordList.contains(coordinate) == false) {

                marker = new Marker(Double.parseDouble(dataElems[3]), Double.parseDouble(dataElems[4]),
                        dataElems[2].trim(), dataElems[1].trim());
                markerList.add(marker);

                coordList.add(coordinate);
            }

        }
    } catch (IOException e) {
        logger.error("Unable to read from the input file", e);
        throw new IOException("Unable to read from the input file", e);
    }

    // close the file
    reader.close();

    // build a JSON representation of the data and output it to the file
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    outputFile.println(gson.toJson(markerList));

    // close the output file
    outputFile.close();

    logger.debug("task completed");
}

From source file:au.edu.unsw.cse.soc.federatedcloud.CloudResourceBaseDeploymentEngine.java

License:Open Source License

/**
 * Deploy a cloud resources configuration for a given {@code CloudResourceDescription}
 *
 *
 * @param description object of {@code CloudResourceDescription}
 * @throws Exception/*from  w  w  w . j  av a  2s.  com*/
 */
public String deployCloudResourceDescription(CloudResourceDescription description) throws Exception {
    Handler handler = description.getHandler();

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(description);

    return ServiceBusClient.invoke(handler.getName(), "deploy", json);
}