List of usage examples for com.google.gson GsonBuilder create
public Gson create()
From source file:com.dabay6.android.apps.carlog.app.InitializationIntentService.java
License:Open Source License
/** * @return/*from w w w. j av a2s . c o m*/ */ private Gson buildGson() { final GsonBuilder builder = new GsonBuilder(); builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE); return builder.create(); }
From source file:com.dabay6.android.apps.carlog.data.DTO.FuelHistoryDTO.java
License:Open Source License
@Override public String toString() { final GsonBuilder builder = new GsonBuilder(); final Gson gson; gson = builder.create(); return gson.toJson(this); }
From source file:com.dabay6.libraries.androidshared.util.GsonUtils.java
License:Open Source License
/** * Create a pre-configured {@link Gson} instance. * * @param serializeNulls determines if nulls will be serialized. * * @return {@link Gson} instance.//from w w w .j a v a 2s .c o m */ private static Gson createGson(final boolean serializeNulls) { final GsonBuilder builder = new GsonBuilder(); if (serializeNulls) { builder.serializeNulls(); } return builder.create(); }
From source file:com.datascience.core.storages.JSONUtils.java
License:Open Source License
public JSONUtils() { GsonBuilder builder = getFilledDefaultGsonBuilder(); gson = builder.create(); }
From source file:com.datascience.core.storages.JSONUtils.java
License:Open Source License
public static Gson getOldGson() { GsonBuilder builder = getFilledDefaultGsonBuilder(); builder.registerTypeAdapter(com.datascience.gal.Worker.class, Worker.deserializer); return builder.create(); }
From source file:com.datascience.gal.scripts.SerializationSanityChecks.java
License:Open Source License
/** * @param args/* ww w. j a v a 2 s . c o m*/ */ public static void main(String[] args) { AssignedLabel al = new AssignedLabel("foo", "bar", "baz"); System.out.println(al); String json = al.toString(); GsonBuilder builder = new GsonBuilder(); Type type = new TypeToken<AssignedLabel>() { }.getType(); Type ctype = new TypeToken<CorrectLabel>() { }.getType(); Type atype = new TypeToken<Collection<AssignedLabel>>() { }.getType(); Type cattype = new TypeToken<Category>() { }.getType(); Type catstype = new TypeToken<Collection<Category>>() { }.getType(); Type mattype = new TypeToken<MultinomialConfusionMatrix>() { }.getType(); builder.registerTypeAdapter(type, AssignedLabel.deserializer); builder.registerTypeAdapter(ctype, CorrectLabel.deserializer); builder.registerTypeAdapter(cattype, Category.deserializer); builder.registerTypeAdapter(mattype, MultinomialConfusionMatrix.deserializer); Gson gson = builder.create(); System.out.println(gson.fromJson(json, type)); System.out.println(gson.fromJson(json, type).getClass()); CorrectLabel cl = new CorrectLabel("foo", "bar"); System.out.println(cl); String t = cl.toString(); System.out.println(gson.fromJson(t, ctype)); System.out.println(gson.fromJson(t, ctype).getClass()); Collection<AssignedLabel> col = new HashSet<AssignedLabel>(); for (int i = 0; i < 10; i++) { String foo = i + ""; col.add(new AssignedLabel(foo, foo, foo)); } String tmp = gson.toJson(col); System.out.println(gson.fromJson(tmp, atype).getClass()); Collection<Category> cats = new HashSet<Category>(); for (int i = 0; i < 5; i++) { cats.add(new Category("" + i)); } System.out.println(gson.toJson(cats)); String foo = gson.toJson(cats); System.out.println(gson.fromJson(foo, catstype).getClass()); Datum datum = new Datum("foo", new HashSet<Category>(cats)); System.out.println(datum); Worker worker = new Worker("foo", new HashSet<Category>(cats)); System.out.println(worker); ConfusionMatrix mat = new MultinomialConfusionMatrix(cats); String matrix = gson.toJson(mat); System.out.println(matrix); ConfusionMatrix mat2 = gson.fromJson(matrix, mattype); System.out.println(mat2.getClass()); }
From source file:com.denimgroup.threadfix.importer.util.JsonUtils.java
License:Mozilla Public License
private static Gson getGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer()); return gsonBuilder.create(); }
From source file:com.denimgroup.threadfix.remote.response.ResponseParser.java
License:Mozilla Public License
private static Gson getGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Calendar.class, new CalendarSerializer()); gsonBuilder.registerTypeAdapter(GregorianCalendar.class, new CalendarSerializer()); gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer()); gsonBuilder.registerTypeAdapter(byte[].class, new ByteToStringSerializer()); // needed for files. return gsonBuilder.create(); }
From source file:com.devamatre.core.JSONHelper.java
License:Open Source License
/** * Returns the GSON object./*from w w w .ja v a 2 s. co m*/ * * @param prettyPrint * @return */ private static Gson newGsonObject(boolean prettyPrint) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.enableComplexMapKeySerialization(); if (prettyPrint) { gsonBuilder.setPrettyPrinting(); } Gson gson = gsonBuilder.create(); return gson; }
From source file:com.devbliss.doctest.utils.JSONHelper.java
License:Apache License
/** * /*w ww . j a v a 2s. co m*/ * Converts the given POJO into a Json representation. * If prettyPrint is true, the output will be nicely formatted. * * @param obj * @param prettyPrint * @return */ public String toJson(Object obj, boolean prettyPrint) { GsonBuilder builder = new GsonBuilder(); if (prettyPrint) { builder.setPrettyPrinting(); } return builder.create().toJson(obj); }