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:org.unitime.timetable.server.sectioning.PublishedSectioningSolutionsBackend.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/* w  ww .j av a2  s .  c  om*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

/**
 * @return the gson//from  w  w  w  .  ja  v  a 2  s .com
 */
public Gson getGson() {
    if (gson == null) {
        GsonBuilder gbuilder = new GsonBuilder();
        if (serializeNulls) {
            gbuilder.serializeNulls();
        }
        gbuilder.setDateFormat(datePattern);
        if (prettyPrinting) {
            gbuilder.setPrettyPrinting();
        }
        if (excludeFieldsWithoutExposeAnnotation) {
            gbuilder.excludeFieldsWithoutExposeAnnotation();
        }
        if (enableComplexMapKey) {
            gbuilder.enableComplexMapKeySerialization();
        }
        if (version != null) {
            gbuilder.setVersion(version);
        }
        gson = gbuilder.create();
    }
    return gson;
}

From source file:org.wooden.util.GsonConf.java

License:Open Source License

private static Gson createGson() {
    GsonBuilder gb = new GsonBuilder();
    gb.serializeNulls();/* ww w .j  a v  a  2s.  co  m*/
    gb.setPrettyPrinting();
    return gb.create();
}

From source file:org.wso2.ppaas.tools.artifactmigration.Transformation.java

License:Open Source License

/**
 * Method to get Gson/*from w w  w  . jav a 2  s. c o m*/
 */
public Gson getGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    return gsonBuilder.setPrettyPrinting().create();
}

From source file:org.wso2.ppaas.tools.artifactmigration.Transformer.java

License:Open Source License

/**
 * Method to get Gson//  w w  w. j  ava2  s . c  o m
 */
private static Gson getGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    return gsonBuilder.setPrettyPrinting().create();
}

From source file:org.xwiki.contrib.nestedpagesmigrator.internal.serializer.MigrationPlanSerializer.java

License:Open Source License

private static Gson createSerializer() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.registerTypeAdapter(DocumentReference.class, new DocumentReferenceSerializer());
    gsonBuilder = gsonBuilder.registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter());
    gsonBuilder = gsonBuilder.registerTypeHierarchyAdapter(MigrationAction.class,
            new MigrationActionSerializer());
    gsonBuilder = gsonBuilder.setPrettyPrinting();
    return gsonBuilder.create();
}

From source file:org.xwiki.contrib.nestedpagesmigrator.script.internal.StatusAndLogSerializer.java

License:Open Source License

public static String serialize(JobStatus status) {
    HashMap<String, Object> results = new HashMap<>();

    results.put("state", status.getState());
    results.put("progress", status.getProgress().getOffset());
    results.put("logs", status.getLog().getLogsFrom(LogLevel.INFO));

    LogEventSerializer logEventSerializer = new LogEventSerializer();
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.setPrettyPrinting();
    gsonBuilder = gsonBuilder.registerTypeAdapter(LogEvent.class, logEventSerializer);
    gsonBuilder = gsonBuilder.registerTypeAdapter(BeginLogEvent.class, logEventSerializer);
    gsonBuilder = gsonBuilder.registerTypeAdapter(EndLogEvent.class, logEventSerializer);
    Gson gson = gsonBuilder.create();/*from   www. j a v a 2 s. co m*/
    return gson.toJson(results);
}

From source file:password.pwm.util.java.JsonUtil.java

License:Open Source License

private static Gson getGson(final Flag... flags) {
    if (flags == null || flags.length == 0) {
        return GENERIC_GSON;
    }/*w w  w.  ja  v  a2s  .  c  o m*/

    final GsonBuilder gsonBuilder = registerTypeAdapters(new GsonBuilder());

    if (!JavaHelper.enumArrayContainsValue(flags, Flag.HtmlEscape)) {
        gsonBuilder.disableHtmlEscaping();
    }

    if (JavaHelper.enumArrayContainsValue(flags, Flag.PrettyPrint)) {
        gsonBuilder.setPrettyPrinting();
    }

    return gsonBuilder.create();
}

From source file:password.pwm.util.JsonUtil.java

License:Open Source License

