Java Code Examples for com.fasterxml.jackson.databind.type.TypeFactory#constructCollectionType()

The following examples show how to use com.fasterxml.jackson.databind.type.TypeFactory#constructCollectionType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDataProviderEngine.java    From n2o-framework with Apache License 2.0 7 votes vote down vote up
private List<DataSet> loadJson(InputStream is, PrimaryKeyType primaryKeyType, String primaryKeyFieldId) throws IOException {
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    CollectionType collectionType = typeFactory.constructCollectionType(
            List.class, DataSet.class);
    List<DataSet> dataList = objectMapper.readValue(is, collectionType);
    for (DataSet data : dataList) {
        if (data.containsKey(primaryKeyFieldId) && integer.equals(primaryKeyType))
            data.put(primaryKeyFieldId, ((Number) data.get(primaryKeyFieldId)).longValue());
    }
    return dataList;
}
 
Example 2
Source File: TestDataProviderEngineTest.java    From n2o-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteOnFile() throws IOException {
    TestDataProviderEngine engine = new TestDataProviderEngine();
    engine.setResourceLoader(new DefaultResourceLoader());
    engine.setPathOnDisk(testFolder.getRoot() + "/");

    N2oTestDataProvider provider = new N2oTestDataProvider();
    provider.setFile(testFile.getName());

    //Удаление данных
    provider.setOperation(delete);

    Map<String, Object> inParamsForDelete = new LinkedHashMap<>();
    inParamsForDelete.put("id", 1L);

    engine.invoke(provider, inParamsForDelete);

    //Проверка, что после delete, json файл содержит ожидаемые данные
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    CollectionType collectionType = typeFactory.constructCollectionType(
            List.class, HashMap.class);
    List<Map> result = objectMapper.readValue(testFile, collectionType);

    assertThat(result.size(), is(0));
}
 
Example 3
Source File: RestServiceImpl.java    From peer-os with Apache License 2.0 6 votes vote down vote up
private Map<String, ContainerQuota> getChangedContainers( final String quotaContainers ) throws java.io.IOException
{
    Map<String, ContainerQuota> changedContainersFiltered = new HashMap<>();
    TypeFactory typeFactory = mapper.getTypeFactory();
    CollectionType arrayType = typeFactory.constructCollectionType( ArrayList.class, ChangedContainerDto.class );
    List<ChangedContainerDto> changedContainers = mapper.readValue( quotaContainers, arrayType );
    for ( ChangedContainerDto cont : changedContainers )
    {
        ContainerQuotaDto containerQuotaDto = cont.getQuota();
        ContainerSize containerSize = containerQuotaDto.getContainerSize();
        ContainerQuota defaultQuota = ContainerSize.getDefaultContainerQuota( containerSize );
        if ( containerSize == ContainerSize.CUSTOM )
        {
            defaultQuota = containerQuotaDto.getContainerQuota();
        }

        changedContainersFiltered.put( cont.getHostId(), defaultQuota );
    }
    return changedContainersFiltered;
}
 
Example 4
Source File: TestDataProviderEngineTest.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateOnFile() throws IOException {
    TestDataProviderEngine engine = new TestDataProviderEngine();
    engine.setResourceLoader(new DefaultResourceLoader());
    engine.setPathOnDisk(testFolder.getRoot() + "/");

    N2oTestDataProvider provider = new N2oTestDataProvider();
    provider.setFile(testFile.getName());

    //Добавление новых данных
    provider.setOperation(create);

    Map<String, Object> inParamsForCreate = new LinkedHashMap<>();
    inParamsForCreate.put("id", 9L);
    inParamsForCreate.put("name", "test9");
    inParamsForCreate.put("type", "9");

    engine.invoke(provider, inParamsForCreate);

    //Проверка, что после create, json файл содержит ожидаемые данные
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    CollectionType collectionType = typeFactory.constructCollectionType(
            List.class, HashMap.class);
    List<Map> result = objectMapper.readValue(testFile, collectionType);

    assertThat(result.size(), is(2));
    assertThat(result.get(0).get("id"), is(9));
    assertThat(result.get(0).get("name"), is("test9"));
    assertThat(result.get(0).get("type"), is("9"));
    assertThat(result.get(1).get("id"), is(1));
    assertThat(result.get(1).get("name"), is("test1"));
    assertThat(result.get(1).get("type"), is("1"));
}
 
Example 5
Source File: TestDataProviderEngineTest.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateOnFile() throws IOException {
    TestDataProviderEngine engine = new TestDataProviderEngine();
    engine.setResourceLoader(new DefaultResourceLoader());
    engine.setPathOnDisk(testFolder.getRoot() + "/");

    N2oTestDataProvider provider = new N2oTestDataProvider();
    provider.setFile(testFile.getName());

    //Обновление данных
    provider.setOperation(update);

    Map<String, Object> inParamsForUpdate = new LinkedHashMap<>();
    inParamsForUpdate.put("id", 1L);
    inParamsForUpdate.put("name", "test9");
    inParamsForUpdate.put("type", "9");

    engine.invoke(provider, inParamsForUpdate);

    //Проверка, что после update, json файл содержит ожидаемые данные
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    CollectionType collectionType = typeFactory.constructCollectionType(
            List.class, HashMap.class);
    List<Map> result = objectMapper.readValue(testFile, collectionType);

    assertThat(result.size(), is(1));
    assertThat(result.get(0).get("id"), is(1));
    assertThat(result.get(0).get("name"), is("test9"));
    assertThat(result.get(0).get("type"), is("9"));
}
 
