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:com.softwaremagico.tm.json.PartyJsonManager.java

License:Open Source License

public static String toJson(Party party) {
    if (party != null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy()).create();
        gsonBuilder.registerTypeAdapter(IValue.class, new IValueSerializer<IValue>());
        gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter());
        gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter());
        gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter());
        gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter());
        gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter());
        gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter());
        gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter());
        gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter());
        gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter());
        gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter());
        // final Gson gson = new
        // GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
        Gson gson = gsonBuilder.create();
        String jsonText = gson.toJson(party);
        return jsonText;
    }/*from   w  w  w .  ja  v  a  2  s .c  o m*/
    return null;
}

From source file:com.softwaremagico.tm.json.PartyJsonManager.java

License:Open Source License

public static Party fromJson(String jsonText) {
    if (jsonText != null && jsonText.length() > 0) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.registerTypeAdapter(IValue.class, new InterfaceAdapter<IValue>());
        gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter());
        gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter());
        gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter());
        gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter());
        gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter());
        gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter());
        gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter());
        gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter());
        gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter());
        gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter());
        Gson gson = gsonBuilder.create();

        Party party = gson.fromJson(jsonText, Party.class);
        return party;
    }/*from  w  w  w  . j  a  va2  s  .  c  om*/
    return null;
}

From source file:com.softwarementors.extjs.djn.gson.DefaultGsonBuilderConfigurator.java

License:Open Source License

public void configure(GsonBuilder builder, GlobalConfiguration configuration) {
    assert builder != null;
    assert configuration != null;

    if (configuration.getDebug()) {
        builder.setPrettyPrinting();
    }//from w  w  w . jav a2s  . com
    builder.serializeNulls();
    builder.disableHtmlEscaping();

    builder.registerTypeAdapter(Date.class, new DateDeserializer());
    builder.registerTypeAdapter(Date.class, new DateSerializer());
}

From source file:com.srotya.tau.wraith.actions.alerts.templated.AlertTemplateSerializer.java

License:Apache License

/**
 * @param template//w  w w  .  ja v a 2  s .  c o m
 * @return
 */
public static String serialize(AlertTemplate template, boolean pretty) {
    GsonBuilder builder = new GsonBuilder();
    if (pretty) {
        builder.setPrettyPrinting();
    }
    return builder.create().toJson(template);
}

From source file:com.srotya.tau.wraith.actions.alerts.templated.AlertTemplateSerializer.java

License:Apache License

public static String serialize(List<AlertTemplate> template, boolean pretty) {
    GsonBuilder builder = new GsonBuilder();
    if (pretty) {
        builder.setPrettyPrinting();
    }/*from w  ww  . j  a  v  a  2  s .  c o  m*/
    return builder.create().toJson(template);
}

From source file:com.srotya.tau.wraith.rules.RuleSerializer.java

License:Apache License

/**
 * Serialize {@link Rule}s to JSON string
 * //from ww  w. ja v a  2s .  c o m
 * @param rules
 * @return rules as JSON
 */
public static String serializeRulesToJSONString(List<Rule> rules, boolean pretty) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Condition.class, new ConditionSerializer());
    gsonBuilder.registerTypeAdapter(Action.class, new ActionSerializer());
    gsonBuilder.disableHtmlEscaping();
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }
    Gson gson = gsonBuilder.create();
    return gson.toJson(rules);
}

From source file:com.srotya.tau.wraith.rules.RuleSerializer.java

License:Apache License

/**
 * Serialize {@link Rule} to JSON string
 * /*from  w w w  .j  ava2s . c o m*/
 * @param rule
 * @return rule as JSON
 */
public static String serializeRuleToJSONString(Rule rule, boolean pretty) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Condition.class, new ConditionSerializer());
    gsonBuilder.registerTypeAdapter(Action.class, new ActionSerializer());
    gsonBuilder.disableHtmlEscaping();
    if (pretty) {
        gsonBuilder.setPrettyPrinting();
    }
    Gson gson = gsonBuilder.create();
    return gson.toJson(rule);
}

From source file:com.stratio.explorer.notebook.Note.java

License:Apache License

public void exportToFile(String path, String filename) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();//from w  ww . j  a v  a  2  s  . co  m

    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();
    } else if (dir.isFile()) {
        throw new RuntimeException("File already exists" + dir.toString());
    }

    File file = new File(dir.getPath() + "/" + filename + ".json");
    logger.info("Persist note {} into {}", filename, file.getAbsolutePath());

    String json = gson.toJson(this);
    FileOutputStream out = new FileOutputStream(file);
    out.write(json.getBytes(conf.getString(ConfVars.EXPLORER_ENCODING)));
    out.close();
}

From source file:com.stratio.explorer.notebook.Note.java

License:Apache License

public void persist() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();/* ww  w . ja  v  a 2s.  c  o m*/

    if (conf == null) {
        conf = ExplorerConfiguration.create(ConstantsFolder.CT_NAME_FILE_INTERPRETERS_CONFIGURE);
    }
    String explorerDir = conf.getExplorerDir();
    File dir = new File(explorerDir + "/" + id);
    if (!dir.exists()) {
        dir.mkdirs();
    } else if (dir.isFile()) {
        throw new RuntimeException("File already exists" + dir.toString());
    }

    File file = new File(explorerDir + "/" + id + "/note.json");
    logger.info("Persist note {} into {}", id, file.getAbsolutePath());

    String json = gson.toJson(this);
    FileOutputStream out = new FileOutputStream(file);
    out.write(json.getBytes(conf.getString(ConfVars.EXPLORER_ENCODING)));
    out.close();
}

From source file:com.stratio.explorer.notebook.Note.java

License:Apache License

public static Note importFromFile(Note n, String filename, String path) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();/*from   ww w. ja va2  s  .  co  m*/
    File file = new File(path);
    logger.info("Load note {} from {}", filename, file.getAbsolutePath());

    if (!file.isFile()) {
        logger.info("##### Note-> !file found");
        return null;
    }

    String[] filenameWithExtension = filename.split("\\.");
    for (String aFilenameWithExtension : filenameWithExtension) {
        logger.info("##### Note-> " + aFilenameWithExtension);
    }
    String ext = filenameWithExtension[filenameWithExtension.length - 1];
    FileInputStream ins = new FileInputStream(file);
    String fileString = IOUtils.toString(ins, ConfVars.EXPLORER_ENCODING.getStringValue());
    if (ext.compareToIgnoreCase("json") == 0) {
        Note note = gson.fromJson(fileString, Note.class);
        n.addParagraphs(note.getParagraphs());
        n.setName(note.getName() + " - copy");
        for (Paragraph p : n.paragraphs) {
            if (p.getStatus() == Job.Status.PENDING || p.getStatus() == Job.Status.REFRESH_RESULT
                    || p.getStatus() == Job.Status.RUNNING) {
                p.setStatus(Job.Status.ABORT);
            }
        }
    } else {
        String[] lines = fileString.split("\\r?\\n");
        for (String line : lines) {
            Paragraph p = n.addParagraph();
            p.setText(line);
        }
    }

    return n;
}