FWIW, Jackson will serialize anonymous and local classes just fine.
public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); class MethodLocal {public String key = "method local";} System.out.println(mapper.writeValueAsString(new MethodLocal()));
Note that Jackson will by default not access non-public fields through reflection, as Gson does, but it can be configured for this. Jackson's way is to use the usual Java properties (via get / set methods). (Setting it to use private fields slows down performance, a little, but still faster than Gson.)
source share