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:au.edu.unsw.cse.soc.federatedcloud.curator.CuratorAPI.java

License:Open Source License

/**
 * This is a temporary method until a proper DB is figured out.
 * @param description//from  w w  w.  j  a  v  a 2 s  .co  m
 */
private void persistDescription(CloudResourceDescription description) throws Exception {
    //convert the description to json object
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(description);

    try {
        FileWriter writer = new FileWriter(KB_LOCATION + description.getName() + ".json");
        writer.write(json);
        writer.close();
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw e;
    }

}

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

License:Open Source License

/**
 * Init an App.// w  w  w  .  j ava  2  s .c  o m
 * Sample command: init aws-s3
 * @param appName
 * @param appAttributes
 */
private void initCloudBase(String appName, String appAttributes) {
    //check if app.json exist in the current directory
    File jsonFile = new File("./" + Constants.FILE_NAME);
    //if app.json does not exist
    if (!jsonFile.exists()) {
        //create an app.json
        try {
            boolean isCreated = jsonFile.createNewFile();

            if (isCreated) {
                Gson gson = new GsonBuilder().setPrettyPrinting().create();

                InputStream inputStream = this.getClass().getClassLoader()
                        .getResourceAsStream(Constants.TEMPLATE_FILE_NAME);
                StringWriter writer = new StringWriter();
                IOUtils.copy(inputStream, writer);
                String fileContent = writer.toString();

                App app = gson.fromJson(fileContent, App.class);

                //Set name
                app.setName(appName);

                //set attributes
                Map<String, String> map = new HashMap<String, String>();
                app.setAttributes((Map<String, String>) gson.fromJson(appAttributes, map.getClass()));

                transformAppToFile(app, jsonFile);

                System.out.println("App:" + app.getName() + " was created");
            } else {
                System.err.println("File was not created");
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
    } else {
        System.out.println("File:" + jsonFile.getAbsolutePath() + " already exists.");
    }
}

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

License:Open Source License

private App transformJsonFileToApp(File jsonFile) throws IOException {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    String fileContent = FileUtils.readFileToString(jsonFile);

    App app = gson.fromJson(fileContent, App.class);

    return app;//from  ww  w . java 2s .  c om
}

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

License:Open Source License

private File transformAppToFile(App app, File jsonFile) throws IOException {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    String appJSON = gson.toJson(app);

    FileUtils.writeStringToFile(jsonFile, appJSON);

    return jsonFile;
}

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

License:Open Source License

private Map<String, String> generateAttributesFromJson(String resourceMetadata) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    Map<String, String> map = new HashMap<String, String>();
    map = (Map<String, String>) gson.fromJson(resourceMetadata, map.getClass());
    return map;/*w  ww. j  a  v a  2 s.com*/
}

From source file:augsburg.se.alltagsguide.MainModule.java

License:Open Source License

@Provides
Gson gson() {/*from  w w w. j a va 2 s .  c  om*/
    return new GsonBuilder().registerTypeAdapter(new TypeToken<List<Page>>() {
    }.getType(), new PageSerializer()).registerTypeAdapter(new TypeToken<List<Language>>() {
    }.getType(), new LanguageSerializer()).registerTypeAdapter(new TypeToken<List<Location>>() {
    }.getType(), new LocationSerializer()).registerTypeAdapter(new TypeToken<List<EventPage>>() {
    }.getType(), new EventPageSerializer()).create();
}

From source file:azkaban.executor.ExecutionOptions.java

License:Apache License

public String toJSON() {
    return new GsonBuilder().setPrettyPrinting().create().toJson(toObject());
}

From source file:azkaban.webapp.servlet.FlowTriggerInstanceServlet.java

License:Apache License

private void wrapTriggerInst(final TriggerInstance triggerInst, final HashMap<String, Object> ret) {
    final List<Map<String, Object>> dependencyOutput = new ArrayList<>();
    for (final DependencyInstance depInst : triggerInst.getDepInstances()) {
        final Map<String, Object> depMap = new HashMap<>();
        depMap.put("triggerInstanceId", depInst.getTriggerInstance().getId());
        depMap.put("dependencyName", depInst.getDepName());
        depMap.put("dependencyType", depInst.getTriggerInstance().getFlowTrigger()
                .getDependencyByName(depInst.getDepName()).getType());
        depMap.put("dependencyStartTime", depInst.getStartTime());
        depMap.put("dependencyEndTime", depInst.getEndTime());
        depMap.put("dependencyStatus", depInst.getStatus());
        depMap.put("dependencyCancelCause", depInst.getCancellationCause());
        depMap.put("dependencyConfig",
                depInst.getTriggerInstance().getFlowTrigger().getDependencyByName(depInst.getDepName()));
        dependencyOutput.add(depMap);//from  w  w  w .j  av a 2  s.  co  m
    }
    ret.put("items", dependencyOutput);

    ret.put("triggerId", triggerInst.getId());
    ret.put("triggerSubmitter", triggerInst.getSubmitUser());
    ret.put("triggerStartTime", triggerInst.getStartTime());
    ret.put("triggerEndTime", triggerInst.getEndTime());
    ret.put("triggerStatus", triggerInst.getStatus());
    final String flowTriggerJson = new GsonBuilder().setPrettyPrinting().create()
            .toJson(triggerInst.getFlowTrigger());
    ret.put("triggerProps", flowTriggerJson);
}

From source file:azkaban.webapp.servlet.StatusServlet.java

License:Apache License

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    try {/*from w w  w  . j a  va 2 s.  co  m*/
        resp.setContentType(JSON_MIME_TYPE);
        resp.getOutputStream()
                .println(new GsonBuilder().setPrettyPrinting().create().toJson(this.statusService.getStatus()));
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (final Exception e) {
        log.error("Error!! while reporting status: ", e);
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
        resp.getOutputStream().close();
    }
}

From source file:backtype.storm.utils.Utils.java

License:Apache License

public static String toPrettyJsonString(Object obj) {
    Gson gson2 = new GsonBuilder().setPrettyPrinting().create();
    String ret = gson2.toJson(obj);

    return ret;/*from w  ww  . ja  v  a  2s.com*/
}