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:adams.data.conversion.ReportToJson.java

License:Open Source License

/**
 * Performs the actual conversion.// w ww  . ja  v  a2 s .  c  om
 *
 * @return      the converted data
 * @throws Exception   if something goes wrong with the conversion
 */
@Override
protected Object doConvert() throws Exception {
    GsonBuilder builder;
    Gson gson;
    JsonObject jobj;

    jobj = ReportJsonUtils.toJson((Report) m_Input);
    builder = new GsonBuilder();
    if (m_PrettyPrinting)
        builder.setPrettyPrinting();
    gson = builder.create();
    return gson.toJson(jobj);
}

From source file:adams.data.io.output.AbstractSimpleJsonReportWriter.java

License:Open Source License

/**
 * Performs the actual writing.// w  w  w .j  a va2  s .  co m
 *
 * @param data   the data to write
 * @return      true if successfully written
 */
@Override
protected boolean writeData(T data) {
    boolean result;
    GsonBuilder builder;
    Gson gson;
    JsonObject jobj;

    jobj = ReportJsonUtils.toJson(data);
    builder = new GsonBuilder();
    if (m_PrettyPrinting)
        builder.setPrettyPrinting();
    gson = builder.create();
    result = FileUtils.writeToFile(m_Output.getAbsolutePath(), gson.toJson(jobj), false);
    if (!result)
        getLogger().severe("Error writing report #" + data.getDatabaseID() + " to " + m_Output);

    return result;
}

From source file:at.alladin.rmbt.controlServer.ServerResource.java

License:Apache License

public static Gson getGson(boolean prettyPrint) {
    GsonBuilder gb = new GsonBuilder().registerTypeAdapter(DateTime.class, new MyDateTimeAdapter());
    if (prettyPrint)
        gb = gb.setPrettyPrinting();
    return gb.create();
}

From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java

License:Open Source License

public static String toJsonString(NeuralNetworkDTO dto, boolean pretty) {
    JsonObject nn = toJson(dto);/*  w w w  . jav  a2  s .  co m*/

    GsonBuilder builder = new GsonBuilder();
    if (pretty) {
        builder.setPrettyPrinting();
    }
    Gson gson = builder.create();
    String output = gson.toJson(nn);

    return output;
}

From source file:br.com.davimonteiro.lotus_runtime.config.ConfigurationUtil.java

License:Open Source License

