mercredi 19 avril 2017

How to avoid the if-else idiom to handle object or array

In the scope of Gson handle object or array I have a similar problem where a property may be of one of three different types: JsonPrimitive, JsonObject or JsonArray.

However I would like to avoid the if-else idiom and I cannot found any other alternative approach. Is there any solution to avoid the if-else:

if(sets.isJsonPrimitive()) {
    ...
} else if(set.isJsonArray()) {
    ...
} else {
    ...
}

To the best of my knowledge the visitor design pattern helps to solve this kind of idiomatic blocks. Yet, I did not find any support to that pattern in Gson.

Aucun commentaire:

Enregistrer un commentaire