I'm afraid that the solution that you are trying to achieve is not possible because colorMap
is not a property of the JSON, the JSON should be like
{
user: "me",
colorMap: {
red: {"some object"},
green: {...}
...
}
}
Then what you need right now is a custom deserializer which iterates over JSONObjects of the response. Custom JSON deserializer using Gson this is a good example of how to deserialize with custom code and GSON.
Basically you have to register a custom deserializer for the class ColorResponse which you do something like:
deserialize() {
ColorResponse response = new ColorResponse();
List<Color> colors = new ArrayList<>();
response.setName(json.getAsJsonObject().get("name").asString());
for (Entry<String, JsonElement> entry : json.entrySet()) {
if (!entry.getKey().equals("name")) {
colors.add(new Color(entry.getKey(), entry.getValue()));
}
}
}
This code is just to show how can that be done, I have written it in the fly and I am not sure if it will compile but the key is that if you will only have that name will be constant, so the rest are colors, then just add all the colors to a List, Map, or whatever you want.