Skip to content

Instantly share code, notes, and snippets.

@jripault
Last active December 17, 2015 16:59
Show Gist options
  • Save jripault/5642381 to your computer and use it in GitHub Desktop.
Save jripault/5642381 to your computer and use it in GitHub Desktop.
Verify that Jackson can de/serialize an object
@Test
public void testMapper(){
List<Note> notes= new ArrayList<Note>();
HibernateAwareObjectMapper mapper = new HibernateAwareObjectMapper();
String jsonInput =
"{\n" +
" \"type\" : \"org.samil.catalog.model.event.RecordEvent\",\n" +
" \"id\" : null,\n" +
" \"userId\" : null,\n" +
" \"structureId\" : null,\n" +
" \"date\" : null,\n" +
" \"nature\" : null,\n" +
" \"notes\" : [ {\n" +
" \"type\" : \"org.samil.catalog.model.event.note.RecordNote\",\n" +
" \"id\" : null,\n" +
" \"title\" : \"bouh\",\n" +
" \"content\" : null,\n" +
" \"noteType\" : \"GENERIC\",\n" +
" \"recordId\" : null\n" +
" },\n" +
"\t{\n" +
" \"type\" : \"org.samil.catalog.model.event.note.PartnerNote\",\n" +
" \"id\" : null,\n" +
" \"title\" : \"bouh2\",\n" +
" \"content\" : null,\n" +
" \"noteType\" : \"GENERIC\",\n" +
" \"partnerId\" : null\n" +
" }],\n" +
" \"draft\" : true,\n" +
" \"firstMeeting\" : false,\n" +
" \"recordId\" : 1\n" +
"}";
try {
Event event = mapper.readValue(jsonInput, RecordEvent.class);
System.out.print("");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
public class HibernateAwareObjectMapper extends ObjectMapper {
public HibernateAwareObjectMapper() {
Hibernate4Module hm = new Hibernate4Module();
registerModule(hm);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment