I'm dealing with the strange javax.json
library. So here's the problem:
I need to cast an Object
of type JsonValue
to either JsonObject
or JsonArray
so I can call the methods getJsonObject
and getJsonArray
of it. Both JsonArray
and JsonObject
have the same method names with the same functionalities but they're not implemented methods, they are methods defined on each of them! See: JsonObject, JsonArray.
The obvious solution would be to verify the type and then cast depending on the verified type, like this:
if (current.getValueType().equals(JsonValue.ValueType.OBJECT)) {
current = ((JsonObject) current).getJsonObject(node);
} else if (current.getValueType().equals(JsonValue.ValueType.ARRAY)) {
current = ((JsonArray) current).getJsonObject(node);
}
but it'd require too many repetitions on my code. So I ask:
1) If both JsonObject
and JsonArray
have the same methods, why they're not implementations of some interface?
2) Is there a more elegant way to cast the object to JsonObject
or JsonArray
at the same time by using some trick? Do you know any way to make this situation better?