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.ccc.crest.core.cache.crest.alliance.Alliances.java

License:Open Source License

public String toJson() {
    GsonBuilder gson = new GsonBuilder();
    gson.setPrettyPrinting();
    return gson.create().toJson(this);
}

From source file:com.ccc.crest.core.cache.crest.schema.RootEndpoint.java

License:Open Source License

public void dumpTree(File fbase) throws Exception {
    AtomicInteger level = new AtomicInteger(-2);
    traverse(endpoints.root, fbase, level);

    String path = fbase.getPath() + "/root/" + "technical.md";
    PrintWriter out = new PrintWriter(path);
    out.println(techPage);/*from w w w  .j  a  v a 2s. c om*/
    out.close();
    GsonBuilder gson = new GsonBuilder();
    gson.setPrettyPrinting();
    jsonStr = gson.create().toJson(endpoints);
    log.info("\n" + jsonStr + "\n");
    log.info("\ntotalGroups: " + totalGroups.get() + "\ntotalLeafs: " + totalLeafs.get() + "\ntotalUids: "
            + totalUids.get());
}

From source file:com.ccc.crest.core.client.json.RootJsonGenerator.java

License:Open Source License

public RootJsonGenerator() {
    String logPath = DefaultLogPath;
    System.setProperty(LogFilePathKey, logPath);
    String base = logPath;/*  w w w.  j ava 2  s .  c  o m*/
    int idx = logPath.lastIndexOf('.');
    if (idx != -1)
        base = logPath.substring(0, idx);
    System.setProperty(LogFileBaseKey, logPath);
    log = LoggerFactory.getLogger(getClass());
    if (log.isDebugEnabled()) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        // TODO: StatusPrinter.setPrintStream
        StatusPrinter.print(lc);
    }
    if (UseSisi)
        rootUrl = SisiUrlBase;
    else
        rootUrl = TqUrlBase;
    init();
    GsonBuilder gson = new GsonBuilder();
    gson.setPrettyPrinting();
    json = gson.create().toJson(endpoints);
}

From source file:com.cognifide.aet.rest.MetadataServlet.java

License:Apache License

@Override
protected void process(DBKey dbKey, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String correlationId = req.getParameter(Helper.CORRELATION_ID_PARAM);
    String suiteName = req.getParameter(Helper.SUITE_PARAM);
    String formatted = req.getParameter(FORMATTED_PARAM);
    resp.setCharacterEncoding("UTF-8");

    Suite suite;// w  ww . j  a  va2  s .com

    try {
        if (isValidCorrelationId(correlationId)) {
            suite = metadataDAO.getSuite(dbKey, correlationId);
        } else if (isValidName(suiteName)) {
            suite = metadataDAO.getLatestRun(dbKey, suiteName);
        } else {
            resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
            resp.getWriter().write(responseAsJson("Neither valid correlationId or suite param was specified."));
            return;
        }
    } catch (StorageException e) {
        LOGGER.error("Failed to get suite", e);
        resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
        resp.getWriter().write(responseAsJson("Failed to get suite: %s", e.getMessage()));
        return;
    }
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (formatted != null && "true".equals(formatted)) {
        gsonBuilder.setPrettyPrinting();
    }
    String result = gsonBuilder.create().toJson(suite, Suite.class);

    resp.setContentType("application/json");
    if (result != null) {
        resp.getWriter().write(result);
    } else {
        resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
        resp.getWriter().write(responseAsJson("Unable to get Suite Metadata for %s", dbKey.toString()));
    }
}

From source file:com.cubeia.poker.handhistory.impl.JsonHandHistoryLogger.java

License:Open Source License

private Gson createGson() {
    GsonBuilder b = new GsonBuilder();
    b.registerTypeAdapter(HandHistoryEvent.class, new EventSerializer());
    b.setPrettyPrinting();
    return b.create();
}

From source file:com.devamatre.core.JSONHelper.java

License:Open Source License

/**
 * Returns the GSON object./*from  ww w.ja  v a  2 s  .  co  m*/
 * 
 * @param prettyPrint
 * @return
 */
private static Gson newGsonObject(boolean prettyPrint) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.enableComplexMapKeySerialization();
    if (prettyPrint) {
        gsonBuilder.setPrettyPrinting();
    }

    Gson gson = gsonBuilder.create();
    return gson;
}

From source file:com.devbliss.doctest.utils.JSONHelper.java

License:Apache License

/**
 * //  ww w.  ja  va 2  s . c om
 * Converts the given POJO into a Json representation.
 * If prettyPrint is true, the output will be nicely formatted.
 * 
 * @param obj
 * @param prettyPrint
 * @return
 */
public String toJson(Object obj, boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder();

    if (prettyPrint) {
        builder.setPrettyPrinting();
    }

    return builder.create().toJson(obj);
}

From source file:com.devbliss.doctest.utils.JSONHelper.java

License:Apache License

/**
 * //from w ww. ja v a  2  s .c  om
 * Converts the given POJO and will skip the given fields while doing so.
 * If prettyPrint is true, the output will be nicely formatted.
 * 
 * @param obj
 * @param excludedFields
 * @param prettyPrint
 * @return
 */
public String toJsonAndSkipCertainFields(Object obj, final List<String> excludedFields, boolean prettyPrint) {
    ExclusionStrategy strategy = new ExclusionStrategy() {
        public boolean shouldSkipField(FieldAttributes f) {
            if (excludedFields.contains(f.getName())) {
                return true;
            }

            return false;
        }

        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };

    GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy)
            .addDeserializationExclusionStrategy(strategy);

    if (prettyPrint)
        builder.setPrettyPrinting();

    return builder.create().toJson(obj);
}

From source file:com.drguildo.bm.BookmarksManager.java

License:Open Source License

public static void save(Bookmarks bookmarks) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
    gsonBuilder.registerTypeAdapter(Tags.class, new TagsSerializer());

    if (DEBUG) {/*  ww w  . j  a va2  s.co m*/
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        System.out.println(gson.toJson(bookmarks.toArrayList()));
        return;
    }

    if (bookmarkFile.exists()) {
        Files.move(bookmarkFile.toPath(), new File(bookmarkFile.getPath() + ".bak").toPath(),
                StandardCopyOption.REPLACE_EXISTING);
    } else {
        if (!bookmarkFile.createNewFile()) {
            throw new IOException("Failed to create bookmark file.");
        }
    }

    FileWriter writer = new FileWriter(bookmarkFile);
    writer.write(gsonBuilder.create().toJson(bookmarks.toArrayList()));
    writer.flush();
    writer.close();
}

From source file:com.droiddevil.myuber.data.DataModule.java

License:Apache License

@Provides
@Singleton/*  w w w.  j a v  a  2s .c  o m*/
Gson provideGson() {
    GsonBuilder builder = new GsonBuilder();

    if (BuildConfig.DEBUG) {
        builder.setPrettyPrinting();
    }

    return builder.create();

}