SnakeYAML Parsing Dynamic Key/Property Name into Map of custom object

Viewed 20

Been struggling with this problem for a bit and can't seem to get a clear answer online.

I'm loading the snakeYaml with

return new Yaml().loadAs(getResourceAsString("access-control.yaml"),
    AccessControlMap.class);

access-control.yaml

accessControlMap:
  Team1:
    Default:
      allowed_services:
        - service1
        - service2
      allowed_owners:
        - owner1
      allowed_user_roles:
        - role1
        - role2
    AccessTypeA:
      allowed_services:
        - service1
      allowed_owners:
        - owner1
      allowed_user_roles:
        - role1
  Team2:
    Default:
      allowed_services:
        - service1
        - service2
      allowed_owners:
        - owner1
      allowed_user_roles:
        - role1
        - role2

I can easily parse this into Map<String, Map<String, Map<String, List>>> with no problem but I'm trying to convert each of the map into objects.

Ideally I would have

AccessControlMap.java

private Map<String, LayerOneMap> accessControlMap;

LayerOneMap.java

private Map<String, LayerTwoMap> layerOneControlMap;

LayerTwoMap.java

private List<String> allowed_services;
private List<String> allowed_owners;
private List<String> allowed_user_roles;

I was able to get it to work with

Map<String, Map<String, LayerTwoMap>

but when I go for

Map<String, LayerOneMap>

Its showing can not fine property "Default" and thats because my objects does not have a field with the name "Default".
"Default", "AccessTypeA", "AccessTypeB"... are kind of like Dynamic Keys, I can not predefine them as a field name as different teams will have different type of access types and what not.

Any suggestions on how to work with this Dynamic Key?

0 Answers
Related