@Override public String requestMessageForNotifyPluginSettingsChange(Map<String, String> pluginSettings) { return new GsonBuilder().create().toJson(pluginSettings); }
private ResponseScratch parseResponseForMigration(String responseBody) { return new GsonBuilder().create().fromJson(responseBody, ResponseScratch.class); }
Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(uglyJSONString); String prettyJsonString = gson.toJson(je);
public static String toPrettyJsonString(Object obj) { Gson gson2 = new GsonBuilder().setPrettyPrinting().create(); return gson2.toJson(obj); }
Gson gson = new GsonBuilder() .setExclusionStrategies(new TestExclStrat("in.naishe.test.Country.name")) //.serializeNulls() .create(); Student src = new Student(); String json = gson.toJson(src); System.out.println(json);
Gson gson= new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); String date = "\"2013-02-10T13:45:30+0100\""; Date test = gson.fromJson(date, Date.class); System.out.println("date:" + test);
Person p = new Person(1, "Joe", new Person(2, "Mike")); com.google.gson.Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer()) .create(); System.out.println(gson.toJson(p));
com.google.gson.Gson gson = new GsonBuilder().create(); MoshiConverterFactory moshiConverterFactory = MoshiConverterFactory.create(moshi); GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson); System.out.println("Library 1: " + library1.name); System.out.println("Library 2: " + library2.name); System.out.println("Library 3: " + library3.name);
GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer()); Gson gson = gsonBuilder.create();
public JavadocJsonGenerator( RobolectricModel model, ProcessingEnvironment environment, File jsonDocsDir) { super(); this.model = model; this.messager = environment.getMessager(); gson = new GsonBuilder() .setPrettyPrinting() .create(); this.jsonDocsDir = jsonDocsDir; }
GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(CityList.class, new CityListDeserializer()); Gson gson = builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create(); CityList list = gson.fromJson(json, CityList.class);
public JSONHandler() { gson = new GsonBuilder().disableHtmlEscaping().create(); }
// Creates the json object which will manage the information received GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); } }); Gson gson = builder.create();
Map<String, String> myMap = new HashMap<String, String>(); myMap.put("one", "hello"); myMap.put("two", "world"); Gson gson = new GsonBuilder().create(); String json = gson.toJson(myMap); System.out.println(json); Type typeOfHashMap = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> newMap = gson.fromJson(json, typeOfHashMap); // This type must match TypeToken System.out.println(newMap.get("one")); System.out.println(newMap.get("two"));
Gson gson = new GsonBuilder() .setExclusionStrategies(new TestExclStrat()) //.serializeNulls() <-- uncomment to serialize NULL fields as well .create(); Student src = new Student(); String json = gson.toJson(src); System.out.println(json);
public class GsonTest { public static void main(String[] args) { // Note the time zone format tweak (removed the ':') String json = "[{\"2011-04-30T00:00:00-0700\":100}, {\"2011-04-29T00:00:00-0700\":200}]"; Gson gson = new GsonBuilder() .registerTypeAdapter(MyCustomClass.class, new MyCustomDeserializer()) .create(); Type collectionType = new TypeToken<Collection<MyCustomClass>>(){}.getType(); Collection<MyCustomClass> myCustomClasses = gson.fromJson(json, collectionType); System.out.println(myCustomClasses); } }
private ResponseScratch parseResponseForMigration(String responseBody) { return new GsonBuilder().create().fromJson(responseBody, ResponseScratch.class); }
private static String toJson(Map<String, String> map) { Gson gson = new GsonBuilder().create(); try { return encode(gson.toJson(map), UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
public GLSLShaderFormat() { gson = new GsonBuilder() .registerTypeAdapter(ShaderMetadata.class, new ShaderMetadataHandler()) .create(); }
public ObjectExporter(ObjectDefinition object) { this.object = object; GsonBuilder builder = new GsonBuilder() .setPrettyPrinting(); gson = builder.create(); }