@Test public void deserialize() { Server server = mock(Server.class); Gson gson = new GsonBuilder().registerTypeAdapter(World.class, new WorldDeserializer( server )) .create(); World world = mock(World.class); when(world.getName()).thenReturn("main"); when(server.getWorld(eq("main"))).thenReturn(world); final TestClass testClass = gson.fromJson( "{\"world\":{\"name\":\"main\"},\"x\":0,\"y\":0}", TestClass.class ); assertNotNull( testClass.world); assertEquals("main", testClass.world.getName()); }
@Override public World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return server.getWorld(jsonElement.getAsJsonObject().get("name").getAsString()); }
WorldDeserializer implements JsonDeserializer<World> { @Override public World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return server.getWorld(jsonElement.getAsJsonObject().get("name").getAsString()); } }
WorldDeserializer implements JsonDeserializer<World> { @Override public World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return server.getWorld(jsonElement.getAsJsonObject().get("name").getAsString()); }  }
WorldDeserializer implements JsonDeserializer<World> { @Override public World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return server.getWorld(jsonElement.getAsJsonObject().get("name").getAsString()); }  @Override World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext); }
WorldDeserializer implements JsonDeserializer<World> { @Override public World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return server.getWorld(jsonElement.getAsJsonObject().get("name").getAsString()); }  @Override World deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext);  }