public static Configuration save(Path configPath, Configuration config) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.setPrettyPrinting().create();

    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(gson.toJson(config));

    String json = gson.toJson(jsonElement);

    try {// ww  w.ja  va2s  . co m
        Files.write(configPath, json.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return config;
}

From source file:buri.ddmsence.AbstractBaseComponent.java

License:Open Source License

/**
 * @see IDDMSComponent#toJSON()/* www  .  java  2s.co m*/
 */
public String toJSON() {
    GsonBuilder builder = new GsonBuilder();
    if (Boolean.valueOf(PropertyReader.getProperty("output.json.prettyPrint"))) {
        builder.setPrettyPrinting();
    }
    return (builder.create().toJson(getJSONObject()));
}

From source file:ca.oson.json.Oson.java

License:Open Source License

public Gson getGson() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();

        switch (getDefaultType()) {
        case ALWAYS:
            gsonBuilder.serializeNulls();
            break;
        case NON_NULL:
            break;
        case NON_EMPTY:
            break;
        case DEFAULT:
            gsonBuilder.serializeNulls();
            break;
        default://from   w  w  w .  j  a  v  a2s  .c  o  m
            gsonBuilder.serializeNulls();
            break;
        }

        switch (getFieldNaming()) {
        case FIELD: // original field name: someField_name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
            break;
        case LOWER: // somefield_name -> some_field_name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
            break;
        case UPPER: //SOMEFIELD_NAME -> SomeFieldName
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
            break;
        case CAMELCASE: // someFieldName
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
            break;
        case UPPER_CAMELCASE: // SomeFieldName
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
            break;
        case UNDERSCORE_CAMELCASE: // some_Field_Name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
            break;
        case UNDERSCORE_UPPER_CAMELCASE: // Some_Field_Name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
            break;
        case UNDERSCORE_LOWER: // some_field_name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
            break;
        case UNDERSCORE_UPPER: // SOME_FIELD_NAME
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
            break;
        case SPACE_CAMELCASE: // some Field Name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES);
            break;
        case SPACE_UPPER_CAMELCASE: // Some Field Name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES);
            break;
        case SPACE_LOWER: // some field name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES);
            break;
        case SPACE_UPPER: // SOME FIELD NAME
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES);
            break;
        case DASH_CAMELCASE: // some-Field-Name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
            break;
        case DASH_UPPER_CAMELCASE: // Some-Field-Name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
            break;
        case DASH_LOWER: // some-field-name
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
            break;
        case DASH_UPPER: // SOME-FIELD-NAME
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
            break;
        default:
            gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
            break;
        }

        if (getPrettyPrinting() && getIndentation() > 0) {
            gsonBuilder.setPrettyPrinting();
        }

        gsonBuilder.setDateFormat(options.getSimpleDateFormat());

        Set<FieldMapper> mappers = getFieldMappers();
        Map<Class, ClassMapper> classMappers = getClassMappers();
        List<ExclusionStrategy> strategies = new ArrayList<>();
        if (mappers != null) {
            gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
                @Override
                public String translateName(Field f) {
                    String fieldName = f.getName();
                    String serializedName = java2Json(f);
                    if (!fieldName.equalsIgnoreCase(serializedName)) {
                        // if null returned, this field is ignored
                        return serializedName;
                    }
                    return json2Java(f);
                }
            });

            for (FieldMapper mapper : mappers) {
                if (mapper.java == null || mapper.json == null || mapper.ignore) {
                    strategies.add(new ExclusionStrategy() {

                        @Override
                        public boolean shouldSkipField(FieldAttributes f) {
                            String name = f.getName();
                            Class cls = f.getClass();

                            if (mapper.java == null) {
                                if (mapper.json.equals(name)) {
                                    if (mapper.getType() == null || cls.equals(mapper.getType())) {
                                        return true;
                                    }
                                }

                            } else if (mapper.json == null || mapper.ignore) {
                                if (mapper.java.equals(name)) {
                                    if (mapper.getType() == null || cls.equals(mapper.getType())) {
                                        return true;
                                    }
                                }

                            }

                            return false;
                        }

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

        if (classMappers != null) {
            for (Entry<Class, ClassMapper> entry : classMappers.entrySet()) {
                ClassMapper mapper = entry.getValue();
                if (mapper.getType() == null) {
                    mapper.setType(entry.getKey());
                }
                if (mapper.ignore != null && mapper.ignore) {
                    strategies.add(new ExclusionStrategy() {

                        @Override
                        public boolean shouldSkipField(FieldAttributes f) {
                            return false;
                        }

                        @Override
                        public boolean shouldSkipClass(Class<?> clazz) {
                            if (clazz.equals(mapper.getType())) {
                                return true;
                            }
                            return false;
                        }
                    });
                }
            }

            for (Entry<Class, ClassMapper> entry : classMappers.entrySet()) {
                ClassMapper mapper = entry.getValue();
                if (!mapper.ignore && mapper.constructor != null) {
                    gsonBuilder.registerTypeAdapter(entry.getKey(), mapper.constructor);
                }
            }
        }

        int size = strategies.size();
        if (size > 0) {
            gsonBuilder.setExclusionStrategies(strategies.toArray(new ExclusionStrategy[size]));
        }

        Double version = getVersion();
        if (version != null) {
            gsonBuilder.setVersion(version);
        }

        if (isUseGsonExpose()) {
            gsonBuilder.excludeFieldsWithoutExposeAnnotation();
        }

        if (!DefaultValue.isDefault(getPatterns())) {
            gsonBuilder.setLenient();
        }

        gson = gsonBuilder.create();
    }

    return gson;
}

From source file:cl.niclabs.tscrypto.common.utils.Util.java

License:Open Source License

public static Gson GsonFactory(boolean pretty) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (pretty)/*from   www . ja  v a 2s .co m*/
        gsonBuilder.setPrettyPrinting();
    gsonBuilder.registerTypeAdapter(BigInteger.class, new BigIntegerBase64TypeAdapter());
    gsonBuilder.registerTypeAdapter(TSMessage.class, new TSMessageParser());
    return gsonBuilder.create();
}

From source file:codemate.ui.Config.java

License:Open Source License

/**
 * createTemplateConfig//w  w  w  .j  a v  a 2  s  . c  om
 * 
 * This method creates a template of configuration and asks user for
 * necessary information.
 * 
 * @param fileName
 * 
 * @author Li Dong <dongli@lasg.iap.ac.cn>
 */
private static void createTemplateConfig(String fileName) {
    UI.notice("codemate", "Create a configuration as " + fileName + ".");
    // compiler section
    SectionData compilerSection = new SectionData();
    BlockData fortranBlock = new BlockData();
    UI.notice("codemate", "Choose a Fortran compiler:");
    String vendor = UI.getAnswer(CompilerMates.getVendorNames())[0];
    fortranBlock.data.put("vendor", vendor);
    compilerSection.data.put("Fortran", fortranBlock);
    configData.data.put("compiler", compilerSection);
    // library section
    SectionData librarySection = new SectionData();
    for (LibraryMate libraryMate : LibraryMates.getLibraryMates()) {
        BlockData libraryBlock = new BlockData();
        if (libraryMate.provideCompilerWrapper()) {
            UI.notice("codemate", "Set Fortran compiler wrappers for " + libraryMate.getLibraryName() + ":");
            String wrapper = UI.getAnswer(null)[0];
            libraryBlock.data.put("fortran_wrapper", wrapper);
        } else {
            UI.notice("codemate", "Set library root for " + libraryMate.getLibraryName() + ":");
            String root = UI.getAnswer(null)[0];
            libraryBlock.data.put("root", root);
        }
        librarySection.data.put(libraryMate.getLibraryName(), libraryBlock);
    }
    configData.data.put("library", librarySection);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ConfigData.class, new ConfigDataSerializer());
    Gson gson = gsonBuilder.setPrettyPrinting().create();
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(fileName);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        UI.error("codemate", "Encounter error while creating configuration as " + fileName + "!");
    }
    writer.println(gson.toJson(configData));
    writer.flush();
}

From source file:com.atlauncher.workers.InstanceInstaller.java

License:Open Source License

public InstanceInstaller(String instanceName, Pack pack, PackVersion version, boolean isReinstall,
        boolean isServer) {
    this.instanceName = instanceName;
    this.pack = pack;
    this.version = version;
    this.isReinstall = isReinstall;
    this.isServer = isServer;
    if (isServer) {
        serverLibraries = new ArrayList<File>();
    }/*from w ww . j  ava2s . co  m*/
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.registerTypeAdapter(File.class, new FileTypeAdapter());
    builder.setPrettyPrinting();
    this.gson = builder.create();
}