@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public DtoContainer<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!json.isJsonObject())
throw new JsonParseException("Expected top-level json to be an object");
JsonObject root = json.getAsJsonObject();
if (root.entrySet().size() != 1)
throw new JsonParseException("Expected top-level json to have a single property");
Entry<String, JsonElement> child = root.entrySet().iterator().next();
if (child.getValue().isJsonArray()) {
// assuming we're dealing with multiples, expect plural name
JsonArray array = child.getValue().getAsJsonArray();
Class<? extends Dto> type = pluralDtoMap.get(child.getKey());
if (type == null)
throw new JsonParseException("Unexpected property name: " + child.getKey());
List<Dto> dtos = new ArrayList<Dto>(array.size());
for (JsonElement el: array)
dtos.add((Dto) context.deserialize(el, type));
return new DtoContainer(type, dtos);
} else if (child.getValue().isJsonObject()) {
// assume we're dealing with a single object
Class<? extends Dto> type = singleDtoMap.get(child.getKey());
if (type == null)
throw new JsonParseException("Unexpected property name: " + child.getKey());
Dto wrapped = context.deserialize(child.getValue(), type);
return new DtoContainer(type, wrapped);
}
throw new JsonParseException("Expected top-level json to have a single property that's an object or array");
}