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"));
/** * Example to writeJson using TreeModel */ private static void writeJson() throws IOException { JsonObject rootObject = new JsonObject(); rootObject.addProperty("message", "Hi"); JsonObject childObject = new JsonObject(); childObject.addProperty("name", "World!"); rootObject.add("place", childObject); Gson gson = new Gson(); String json = gson.toJson(rootObject); System.out.println(json); // print "{"message":"Hi","place":{"name":"World!"}}" } }
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);
private String mockLongPollBody(String notificationsStr) { List<ApolloConfigNotification> oldNotifications = gson.fromJson(notificationsStr, notificationType); List<ApolloConfigNotification> newNotifications = new ArrayList<>(); for (ApolloConfigNotification notification : oldNotifications) { newNotifications .add(new ApolloConfigNotification(notification.getNamespaceName(), notification.getNotificationId() + 1)); } return gson.toJson(newNotifications); }
@Override public String requestMessageForNotifyPluginSettingsChange(Map<String, String> pluginSettings) { return new GsonBuilder().create().toJson(pluginSettings); }
Gson gson = Converters.registerDateTime(new GsonBuilder()).create(); SomeContainerObject original = new SomeContainerObject(new DateTime()); String json = gson.toJson(original); SomeContainerObject reconstituted = gson.fromJson(json, SomeContainerObject.class);
Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(uglyJSONString); String prettyJsonString = gson.toJson(je);
String jsonVal0 = "{\"id\": 5382, \"user\": \"Mary\" }"; String jsonVal1 = "{\"id\": 2341, \"person\": \"Bob\"}"; final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(MyClass.class, new MyClassTypeAdapter()); final Gson gson = gsonBuilder.create(); MyClass myClassInstance0 = gson.fromJson(jsonVal0, MyClass.class); MyClass myClassInstance1 = gson.fromJson(jsonVal1, MyClass.class); System.out.println("jsonVal0 :" + gson.toJson(myClassInstance0)); // output: jsonVal0 :{"id":5382,"name":"Mary"} System.out.println("jsonVal1 :" + gson.toJson(myClassInstance1)); // output: jsonVal1 :{"id":2341,"name":"Bob"}
import java.io.FileReader; import java.util.ArrayList; import com.google.gson.Gson; public class Foo { public static void main(String[] args) throws Exception { Gson gson = new Gson(); TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class); System.out.println(gson.toJson(myTypes)); } } class TypeDTO { int id; String name; ArrayList<ItemDTO> items; } class ItemDTO { int id; String name; Boolean valid; }
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);
public static void main(String[] args) { ObixBaseObj lobbyObj = new ObixBaseObj(); lobbyObj.setIs("obix:Lobby"); ObixOp batchOp = new ObixOp(); batchOp.setName("batch"); batchOp.setIn("obix:BatchIn"); batchOp.setOut("obix:BatchOut"); lobbyObj.addChild(batchOp); Gson gson = GsonUtils.getGson(); System.out.println(gson.toJson(lobbyObj)); }
@Test public void shouldHandlePolymorphismWhenDeserializingAntTask() { CRTask value = antTask; String json = gson.toJson(value); CRBuildTask deserializedValue = (CRBuildTask)gson.fromJson(json,CRTask.class); assertThat("Deserialized value should equal to value before serialization", deserializedValue,is(value)); } @Test
public static String toPrettyJsonString(Object obj) { Gson gson2 = new GsonBuilder().setPrettyPrinting().create(); return gson2.toJson(obj); }