Example 6
Source File: TestDataProviderEngineTest.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateFieldOnFile() throws IOException {
    TestDataProviderEngine engine = new TestDataProviderEngine();
    engine.setResourceLoader(new DefaultResourceLoader());
    engine.setPathOnDisk(testFolder.getRoot() + "/");

    N2oTestDataProvider provider = new N2oTestDataProvider();
    provider.setFile(testFile.getName());

    //Обновление данных поля
    provider.setOperation(updateField);

    Map<String, Object> inParamsForUpdate = new LinkedHashMap<>();
    inParamsForUpdate.put("id", 1L);
    inParamsForUpdate.put("key", "name");
    inParamsForUpdate.put("value", "newName");

    engine.invoke(provider, inParamsForUpdate);

    //Проверка, что после updateField, json файл содержит ожидаемые данные
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    CollectionType collectionType = typeFactory.constructCollectionType(
            List.class, HashMap.class);
    List<Map> result = objectMapper.readValue(testFile, collectionType);

    assertThat(result.size(), is(1));
    assertThat(result.get(0).get("id"), is(1));
    assertThat(result.get(0).get("name"), is("newName"));
    assertThat(result.get(0).get("type"), is("1"));
}
 
Example 7
Source File: TestDataProviderEngineTest.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateOnEmptyFile() throws IOException {
    TestDataProviderEngine engine = new TestDataProviderEngine();
    engine.setResourceLoader(new DefaultResourceLoader());
    engine.setPathOnDisk(testFolder.getRoot() + "/");

    N2oTestDataProvider provider = new N2oTestDataProvider();
    provider.setFile(emptyFile.getName());

    //Добавление новых данных в пустой файл
    provider.setOperation(create);

    Map<String, Object> inParamsForCreate = new LinkedHashMap<>();
    inParamsForCreate.put("name", "test10");
    inParamsForCreate.put("type", "10");

    engine.invoke(provider, inParamsForCreate);

    //Проверка, что после create, json файл содержит ожидаемые данные
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();
    CollectionType collectionType = typeFactory.constructCollectionType(
            List.class, HashMap.class);
    List<Map> result = objectMapper.readValue(emptyFile, collectionType);

    assertThat(result.size(), is(1));
    assertThat(result.get(0).get("id"), is(1));
    assertThat(result.get(0).get("name"), is("test10"));
    assertThat(result.get(0).get("type"), is("10"));
}
 
Example 8
Source File: ListJacksonJsonTypeDetector.java    From camunda-external-task-client-java with Apache License 2.0 5 votes vote down vote up
protected JavaType constructType(Object object) {
  TypeFactory typeFactory = TypeFactory.defaultInstance();

  if (object instanceof List && !((List<?>) object).isEmpty()) {
    List<?> list = (List<?>) object;
    Object firstElement = list.get(0);
    if (bindingsArePresent(list.getClass())) {
      final JavaType elementType = constructType(firstElement);
      return typeFactory.constructCollectionType(list.getClass(), elementType);
    }
  }
  return typeFactory.constructType(object.getClass());
}
 
Example 9
Source File: ListJacksonJsonTypeDetector.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
protected JavaType constructType(Object object) {
  TypeFactory typeFactory = TypeFactory.defaultInstance();

  if (object instanceof List && !((List<?>) object).isEmpty()) {
    List<?> list = (List<?>) object;
    Object firstElement = list.get(0);
    if (bindingsArePresent(list.getClass())) {
      final JavaType elementType = constructType(firstElement);
      return typeFactory.constructCollectionType(list.getClass(), elementType);
    }
  }
  return typeFactory.constructType(object.getClass());
}
 
Example 10
Source File: KAEntityCollectionFetcherTask.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 5 votes vote down vote up
public KAEntityCollectionFetcherTask(Class<T> type, String url) {
		this.url = url;
		TypeFactory f = TypeFactory.defaultInstance();
//		this.type = new TypeReference<List<T>>() {};
//		this.type = f.constructParametricType(ArrayList.class, type);
		this.type = f.constructCollectionType(ArrayList.class, type);
	}
 
Example 11
Source File: RestServiceImpl.java    From peer-os with Apache License 2.0 4 votes vote down vote up
private List<NodeSchemaDto> parseNodes( final String nodes ) throws java.io.IOException
{
    TypeFactory typeFactory = mapper.getTypeFactory();
    CollectionType arrayType = typeFactory.constructCollectionType( ArrayList.class, NodeSchemaDto.class );
    return mapper.readValue( nodes, arrayType );
}
 
Example 12
Source File: MultimapSerializer.java    From vavr-jackson with Apache License 2.0 4 votes vote down vote up
@Override
JavaType emulatedJavaType(TypeFactory typeFactory) {
    JavaType containerType = typeFactory.constructCollectionType(ArrayList.class, mapType.getContentType());
    return typeFactory.constructMapType(LinkedHashMap.class, mapType.getKeyType(), containerType);
}
 
Example 13
Source File: ArraySerializer.java    From vavr-jackson with Apache License 2.0 4 votes vote down vote up
@Override
JavaType emulatedJavaType(TypeFactory typeFactory) {
    return typeFactory.constructCollectionType(ArrayList.class, collectionType.getContentType());
}