Example usage for com.google.gson GsonBuilder setPrettyPrinting

List of usage examples for com.google.gson GsonBuilder setPrettyPrinting

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setPrettyPrinting.

Prototype

public GsonBuilder setPrettyPrinting() 

Source Link

Document

Configures Gson to output Json that fits in a page for pretty printing.

Usage

From source file:pl.datamatica.traccar.api.Context.java

License:Open Source License

private Context() {
    emf = Persistence.createEntityManagerFactory("release");
    Map<String, String> properties = getApiConnectionData();

    if (properties.size() > 0) {
        // Use properties obtained from 'debug.xml' or PRODUCTION_TRACCAR_CONFIG_FILE if possible
        emfMetadata = Persistence.createEntityManagerFactory("traccar_api_metadata_persistence", properties);
    } else {//  w ww.  j  a  va2 s  . co m
        // Otherwise settings from 'persistence.xml' will be used
        emfMetadata = Persistence.createEntityManagerFactory("traccar_api_metadata_persistence");
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat(Application.DATE_FORMAT);
    gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy());
    if (isInDevMode())
        gsonBuilder.setPrettyPrinting();
    gson = gsonBuilder.create();

    daemonExecutor = Executors.newScheduledThreadPool(2);
}

From source file:player.PlayerStats.java

License:Apache License

/**
 * print out as a json file//  w w  w  .j a v  a2s .  c  o  m
 */
public String toString() {
    GsonBuilder gbuilder = new GsonBuilder();
    Gson gson = gbuilder.setPrettyPrinting().create();
    return gson.toJson(this);
}

From source file:ru.koluch.JsonFilesBuilder.java

License:Apache License

public <T> void build(File parentDir, Dictionary dict, PrefixTree<T> tree,
        Function<List<T>, JsonElement> dataSerializer) throws IOException {

    parentDir.mkdir();/*  w  w  w.  ja v  a  2s  .c o m*/

    // Write tree
    File treeDir = new File(parentDir, "tree");
    treeDir.mkdir();
    int root;
    try (JsonArrayWriter treeJsonWriter = new JsonArrayWriter(TREE_ITEMS_PER_FILE, treeDir)) {
        root = writeTree(treeJsonWriter, tree, dataSerializer);
    }

    // Write dictionary
    File dictLexDir = new File(parentDir, "dict_lexs");
    dictLexDir.mkdir();
    File dictParadigmDir = new File(parentDir, "dict_paradigms");
    dictParadigmDir.mkdir();
    File dictPrefixDir = new File(parentDir, "dict_prefixes");
    dictPrefixDir.mkdir();
    try (JsonArrayWriter lexRecsWriter = new JsonArrayWriter(DICT_LEX_ITEMS_PER_FILE, dictLexDir);
            JsonArrayWriter paradigmsRecsWriter = new JsonArrayWriter(DICT_PARADIGM_ITEMS_PER_FILE,
                    dictParadigmDir);
            JsonArrayWriter prefixesWriter = new JsonArrayWriter(DICT_PREFIX_ITEMS_PER_FILE, dictPrefixDir)) {
        writeDict(lexRecsWriter, paradigmsRecsWriter, prefixesWriter, dict);
    }

    // Write index file
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(parentDir, "index.json")))) {
        JsonObject indexJson = new JsonObject();
        indexJson.add("root", new JsonPrimitive(root));
        indexJson.add("treeItemsPerFile", new JsonPrimitive(TREE_ITEMS_PER_FILE));
        indexJson.add("dictLexItemsPerFile", new JsonPrimitive(DICT_LEX_ITEMS_PER_FILE));
        indexJson.add("dictParadigmItemsPerFile", new JsonPrimitive(DICT_PARADIGM_ITEMS_PER_FILE));
        indexJson.add("dictPrefixItemsPerFile", new JsonPrimitive(DICT_PREFIX_ITEMS_PER_FILE));
        indexJson.add("treeDir", new JsonPrimitive(treeDir.getName()));
        indexJson.add("dictLexDir", new JsonPrimitive(dictLexDir.getName()));
        indexJson.add("dictParadigmDir", new JsonPrimitive(dictParadigmDir.getName()));
        indexJson.add("dictPrefixDir", new JsonPrimitive(dictPrefixDir.getName()));
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        writer.write(gson.toJson(indexJson));
    }
}

From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java

License:Apache License

@Override
public void save(Note note) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();//  w w  w. j  a v  a 2 s.co  m
    String json = gson.toJson(note);

    FileObject rootDir = getRootDir();

    FileObject projectDir = rootDir.resolveFile(this.project.getName(), NameScope.CHILD);
    if (!projectDir.exists()) {
        projectDir.createFolder();
    }
    if (!isDirectory(projectDir)) {
        throw new IOException(projectDir.getName().toString() + " is not a directory");
    }

    FileObject noteDir = projectDir.resolveFile(note.id(), NameScope.CHILD);

    if (!noteDir.exists()) {
        noteDir.createFolder();
    }
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    // false means not appending. creates file if not exists
    OutputStream out = noteJson.getContent().getOutputStream(false);
    out.write(json.getBytes(conf.getString(ConfVars.ZEPPELIN_ENCODING)));
    out.close();
}

From source file:se.kth.hopsworks.zeppelin.server.JsonResponse.java

License:Apache License

@Override
public String toString() {
    GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Interpreter.class,
            new InterpreterSerializer());
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }// w  w w.  jav a  2 s .  c o m
    Gson gson = gsonBuilder.create();
    return gson.toJson(this);
}

From source file:se.kth.karamel.backend.ClusterDefinitionService.java

public static String serializeJson(JsonCluster jsonCluster) throws KaramelException {
    GsonBuilder builder = new GsonBuilder();
    builder.disableHtmlEscaping();//from   w w w .  j ava  2  s . c om
    Gson gson = builder.setPrettyPrinting().create();
    String json = gson.toJson(jsonCluster);
    return json;
}

From source file:se.kth.karamel.backend.running.model.tasks.DagBuilder.java

private static RunRecipeTask makeRecipeTaskIfNotExist(String recipeName, MachineRuntime machine,
        ClusterStats clusterStats, JsonObject chefJson, TaskSubmitter submitter, String cookbookId,
        String cookbookName, Map<String, RunRecipeTask> allRecipeTasks, Dag dag)
        throws DagConstructionException {
    String recId = RunRecipeTask.makeUniqueId(machine.getId(), recipeName);
    RunRecipeTask runRecipeTask = allRecipeTasks.get(recId);
    if (!allRecipeTasks.containsKey(recId)) {
        ChefJsonGenerator.addRunListForRecipe(chefJson, recipeName);
        GsonBuilder builder = new GsonBuilder();
        builder.disableHtmlEscaping();/* w  ww.j a v  a 2s  . c  o m*/
        Gson gson = builder.setPrettyPrinting().create();
        String jsonString = gson.toJson(chefJson);
        runRecipeTask = new RunRecipeTask(machine, clusterStats, recipeName, jsonString, submitter, cookbookId,
                cookbookName);
        dag.addTask(runRecipeTask);
    }
    allRecipeTasks.put(recId, runRecipeTask);
    return runRecipeTask;
}

From source file:se.kth.karamel.backend.running.model.tasks.RunRecipeTask.java

@Override
public List<ShellCommand> getCommands() throws IOException {

    Set<JsonElement> paramsToMerge = DagParams.getGlobalParams();
    if (paramsToMerge != null) {
        try {/*from   w w  w . j  a va  2 s  . co m*/
            // Merge in Global return results into the json file.
            JsonElement obj = new JsonParser().parse(json);
            if (obj.isJsonObject()) {
                JsonObject jsonObj = obj.getAsJsonObject();

                for (JsonElement param : paramsToMerge) {
                    if (param.isJsonObject()) {
                        JsonObject paramObj = param.getAsJsonObject();
                        merge(jsonObj, paramObj);
                    }
                }
                GsonBuilder builder = new GsonBuilder();
                builder.disableHtmlEscaping();
                Gson gson = builder.setPrettyPrinting().create();
                json = gson.toJson(jsonObj);
            } else {
                logger.warn(String.format("Invalid json object for chef-solo: \n %s'", json));
            }
        } catch (JsonIOException | JsonSyntaxException ex) {
            logger.warn(String.format("Invalid return value as Json object: %s \n %s'", ex.toString(), json));
        }
    }

    if (commands == null) {
        String jsonFileName = recipeCanonicalName.replaceAll(Settings.COOKBOOK_DELIMITER, "__");
        commands = ShellCommandBuilder.makeSingleFileCommand(Settings.SCRIPT_PATH_RUN_RECIPE, "chef_json", json,
                "json_file_name", jsonFileName, "log_file_name", jsonFileName, "sudo_command", getSudoCommand(),
                "task_id", getId(), "succeedtasks_filepath", Settings.SUCCEED_TASKLIST_FILENAME, "pid_file",
                Settings.PID_FILE_NAME);
    }
    return commands;
}

From source file:se.kth.karamel.common.cookbookmeta.KaramelizedCookbook.java

public String getInfoJson() {
    if (json == null) {
        CookbookInfoJson cookbookInfoJson = new CookbookInfoJson(urls.id, metadataRb);
        GsonBuilder builder = new GsonBuilder();
        builder.disableHtmlEscaping();//from   www . ja va2s  . c om
        Gson gson = builder.setPrettyPrinting().create();
        json = gson.toJson(cookbookInfoJson);
    }
    return json;
}

From source file:se.kth.karamel.common.stats.ClusterStats.java

public synchronized String toJsonAndMarkNotUpdated() {
    GsonBuilder builder = new GsonBuilder();
    builder.disableHtmlEscaping();/*from w w  w . jav a 2 s . com*/
    Gson gson = builder.setPrettyPrinting().create();
    String json = gson.toJson(this);
    updated = false;
    return json;
}