GsonObjectMapper implements ObjectMapper { @Override public String writeValue(Object value) { return om.toJson(value); } GsonObjectMapper();  GsonObjectMapper(Gson om); @Override T readValue(String value, Class<T> valueType); @Override T readValue(String value, GenericType<T> genericType); @Override String writeValue(Object value);  }
@Test public void canWrite() throws Exception { TestMe test = new TestMe("foo", 42, new TestMe("bar", 666, null)); String json = om.writeValue(test); JSONAssert.assertEquals( "{\"text\":\"foo\",\"nmbr\":42,\"another\":{\"text\":\"bar\",\"nmbr\":666}}" , json , true ); }
@Test public void serializeNulls() { Gson gson = new GsonBuilder() .serializeNulls() .create(); om = new GsonObjectMapper(gson); TestMe testMe = new TestMe(null, null, null); assertEquals("{\"text\":null,\"nmbr\":null,\"another\":null}", om.writeValue(testMe)); }