Example usage for com.fasterxml.jackson.databind ObjectMapper findAndRegisterModules

List of usage examples for com.fasterxml.jackson.databind ObjectMapper findAndRegisterModules

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper findAndRegisterModules.

Prototype

public ObjectMapper findAndRegisterModules() 

Source Link

Document

Convenience method that is functionally equivalent to: mapper.registerModules(mapper.findModules());

As with #findModules() , no caching is done for modules, so care needs to be taken to either create and share a single mapper instance; or to cache introspected set of modules.

Usage

From source file:org.openlmis.fulfillment.web.BaseWebIntegrationTest.java

<T> List<T> getPageContent(Page page, Class<T> type) {
    List content = page.getContent();

    if (isEmpty(content)) {
        // nothing to do
        return Collections.emptyList();
    }/*from  w  w w. j av  a2  s  .  co m*/

    if (type.isInstance(content.get(0))) {
        // content contains instances of the given type
        return Collections.checkedList(content, type);
    }

    if (content.get(0) instanceof Map) {
        // jackson does not convert json to correct type
        // instead it use default operation and objects are
        // represented by Map

        // We have to do manually convert map to the given type =(
        ObjectMapper mapper = new ObjectMapper();
        mapper.findAndRegisterModules();

        TypeFactory factory = mapper.getTypeFactory();
        CollectionLikeType collectionType = factory.constructCollectionLikeType(List.class, type);

        return mapper.convertValue(content, collectionType);
    }

    throw new IllegalStateException("the page content contains unsupported type");
}

From source file:org.apereo.portal.events.aggr.JpaStatisticalSummaryTest.java

public void testStorelessUnivariateStatistic(StorelessUnivariateStatistic sus, double expected)
        throws Exception {

    assertEquals(expected, sus.getResult(), 0.1);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();

    //Configure Jackson to just use fields
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);

    mapper.addMixInAnnotations(Object.class, IgnoreTypeMixIn.class);

    final FilterProvider filters = new SimpleFilterProvider().addFilter("storedDataFilter",
            SimpleBeanPropertyFilter.serializeAllExcept("storedData"));

    final ObjectWriter ssWriter = mapper.writer(filters);
    final ObjectReader ssReader = mapper.reader(sus.getClass());

    final String susString = ssWriter.writeValueAsString(sus);
    System.out.println(susString);
    final StorelessUnivariateStatistic newSus = ssReader.readValue(susString);

    assertEquals(expected, newSus.getResult(), 0.1);
}

From source file:xyz.codevomit.bootlog.blog.BackupControllerTest.java

@Test
@WithMockUser(username = "merka", password = "merka")
public void testExport() throws Exception {
    MvcResult mvcResult = mockMvc.perform(post("/backup/export").with(csrf())).andExpect(status().isOk())
            .andReturn();// w  w  w  . ja v  a2 s  .  co  m
    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();

    log.info("Here's the response content: {}", mvcResult.getResponse().getContentAsString());

    List<Post> deserializedPosts = mapper.readValue(mvcResult.getResponse().getContentAsString(),
            new TypeReference<List<Post>>() {
            });

    assertFalse(deserializedPosts.isEmpty());
}

From source file:xyz.codevomit.bootlog.service.BackupServiceTest.java

@Test
public void testBackupPostsToJSON() throws IOException {
    List<Post> testPosts = testBuilder.insertTestPosts(4);

    BackupService service = new BackupService(postRepo);

    String json = service.backupPostsToJSON();

    assertNotNull(json);// w  w  w. ja  va 2 s  .  c  o m
    log.info(json);
    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();
    List<Post> readBackPosts = mapper.readValue(json, new TypeReference<List<Post>>() {
    });
    assertNotNull(readBackPosts);
    assertFalse(readBackPosts.isEmpty());
    assertEquals(4, readBackPosts.size());

    //cleanup
    postRepo.delete(testPosts);
}