##Type Support
###Types with Built-in Support
By default, the following types are supported by LoganSquare:
- any object using the
@JsonObject
annotation - int and Integer
- long and Long
- float and Float
- double and Double
- boolean and Boolean
- String
- Date (if formatted using the ISO 8601 standard:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
)
Additionally, the following collections are supported:
- Lists
- ArrayList
- LinkedList
- Sets
- HashSet
- Queues
- ArrayDeque
- Deques
- ArrayDeque
- Maps (with Strings as keys)
- HashMap
- TreeMap
- LinkedHashMap
- Array (Not technically a Collection, but still supported. Note that Lists are preferred to arrays when possible.)
###Support for Additional Types
Any Java object can be supported by LoganSquare, even if they don't fall into the above categories. To add support for your own types, you'll need to extend one of the built-in TypeConverter
classes. You can register your custom type converters using one of the two following ways:
LoganSquare.registerTypeConverter(Date.class, new YourConverter());
Hint: This method is especially useful for handling multiple Date formats!
@JsonObject
public class ModelObject {
@JsonField(typeConverter = YourConverter.class)
public Date speciallyConvertedDate;
}
###Example TypeConverter
s
####TypeConverter
for a custom date format
public class TimeOnlyDateConverter extends DateTypeConverter {
private DateFormat mDateFormat;
public TimeOnlyDateConverter() {
mDateFormat = new SimpleDateFormat("HH:mm");
}
public DateFormat getDateFormat() {
return mDateFormat;
}
}
####TypeConverter
for an enum, where the JSON contains an int
public enum TestEnum {
VALUE_1, VALUE_2, VALUE_3
}
public class TimeOnlyDateConverter extends IntBasedTypeConverter<TestEnum> {
@Override
public TestEnum getFromInt(int i) {
return TestEnum.values()[i];
}
public int convertToInt(TestEnum object) {
return Arrays.asList(TestEnum.values()).indexOf(TestEnum.VALUE_1);
}
}
####TypeConverter
for an enum, where the JSON contains a String
public enum TestEnum {
VALUE_1, VALUE_2, VALUE_3
}
public class EnumConverter extends StringBasedTypeConverter<TestEnum> {
@Override
public TestEnum getFromString(String s) {
TestEnum.valueOf(s);
}
public String convertToString(TestEnum object) {
return object.toString();
}
}