All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ftl.rest.$$RestModelToJsonConverterTemplateftl Maven / Gradle / Ivy

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}();
    
    <#list JAVA_MODEL_CONVERTER_CHILDREN as CHILD>
    <#assign className=CHILD.javaSimpleClassName>

    private final ${CHILD.modelToJsonConverterImplSimpleClassName} ${CHILD.toJsonConverterInstanceName} =
            new ${CHILD.modelToJsonConverterImplSimpleClassName}();
    
    <#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);
        
        return builder.build();
    }
    
    <#if JAVA_MODEL_CLASS.paramEntriesPresentAtThisOrAnyParent>

    public void putValuesToBuilder(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model,
                                   final JsonObjectBuilder builder) {
        <#if HAS_PARENT>
        parentConverter.putValuesToBuilder(model, builder);
        
        <#list JAVA_MODEL_CLASS.paramEntries as fieldEntry>
        <@putJsonEntry entry=fieldEntry/>
        
    }
    
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#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}()));
        
    <#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}());
            
        <#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}()));
            
        
    <#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}());
        
    

<#-- -------------------------------------------------------------------------------------------------------- -->




© 2015 - 2024 Weber Informatics LLC | Privacy Policy