Skip to content

Bean property as array/list and inner beans #395

@gamerover98

Description

@gamerover98

The problem
It is not easy to create a list or array of BeanProperty.
At the moment, I have found this solution:

@Getter @Setter // Lombok
public class Country implements ParameterizedType {

    private static final Type[] TYPE_ARGUMENTS = {
            String.class, // name type
            Integer.TYPE, // total population type
    };

    private String name;
    private int totalPopulation;
    ...

    @Override
    public Type[] getActualTypeArguments() {
        return TYPE_ARGUMENTS;
    }

    @Override
    public Type getRawType() {
        return ItemProperty.class;
    }

    @Override
    public Type getOwnerType() {
        return null;
    }
}
public static final Property<Country[]> COUNTRIES =
            new PropertyBuilder.ArrayPropertyBuilder<>(
                    new BeanPropertyType<>(
                            new TypeInformation(new Country()), DefaultMapper.getInstance()),
                    Country[]::new)
                    .path("country.list")
                    .defaultValue(new Country[0])
                    .build();

Now, if you want to add other info like a bean list called "City", it won't be serialized:

@Getter @Setter // Lombok
public class City implements ParameterizedType {

    private static final Type[] TYPE_ARGUMENTS = {
            String.class, // city type
            Integer.TYPE, // population type
    };

    private String name;
    private int population;
    ...
}
@Getter @Setter // Lombok
public class Country implements ParameterizedType {

    private static final Type[] TYPE_ARGUMENTS = {
            String.class, // name type
            Integer.TYPE, // total population type
            City[].class, // the city array type
    };

    private String name;
    private int totalPopulation;
    private City[] cities;
    ...
}

The YAML result will be:

country.list:
  - name: United States
    totalPopulation: 331000000
    cities: {}

But what I want is:

country.list:
  - name: United States
    totalPopulation: 331000000
    cities:
    - name: New York
      population: 8400000
    - name: Las Vegas
      population: 646700

A solution (?)
Maybe, define a BeanListProperty that extends ListPropery and grants an easy way to add beans without map field types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions