Last active
December 17, 2015 16:59
-
-
Save jripault/5642381 to your computer and use it in GitHub Desktop.
Verify that Jackson can de/serialize an object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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