CcmNG: Custom JSON converters for ComponentModels

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5663 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2018-08-08 14:55:52 +00:00
parent a4865fadf3
commit 50ccf39227
6 changed files with 80 additions and 51 deletions

View File

@ -59,16 +59,21 @@ public class CategorizedItemComponentJsonBuilder
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
public void fromJson(final JsonObject jsonObject,
final ComponentModel componentModel) {
Objects.requireNonNull(jsonObject);
final CategorizedItemComponent component = new CategorizedItemComponent();
if (!(componentModel instanceof CategorizedItemComponent)) {
throw new IllegalArgumentException(
"This converter only processes CategorizedItemComponents.");
}
final CategorizedItemComponent component
= (CategorizedItemComponent) componentModel;
readBasePropertiesFromJson(jsonObject, component);
readContentItemComponentPropertiesFromJson(jsonObject, component);
return component;
}
}

View File

@ -61,21 +61,30 @@ public class CategoryTreeComponentJsonConverter
return builder.build();
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
public void fromJson(final JsonObject jsonObject,
final ComponentModel componentModel) {
Objects.requireNonNull(jsonObject);
final CategoryTreeComponent categoryTree = new CategoryTreeComponent();
if (!(componentModel instanceof CategoryTreeComponent)) {
throw new IllegalArgumentException(
"This converter only processes CategoryTreeComponents.");
}
final CategoryTreeComponent categoryTree
= (CategoryTreeComponent) componentModel;
if (!(componentModel instanceof CategoryTreeComponent)) {
throw new IllegalArgumentException(
"This converter only processes CategoryTreeComponents.");
}
readBasePropertiesFromJson(jsonObject, categoryTree);
if (!jsonObject.isNull(SHOW_FULL_TREE)) {
categoryTree.setShowFullTree(jsonObject.getBoolean(SHOW_FULL_TREE));
}
return categoryTree;
}
}

View File

@ -72,12 +72,18 @@ public class FixedContentItemComponentJsonConverter
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
public void fromJson(final JsonObject jsonObject,
final ComponentModel componentModel) {
Objects.requireNonNull(jsonObject);
if (!(componentModel instanceof FixedContentItemComponent)) {
throw new IllegalArgumentException(
"This converter only processes FixedContentItemComponents.");
}
final FixedContentItemComponent component
= new FixedContentItemComponent();
= (FixedContentItemComponent) componentModel;
readBasePropertiesFromJson(jsonObject, component);
readContentItemComponentPropertiesFromJson(jsonObject, component);
@ -92,10 +98,7 @@ public class FixedContentItemComponentJsonConverter
.orElseThrow(() -> new UnexpectedErrorException(
String.format("No ContentItem with UUID \"%s\" exists.",
uuid))));
}
return component;
}
}

View File

@ -59,16 +59,21 @@ public class GreetingItemComponentJsonConverter
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
public void fromJson(final JsonObject jsonObject,
final ComponentModel componentModel) {
Objects.requireNonNull(jsonObject);
final GreetingItemComponent component = new GreetingItemComponent();
if (!(componentModel instanceof GreetingItemComponent)) {
throw new IllegalArgumentException(
"This converter only processes GreetingItemComponents.");
}
final GreetingItemComponent component
= (GreetingItemComponent) componentModel;
readBasePropertiesFromJson(jsonObject, component);
readContentItemComponentPropertiesFromJson(jsonObject, component);
return component;
}
}

View File

@ -78,11 +78,17 @@ public class ItemListComponentJsonConverter
}
@Override
public ComponentModel fromJson(final JsonObject jsonObject) {
public void fromJson(final JsonObject jsonObject,
final ComponentModel componentModel) {
Objects.requireNonNull(jsonObject);
final ItemListComponent itemList = new ItemListComponent();
if (!(componentModel instanceof ItemListComponent)) {
throw new IllegalArgumentException(
"This implementation does only handle ItemListComponents.");
}
final ItemListComponent itemList = (ItemListComponent) componentModel;
readBasePropertiesFromJson(jsonObject, itemList);
if (!jsonObject.isNull(DESCENDING)) {
@ -107,8 +113,6 @@ public class ItemListComponentJsonConverter
.collect(Collectors.toList()));
}
return itemList;
}
}

View File

@ -31,16 +31,19 @@ public interface ComponentModelJsonConverter {
* Convert a {@link ComponentModel} to JSON.
*
* @param componentModel The {@link ComponentModel} to convert.
*
* @return The JSON representation of the provided {@link ComponentModel}.
*/
JsonObject toJson(ComponentModel componentModel);
/**
* Read the values of a {@link ComponentModel} from a JSON object.
* Read the values of a {@link ComponentModel} from a JSON object and set
* them on the provided component model.
*
* @param jsonObject The JSON object with the values.
* @return The {@link ComponentModel}.
* @param componentModel The {@link ComponentModel} on which the values are
* set.
*/
ComponentModel fromJson(JsonObject jsonObject);
void fromJson(JsonObject jsonObject, ComponentModel componentModel);
}