Example usage for com.google.gson GsonBuilder GsonBuilder

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

Introduction

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

Prototype

public GsonBuilder() 

Source Link

Document

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings.

Usage

From source file:co.vaughnvernon.actormodel.util.serializer.AbstractSerializer.java

License:Apache License

/**
 * Builds my Gson instance for pretty and compact serialization behavior.
 *//*from  ww  w . j  ava2  s.co  m*/
private void buildForPrettyCompact() {
    this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateSerializer())
            .registerTypeAdapter(Date.class, new DateDeserializer()).setPrettyPrinting().create();
}

From source file:co.vaughnvernon.tradercommon.serializer.AbstractSerializer.java

License:Apache License

private void build() {
    this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateSerializer())
            .registerTypeAdapter(Date.class, new DateDeserializer()).serializeNulls().create();
}

From source file:co.vaughnvernon.tradercommon.serializer.AbstractSerializer.java

License:Apache License

private void buildForCompact() {
    this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateSerializer())
            .registerTypeAdapter(Date.class, new DateDeserializer()).create();
}

From source file:co.vaughnvernon.tradercommon.serializer.AbstractSerializer.java

License:Apache License

private void buildForPrettyCompact() {
    this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateSerializer())
            .registerTypeAdapter(Date.class, new DateDeserializer()).setPrettyPrinting().create();
}

From source file:codemate.ui.Config.java

License:Open Source License

/**
 * load/*ww w . j  av  a  2  s .com*/
 * 
 * This method loads the given configuration.
 * 
 * @param fileName
 * 
 * @author Li Dong <dongli@lasg.iap.ac.cn>
 */
public static void load(String fileName) {
    if (fileName.equals(defaultConfig)) {
        File root = new File(defaultRoot);
        if (!root.exists()) {
            UI.notice("Config", "There is no runtime directory as" + defaultRoot + ".");
            boolean succ = (new File(defaultRoot)).mkdir();
            if (succ)
                UI.notice("codemate", "Create one.");
            else
                UI.error("codemate", "Couldn't create one!");
        }
    }
    File file = new File(fileName);
    if (!file.exists()) {
        UI.notice("codemate", "Create a configuration as " + fileName + ".");
        createTemplateConfig(fileName);
    } else {
        UI.notice("codemate", "Load configuration from " + fileName + ".");
        try {
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.registerTypeAdapter(ConfigData.class, new ConfigDataDeserializer());
            Gson gson = gsonBuilder.create();
            String json = new Scanner(file).useDelimiter("\\Z").next();
            configData = gson.fromJson(json, ConfigData.class);
        } catch (Exception e) {
            e.printStackTrace();
            UI.error("codemate", "Encounter error while loading configuation from " + fileName + "!");
        }
    }
    // spread configuration to other classes
    for (Entry<String, BlockData> libraryData : configData.data.get("library").data.entrySet()) {
        LibraryMate libraryMate = LibraryMates.searchLibrary(libraryData.getKey());
        if (libraryMate != null) {
            libraryMate.setRoot(libraryData.getValue().data.get("root"));
            libraryMate.setWrapper("Fortran", libraryData.getValue().data.get("fortran_wrapper"));
        }
    }
}

From source file:codemate.ui.Config.java

License:Open Source License

/**
 * createTemplateConfig//from  w w w.java 2 s.c o m
 * 
 * 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:cognitivej.core.RestAction.java

License:Apache License

public RestAction(@NotNull CognitiveContext cognitiveContext) {
    this.cognitiveContext = cognitiveContext;
    gson = new GsonBuilder().create();
}

From source file:com.aashreys.walls.network.ApiFactory.java

License:Apache License

public UnsplashApi createUnsplashApi(OkHttpClient client, String baseUrl) {
    Gson gson = new GsonBuilder().registerTypeAdapter(Image.class, new UnsplashImageResponseParser()).create();
    return new Retrofit.Builder().client(client.newBuilder().addInterceptor(new Interceptor() {
        @Override//from  w ww  .  ja  v  a  2s.  c om
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            // Add api headers
            Request requestWithHeaders = originalRequest.newBuilder()
                    .addHeader("Authorization", "Client-ID " + SafeApi.decrypt(BuildConfig.UNSPLASH_API_KEY))
                    .addHeader("Accept-Version", UnsplashApi.API_VERSION).build();
            return chain.proceed(requestWithHeaders);
        }
    }).addNetworkInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            String requestUrl = request.url().toString();
            String header = "public, " + UnsplashApi.GENERAL_CACHE_DURATION;
            if (requestUrl.contains("photos/")) {
                header = "public, " + UnsplashApi.PHOTO_INFO_CACHE_DURATION;
            }
            if (requestUrl.contains("/collections/featured")) {
                header = "public, " + UnsplashApi.FEATURED_COLLECTION_CACHE_DURATION;
            }
            return response.newBuilder().header("Cache-Control", header).build();
        }
    }).build()).validateEagerly(true).baseUrl(baseUrl).addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).build().create(UnsplashApi.class);
}

From source file:com.ab.util.AbJsonUtil.java

License:Apache License

/**
 * //  w  w  w  . j  a  v  a 2s  .c  o m
 * ??json.
 * @param list
 * @return
 */
public static String toJson(Object src) {
    String json = null;
    try {
        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();
        json = gson.toJson(src);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return json;
}

From source file:com.ab.util.AbJsonUtil.java

License:Apache License

/**
 * //from  w w  w  . ja va2s.co  m
 * ??json.
 * @param list
 * @return
 */
public static String toJson(List<?> list) {
    String json = null;
    try {
        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();
        json = gson.toJson(list);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return json;
}