This should be doable using a custom deserializer.
A relationship in my opinion should be modeled as a proper java class with proper names. Note that the constructor takes a JSONNode
as an argument and that I have left our any getters and setters:
public class Relationship {
private final String id1;
private final String id2;
private final Relation relation;
private final boolean careTaker;
private final boolean liveTogether;
public Relationship(JsonNode base) {
this.id1 = base.get(0).asText();
this.id2 = base.get(2).asText();
this.relation = Relation.valueOf(base.get(1).asText());
this.careTaker = base.get(3).get("careTaker").asBoolean();
this.liveTogether = base.get(3).get("liveTogether").asBoolean();
}
public enum Relation {
PARENT,
SPOUSE;
}
}
We also need a class which stores the collection. This is the one that you would deserialize the top level object into (again leaving out getters and setters):
@JsonDeserialize( using = FamillyRelationshipsDeserializer.class )
public class FamillyRelationships {
public List<Relationship> familyRelationships = new ArrayList<>();
}
Finally we need to implement the actual JsonDeserializer
referenced in the above class. It should look something like the following. I used this question as a reference:
class FamillyRelationshipsDeserializer extends JsonDeserializer<FamillyRelationships> {
@Override
public FamillyRelationships deserialize(JsonParser jp, DeserializationContext ctxt) throws
IOException, JsonProcessingException {
FamillyRelationships relationships = new FamillyRelationships();
JsonNode node = jp.readValueAsTree();
JsonNode rels = node.get("familyRelationships");
for (int i = 0; i < rels.size(); i++) {
relationships.familyRelationships.add(new Relationship(rels.get(i));
}
return relationships;
}
}
I hope this helps, I haven't actually tested any of this, it probably will not even compile, but the principles should be right. I have also assumed that the JSON is of the format you supplied. If that's not a guarantee then you will need to make the proper checks and deal with any deviations.
If you also want to be able to serialize everything then you will need to implement JsonSerializer
see this question for more details.