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:angularBeans.util.AngularBeansUtils.java

License:LGPL

public void initJsonSerialiser() {

    GsonBuilder builder = new GsonBuilder().serializeNulls();

    builder.registerTypeAdapter(LobWrapper.class, new LobWrapperJsonAdapter(cache));

    builder.registerTypeAdapter(byte[].class, new ByteArrayJsonAdapter(cache, contextPath));

    builder.registerTypeAdapter(LobWrapper.class, new JsonDeserializer<LobWrapper>() {

        @Override//  w  w w.j a  v  a2s .  c  o m
        public LobWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {

            return null;
        }

    });

    mainSerializer = builder.create();

}

From source file:anuncius.api.rt.PushResponse.java

public static String getJson(Object o) {
    Gson gson = new GsonBuilder().create();
    return gson.toJson(o);
}

From source file:aopdomotics.storage.food.FoodJsonDecoder.java

/**
 * Decode bill, using storage array(with prices) and bill json and return total price.
 * @param billJson/*from w  w w.  j a  va 2  s.c  om*/
 * @param magazinList
 * @return 
 */
public static float decodeBill(String billJson, ArrayList<Food> magazinList) {

    JsonElement jelement = new JsonParser().parse(billJson);
    JsonObject json = jelement.getAsJsonObject();

    JsonElement billElement = json.get("Bill");
    System.out.println("Bill JSON " + billElement.toString());

    Gson gson = new GsonBuilder().create();
    FoodJsonDecoder billdecoder = gson.fromJson(billElement, FoodJsonDecoder.class);

    return billdecoder.billPrice(magazinList);
}

From source file:aopdomotics.storage.food.FoodJsonDecoder.java

/**
 * Decode recipe using the three food items from json recipe.
 * @param recipeJson/*from  www  .  j  a  v a2 s  . c  o m*/
 * @return 
 */
public static Recipe decodeRecipe(String recipeJson) {
    Recipe recipe;
    Food[] component = new Food[3];
    JsonElement jelement = new JsonParser().parse(recipeJson);
    JsonObject json = jelement.getAsJsonObject();

    JsonElement recipeElement = json.get("Recipe");
    System.out.println("Recipe JSON " + recipeElement.toString());

    Gson gson = new GsonBuilder().create();
    FoodJsonDecoder recipedecoder = gson.fromJson(recipeElement, FoodJsonDecoder.class);
    int i = 0;
    for (Food item : recipedecoder.foodList()) {
        if (item.quantity > 0) {
            component[i++] = item;
        }
    }
    recipe = new Recipe("eaten", component[0], component[1], component[2]);
    return recipe;
}

From source file:aopdomotics.storage.food.FoodJsonDecoder.java

/**
 * Decode storage update json, return entire catalogue list
 * @param storageJson//from ww w  . ja v  a 2 s  . co m
 * @return 
 */
public static ArrayList<Food> decodeStorage(String storageJson) {
    System.out.println("Storage Json :  " + storageJson);
    JsonElement jelement = new JsonParser().parse(storageJson);
    JsonObject json = jelement.getAsJsonObject();

    JsonElement storageElement = json.get("Storage");
    System.out.println("Storage JSON " + storageElement.toString());

    Gson gson = new GsonBuilder().create();
    FoodJsonDecoder recipedecoder = gson.fromJson(storageElement, FoodJsonDecoder.class);
    return recipedecoder.foodList();
}

From source file:api.PersonResource.java

public PersonResource() {
    gson = new GsonBuilder().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
}

From source file:apidemo.APIDemo.java

public static String ObjectToString(Object obj) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    return gson.toJson(obj);
}

From source file:apidemo.APIDemo.java

public static JSMessage StringToObject(String js) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    return gson.fromJson(js, JSMessage.class);
}

From source file:apidemo.APIMessage.java

public static JSMessageInfo StringToObject(String js) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    return gson.fromJson(js, JSMessageInfo.class);
}

From source file:apidemo.APIStatistical.java

public static JSRecvStatistic StringToObject(String js) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Gson gson = gsonBuilder.disableHtmlEscaping().create();

    return gson.fromJson(js, JSRecvStatistic.class);
}