ftl.rest.$$RestModelToJsonConverterTemplateftl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxmicro-annotation-processor-rest Show documentation
Show all versions of rxmicro-annotation-processor-rest Show documentation
The RxMicro Annotation Processor internal module that generates common REST components.
The newest version!
<#include "../common-lib.javaftl">
<#-- -------------------------------------------------------------------------------------------------------- -->
public final class ${JAVA_MODEL_CLASS.modelToJsonConverterImplSimpleClassName} extends ModelToJsonConverter<${JAVA_MODEL_CLASS.javaSimpleClassName}> {
<#if HAS_PARENT>
private final ${PARENT} parentConverter =
new ${PARENT}();
#if>
<#list JAVA_MODEL_CONVERTER_CHILDREN as CHILD>
<#assign className=CHILD.javaSimpleClassName>
private final ${CHILD.modelToJsonConverterImplSimpleClassName} ${CHILD.toJsonConverterInstanceName} =
new ${CHILD.modelToJsonConverterImplSimpleClassName}();
#list>
<#if JAVA_MODEL_CLASS.modelClassReturnedByRestMethod>
@Override
public Map toJsonObject(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model) {
final JsonObjectBuilder builder = new JsonObjectBuilder();
<#if JAVA_MODEL_CLASS.paramEntriesPresentAtThisOrAnyParent>
putValuesToBuilder(model, builder);
#if>
return builder.build();
}
#if>
<#if JAVA_MODEL_CLASS.paramEntriesPresentAtThisOrAnyParent>
public void putValuesToBuilder(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model,
final JsonObjectBuilder builder) {
<#if HAS_PARENT>
parentConverter.putValuesToBuilder(model, builder);
#if>
<#list JAVA_MODEL_CLASS.paramEntries as fieldEntry>
<@putJsonEntry entry=fieldEntry/>
#list>
}
#if>
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro putJsonEntry entry>
<#if entry.value.object>
<#assign element = entry.value>
<#if entry.key.modelReadAccessorType.name() == "REFLECTION">
builder.put("${entry.key.modelName}", convertFromObjectIfNotNull(${element.toJsonConverterInstanceName}, (${element.javaSimpleClassName}) getFieldValue(model, "${entry.key.fieldName}")));
<#elseif entry.key.modelReadAccessorType.name() == "DIRECT">
builder.put("${entry.key.modelName}", convertFromObjectIfNotNull(${element.toJsonConverterInstanceName}, model.${entry.key.fieldName}));
<#elseif entry.key.modelReadAccessorType.name() == "JAVA_BEAN">
builder.put("${entry.key.modelName}", convertFromObjectIfNotNull(${element.toJsonConverterInstanceName}, model.${entry.key.getter}()));
#if>
<#elseif entry.value.iterable>
<#if entry.value.primitiveIterable || entry.value.enumIterable>
<#if entry.key.modelReadAccessorType.name() == "REFLECTION">
builder.put("${entry.key.modelName}", (${entry.key.fieldSimpleType}) getFieldValue(model, "${entry.key.fieldName}"));
<#elseif entry.key.modelReadAccessorType.name() == "DIRECT">
builder.put("${entry.key.modelName}", model.${entry.key.fieldName});
<#elseif entry.key.modelReadAccessorType.name() == "JAVA_BEAN">
builder.put("${entry.key.modelName}", model.${entry.key.getter}());
#if>
<#elseif entry.value.objectIterable>
<#assign element = entry.value.elementModelClass>
<#assign containerType = entry.value.containerType>
<#if entry.key.modelReadAccessorType.name() == "REFLECTION">
builder.put("${entry.key.modelName}", convertFrom${containerType}IfNotNull(${element.toJsonConverterInstanceName}, (${entry.value.javaSimpleClassName}) getFieldValue(model, "${entry.key.fieldName}")));
<#elseif entry.key.modelReadAccessorType.name() == "DIRECT">
builder.put("${entry.key.modelName}", convertFrom${containerType}IfNotNull(${element.toJsonConverterInstanceName}, model.${entry.key.fieldName}));
<#elseif entry.key.modelReadAccessorType.name() == "JAVA_BEAN">
builder.put("${entry.key.modelName}", convertFrom${containerType}IfNotNull(${element.toJsonConverterInstanceName}, model.${entry.key.getter}()));
#if>
#if>
<#else>
<#if entry.key.modelReadAccessorType.name() == "REFLECTION">
builder.put("${entry.key.modelName}", (${entry.key.fieldSimpleType}) getFieldValue(model, "${entry.key.fieldName}"));
<#elseif entry.key.modelReadAccessorType.name() == "DIRECT">
builder.put("${entry.key.modelName}", model.${entry.key.fieldName});
<#elseif entry.key.modelReadAccessorType.name() == "JAVA_BEAN">
builder.put("${entry.key.modelName}", model.${entry.key.getter}());
#if>
#if>
#macro>
<#-- -------------------------------------------------------------------------------------------------------- -->