List and map converters

This commit is contained in:
Denis Karmyshakov 2018-10-04 19:58:27 +03:00
parent 878953885d
commit 46cbd04df0
3 changed files with 36 additions and 1 deletions

View File

@ -7,6 +7,7 @@ package {{ packageName }}.api;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bluelinelabs.logansquare.LoganSquare;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.bluelinelabs.logansquare.JsonOptional;
@ -35,6 +36,7 @@ import ru.touchin.templates.logansquare.LoganSquareJsonModel;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.arch.persistence.room.TypeConverters;
import android.arch.persistence.room.TypeConverter;
{%- endif %}
{%- if (description is not empty) %}
@ -56,9 +58,15 @@ import android.arch.persistence.room.TypeConverters;
primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if loop.index0 != 0 %}, {% endif -%} "{{- key.name -}}" {%- endfor -%} }
{%- endif -%}
)
@TypeConverters({{ type.baseTypeName }}.class)
{%- endif %}
@JsonObject(serializeNullObjects = true)
public class {% include 'blocks/class/classtype.twig' with { type: type } %} extends {% include 'blocks/class/supertype.twig' with { type: type, parent: parent } %} {
{%- if (storageAttributes is not null) -%}
{%- include 'blocks/class/converters.twig' -%}
{%- endif -%}
{% include 'blocks/class/fields.twig' with { fields: fields } %}
public {{ type.baseTypeName }}() {
super();

View File

@ -0,0 +1,27 @@
{%- import '../../utils.twig' as utils -%}
{%- for field in fields -%}
{%- if field.type.type.baseTypeName == "Array" %}
@TypeConverter
public static String serialize{{ utils.capitalize(field.name) }}(@NonNull final List<{{ utils.formatNonOptionalValueType(field.type.type.itemsType, false) }}> value) throws IOException {
return LoganSquare.serialize(value);
}
@TypeConverter
public static List<{{ utils.formatNonOptionalValueType(field.type.type.itemsType, false) }}> deserialize{{ utils.capitalize(field.name) }}(@NonNull final String value) throws IOException {
return LoganSquare.parseList(value, {{ utils.formatNonOptionalValueType(field.type.type.itemsType, false) }}.class);
}
{%- elseif field.type.type.baseTypeName == "Map" %}
@TypeConverter
public static String serialize{{ utils.capitalize(field.name) }}(@NonNull final Map<String, {{ utils.formatNonOptionalValueType(field.type.type.valuesType, false) }}> value) throws IOException {
return LoganSquare.serialize(value);
}
@TypeConverter
public static Map<String, {{ utils.formatNonOptionalValueType(field.type.type.valuesType, false) }}> deserialize{{ utils.capitalize(field.name) }}(@NonNull final String value) throws IOException {
return LoganSquare.parseMap(value, {{ utils.formatNonOptionalValueType(field.type.type.valuesType, false) }}.class);
}
{%- endif -%}
{%- endfor -%}

View File

@ -7,7 +7,7 @@
* {{ field.description }}
*/
{% endif -%}
{% if (field.type.storable) %}
{% if (storageAttributes is not null) and field.type.storable %}
@TypeConverters({{ field.type.type.baseTypeName }}.class)
{% endif -%}
@JsonField(name = "{{ field.jsonName }}")