I have a design/architecture question. I am currently trying to elegantly parse data into a single data class. But the data comes in wrapped by various protocols: A(B(C))
. The main idea is to be able to switch these at different layers (like A(D(E))
or B(C)
). But in the end I just want to return a single object with everything in it.
My current approach is to use inheritance to abstract the layers/protocols with each class. But with this approach, I am not flexible in switching the layers without having to re-code the classes. See the simplified example and example data below.
So my question is: Is there a pattern to realise this deserialisation of layered data properly while keeping the flexibility to change the layers around?
{ // Layer A
"timestamp": "2021-07-06T10:01:00.000Z",
"value": { // Layer B
"timestamp": "2021-07-06T10:00:30.000Z",
"src": "any",
"data": "some complex data string" // Layer C
}
}
class _Base { public JsonNode parse(JsonNode data) { return data; } }
class A extends _Base {
public Instant aTimestamp;
public JsonNode parse(JsonNode data) {
JsonNode remaining = super.parse(data);
aTimestamp = Instant.parse(remaining.get("timestamp").asText());
return remaining.get("value");
}
}
class B extends A {
public Instant bTimestamp;
public String src;
public JsonNode parse(JsonNode data) {
JsonNode remaining = super.parse(data);
bTimestamp = Instant.parse(remaining.get("timestamp").asText());
src = remaining.get("src").asText();
return remaining.get("data");
}
}
class C extends B {
public String[] data;
public JsonNode parse(JsonNode data) {
JsonNode remaining = super.parse(data);
this.data = remaining.asText().split(" ").
return null;
}
}
Aucun commentaire:
Enregistrer un commentaire