CcmNG: Custom JSON converters for ComponentModels
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5663 8810af33-2d31-482b-a856-94f89814c4df
Former-commit-id: b7e4e9e0d2
pull/2/head
parent
00ffbe9ac8
commit
31f8bec007
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue