GSON appears to be doing some kind of trick where it looks at the internal fields of my JavaBeans instead of using the publically-accessible property information. Unfortunately this won't fly for us because our magically-created beans are full of private fields which I don't want it to store off.
@Test public void testJson() throws Exception { Player player = new MagicPlayer(); //BeanUtils.createDefault(Player.class); player.setName("Alice"); Gson gson = new GsonBuilder() .registerTypeAdapter(Player.class, new PlayerTypeAdapter()) .create(); System.out.println(gson.toJson(bean)); } private static class PlayerTypeAdapter implements JsonSerializer<Player> { @Override public JsonElement serialize(Player player, Type type, JsonSerializationContext context) { throw new RuntimeException("I got called, woohoo"); } } public static interface Player //extends SupportsPropertyChanges { public String getName(); public void setName(String name); } // Simple implementation simulating what we're doing. public static class MagicPlayer implements Player { private final String privateStuff = "secret"; private String name; @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } } This gives:
{"privateStuff":"secret","name":"Alice"} And of course, never calls my type adapter, which seemingly makes it impossible to get any other behaviour.