@Test public void serialize() { final String expectedString = "{\"world\":{\"name\":\"main\"},\"x\":0,\"y\":0}"; World world = mock(World.class); when(world.getName()).thenReturn("main"); TestClass testClass = new TestClass(world); Gson gson = new GsonBuilder().registerTypeAdapter(World.class, new WorldSerializer()).create(); assertEquals(expectedString, gson.toJson(testClass)); }
@Override public JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", world.getName()); return jsonObject; }
WorldSerializer implements JsonSerializer<World> { @Override public JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", world.getName()); return jsonObject; } }
WorldSerializer implements JsonSerializer<World> { @Override public JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", world.getName()); return jsonObject; }  }
WorldSerializer implements JsonSerializer<World> { @Override public JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", world.getName()); return jsonObject; }  @Override JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext); }
WorldSerializer implements JsonSerializer<World> { @Override public JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", world.getName()); return jsonObject; }  @Override JsonElement serialize(World world, Type type, JsonSerializationContext jsonSerializationContext);  }