String id = null;
if (jsonObject.has("id")) {
id = jsonObject.get("id").getAsString();
}
UIWidget element = elementMetadata.newInstance();
if (id != null) {
FieldMetadata fieldMetadata = elementMetadata.getField("id");
if (fieldMetadata == null) {
logger.warn("UIWidget type {} lacks id field", elementMetadata.getUri());
} else {
fieldMetadata.setValue(element, id);
}
}
// Deserialize normal fields.
for (FieldMetadata<? extends UIWidget, ?> field : elementMetadata.getFields()) {
if (jsonObject.has(field.getSerializationName())) {
if (field.getName().equals(CONTENTS_FIELD) && UILayout.class.isAssignableFrom(elementMetadata.getType())) {
continue;
}
try {
if (List.class.isAssignableFrom(field.getType())) {
Type contentType = ReflectionUtil.getTypeParameter(field.getField().getGenericType(), 0);
if (contentType != null) {
List result = Lists.newArrayList();
JsonArray list = jsonObject.getAsJsonArray(field.getSerializationName());
for (JsonElement item : list) {
result.add(context.deserialize(item, contentType));
}
field.setValue(element, result);
}
} else {
field.setValue(element, context.deserialize(jsonObject.get(field.getSerializationName()), field.getType()));
}
} catch (Throwable e) {
logger.error("Failed to deserialize field {} of {}", field.getName(), type, e);
}
}
}
// Deserialize contents and layout hints
if (UILayout.class.isAssignableFrom(elementMetadata.getType())) {
UILayout layout = (UILayout) element;
Class<? extends LayoutHint> layoutHintType = (Class<? extends LayoutHint>)
ReflectionUtil.getTypeParameter(elementMetadata.getType().getGenericSuperclass(), 0);
if (jsonObject.has(CONTENTS_FIELD)) {
for (JsonElement child : jsonObject.getAsJsonArray(CONTENTS_FIELD)) {
UIWidget childElement = context.deserialize(child, UIWidget.class);
if (childElement != null) {
LayoutHint hint = null;
if (child.isJsonObject()) {
JsonObject childObject = child.getAsJsonObject();
if (layoutHintType != null && !layoutHintType.isInterface() && !Modifier.isAbstract(layoutHintType.getModifiers())