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:brooklyn.entity.chef.KnifeConvergeTaskFactory.java

License:Apache License

/** construct the knife command, based on the settings on other methods
 * (called when instantiating the script, after all parameters sent)
 *//*  w  w w .  j a  v a 2s .c  om*/
protected List<String> initialKnifeParameters() {
    // runs inside the task so can detect entity/machine at runtime
    MutableList<String> result = new MutableList<String>();
    SshMachineLocation machine = EffectorTasks.findSshMachine();

    result.add("bootstrap");
    result.addAll(extraBootstrapParameters);

    HostAndPort hostAndPort = machine.getSshHostAndPort();
    result.add(wrapBash(hostAndPort.getHostText()));
    Integer whichPort = knifeWhichPort(hostAndPort);
    if (whichPort != null)
        result.add("-p " + whichPort);

    result.add("-x " + wrapBash(checkNotNull(machine.getUser(), "user")));

    File keyfile = ChefServerTasks.extractKeyFile(machine);
    if (keyfile != null)
        result.add("-i " + keyfile.getPath());
    else
        result.add(
                "-P " + checkNotNull(machine.findPassword(), "No password or private key data for " + machine));

    if (sudo != Boolean.FALSE)
        result.add("--sudo");

    if (!Strings.isNullOrEmpty(nodeName)) {
        result.add("--node-name");
        result.add(nodeName);
    }

    result.add("-r " + wrapBash(runList.apply(entity())));

    if (!knifeAttributes.isEmpty())
        result.add("-j " + wrapBash(new GsonBuilder().create().toJson(knifeAttributes)));

    return result;
}

From source file:brooklyn.networking.cloudstack.CloudstackNew40FeaturesClient.java

License:Apache License

protected static Gson gson() {
    return new GsonBuilder().setPrettyPrinting().create();
}

From source file:buri.ddmsence.AbstractBaseComponent.java

License:Open Source License

/**
 * @see IDDMSComponent#toJSON()/*from   w  w  w.  j  a  va2 s. c o 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:bytehala.flowmortarexample.core.RootModule.java

License:Apache License

@Provides
@Singleton
Gson provideGson() {
    return new GsonBuilder().create();
}

From source file:ca.cmput301w14t09.elasticSearch.ElasticSearchOperations.java

License:GNU General Public License

/**
 * This method creates is what makes the bitmap into a json serialzable format
 *///from   w  w w .  j av  a 2s  .c  o m
private static void constructGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Bitmap.class, new JsonBitmapConverter());
    GSON = builder.create();
}

From source file:ca.cs.ualberta.localpost.controller.ElasticSearchOperations.java

License:Open Source License

/**
 * Constructs a Gson with a custom serializer / desserializer registered for
 * Bitmaps.//  ww w  . j a va2 s .com
 */
private static void constructGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Bitmap.class, new BitmapJsonConverter());
    gson = builder.create();
}

From source file:ca.jackymok.tomatoes.toolbox.GsonRequest.java

License:Apache License

public GsonRequest(int method, String url, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.mClazz = clazz;
    this.mListener = listener;

    //Create Gson qith custom Date parsing
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        final java.text.DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        @Override//from w  w  w .ja va 2s. c  o m
        public Date deserialize(JsonElement arg0, java.lang.reflect.Type arg1, JsonDeserializationContext arg2)
                throws JsonParseException {
            try {
                return df.parse(arg0.getAsString());
            } catch (final java.text.ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
    });
    mGson = gsonBuilder.create();
}

From source file:ca.live.hk12.crescent.server.database.WorldDatabase.java

License:Open Source License

public boolean saveWorld(World world) {
    String dataLocation = FileService.getWorldDataLocation(world.info.worldID);
    String infoLocation = FileService.getWorldInfoLocation(world.info.worldID);

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

    try (Writer writer = new OutputStreamWriter(new FileOutputStream(dataLocation), "UTF-8")) {
        gson.toJson(world.map, writer);/*from w ww  . j av a2s . c om*/
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    try (Writer writer = new OutputStreamWriter(new FileOutputStream(infoLocation), "UTF-8")) {
        gson.toJson(world.info, writer);
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    worlds.remove(world);

    return true;
}

From source file:ca.live.hk12.crescent.server.service.FileService.java

License:Open Source License

public static <T> T readData(String fileLocation, Class<T> dataClass) {
    T t = null;//  www  .  j  a  va2 s.  c om
    try (Reader reader = new InputStreamReader(new FileInputStream(fileLocation), "UTF-8")) {
        Gson gson = new GsonBuilder().create();
        t = gson.fromJson(reader, dataClass);
        reader.close();
    } catch (Exception e) {
        if (ServerLauncher.DEBUG)
            throw new ServerException("Unable to read file:<br>" + fileLocation);
    }
    return t;
}

From source file:ca.live.hk12.crescent.server.service.FileService.java

License:Open Source License

public static <T> boolean writeData(String fileLocation, T t) {
    try (Writer writer = new OutputStreamWriter(new FileOutputStream(fileLocation), "UTF-8")) {
        Gson gson = new GsonBuilder().create();
        gson.toJson(t, writer);//  w  ww  .j a  v  a  2s  . co m
        return true;
    } catch (Exception e) {
        throw new ServerException("Could not write to:<br/>" + fileLocation);
    }
}