diff --git a/Java/Class.java.twig b/Java/Class.java.twig index e1c1096..8fdd5ca 100644 --- a/Java/Class.java.twig +++ b/Java/Class.java.twig @@ -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(); diff --git a/Java/blocks/class/converters.twig b/Java/blocks/class/converters.twig new file mode 100644 index 0000000..352d1ad --- /dev/null +++ b/Java/blocks/class/converters.twig @@ -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 value) throws IOException { + return LoganSquare.serialize(value); + } + + @TypeConverter + public static Map 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 -%} diff --git a/Java/blocks/class/fields.twig b/Java/blocks/class/fields.twig index 29ba24b..6656acb 100644 --- a/Java/blocks/class/fields.twig +++ b/Java/blocks/class/fields.twig @@ -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 }}")