private static Gson getGson(final Set<Flag> flags) {
    if (flags == null || flags.isEmpty()) {
        return GENERIC_GSON;
    }/*from  ww  w  . j a  v  a2s.  c o  m*/

    final GsonBuilder gsonBuilder = registerTypeAdapters(new GsonBuilder());

    if (!flags.contains(Flag.HtmlEscape)) {
        gsonBuilder.disableHtmlEscaping();
    }

    if (flags.contains(Flag.PrettyPrint)) {
        gsonBuilder.setPrettyPrinting();
    }

    return gsonBuilder.create();
}

From source file:pl.asie.modalyze.Main.java

License:Apache License

public static void main(String[] args) throws IOException {
    JCommander jCommander = new JCommander(parameters, args);

    if (parameters.help) {
        jCommander.usage();/*  w  ww.  j  a v a2s  . co m*/
        System.exit(0);
    }

    if (parameters.sortFilename) {
        parameters.filenames = true;
    }

    if (parameters.mcpPath != null && parameters.mcpPath.length() > 0) {
        File f = new File(parameters.mcpPath);
        if (f.isDirectory()) {
            MCPDataManager.MCP_DIR = f;
        } else {
            System.err.println("Not a directory: " + f.getAbsolutePath());
            System.exit(0);
        }
    }

    boolean isDir = false;
    for (String s : parameters.files) {
        File f = new File(s);
        isDir |= f.isDirectory();
        analyzeMods(f);
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();

    if (isDir || parameters.files.size() > 1 || modMetadata.size() > 1) {
        if (parameters.sortFilename) {
            Map<String, ModMetadata> metadataMap = new HashMap<>();
            for (ModMetadata m : modMetadata) {
                if (m == null) {
                    continue;
                }
                metadataMap.put(m.filename, m);
            }
            System.out.println(gson.toJson(metadataMap));
        } else if (parameters.sortId) {
            Map<String, List<ModMetadata>> metadataMap = new HashMap<>();
            for (ModMetadata m : modMetadata) {
                if (m == null) {
                    continue;
                }
                String key = m.modid;
                if (key == null && parameters.unknown) {
                    int i = 0;
                    while (metadataMap.containsKey("UNKNOWN-" + i)) {
                        i++;
                    }
                    key = "UNKNOWN-" + i;
                }
                if (key != null) {
                    List<ModMetadata> mods = metadataMap.get(key);
                    if (mods == null) {
                        mods = new ArrayList<>();
                        metadataMap.put(key, mods);
                    }
                    mods.add(m);
                }
            }
            for (List<ModMetadata> list : metadataMap.values()) {
                Collections.sort(list, new Comparator<ModMetadata>() {
                    @Override
                    public int compare(ModMetadata m1, ModMetadata m2) {
                        if (m1.version == null && m2.version == null) {
                            return 0;
                        } else if (m1.version != null && m2.version != null) {
                            return m1.version.compareTo(m2.version);
                        } else {
                            return m1.version != null ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                        }
                    }
                });
            }
            System.out.println(gson.toJson(metadataMap));
        } else {
            Map<String, Map<String, ModMetadata>> metadataMap = new HashMap<>();
            for (ModMetadata m : modMetadata) {
                if (m == null) {
                    continue;
                }

                String key;
                if (m.modid != null) {
                    key = m.modid;
                } else if (!parameters.unknown) {
                    continue;
                } else {
                    int i = 0;
                    while (metadataMap.containsKey("UNKNOWN-" + i)) {
                        i++;
                    }
                    key = "UNKNOWN-" + i;
                }

                Map<String, ModMetadata> metadataMap1 = metadataMap.get(key);
                if (metadataMap1 == null) {
                    metadataMap1 = new HashMap<>();
                    metadataMap.put(key, metadataMap1);
                }
                if (m.version != null) {
                    metadataMap1.put(m.version, m);
                } else if (parameters.unknown) {
                    int i = 0;
                    while (metadataMap1.containsKey("UNKNOWN-" + i)) {
                        i++;
                    }
                    metadataMap1.put("UNKNOWN-" + i, m);
                }
            }
            System.out.println(gson.toJson(metadataMap));
        }
    } else if (modMetadata.size() >= 1) {
        System.out.println(gson.toJson(modMetadata.toArray()[0]));
    } else {
        System.err.println("[ERROR] No mods found!");
    }
}