-
Notifications
You must be signed in to change notification settings - Fork 16
@BindAdapter
xcesco edited this page Apr 6, 2018
·
5 revisions
Every supported java type in Kripton has a specific representation in its persisted state. This annotation allows to persist a field of a specific type in can be applied to a public field or a field with getter and setter. It is not used in SharedPreference and SQLite generation.
For example:
@BindType
public class Bean {
@BindXml(xmlType = XmlType.ATTRIBUTE)
public String description;
@BindXml(xmlType = XmlType.ATTRIBUTE)
public int id;
public Date date;
}
Its rapresentation in JSON:
{"date":"2017-01-23T00:35:24.728Z","description":"hello","id":0}
Its rapresentation in XML (indented for readibility reasons):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bean description="hello" id="0">
<date>2017-01-23T00:35:24.728Z</date>
</bean>
As you notice, field date
is persisted like a string 2017-01-23T00:35:24.728Z
. To persist date as a long, just change annotations and write an adapter class named DateAdapter
:
@BindType
public class Bean {
@BindXml(xmlType = XmlType.ATTRIBUTE)
public String description;
@BindXml(xmlType = XmlType.ATTRIBUTE)
public int id;
@BindAdapter(adapter = DateAdapter.class, dataType = Long.class)
public Date date;
}
public class DateAdapter implements BindTypeAdapter<Date, Long> {
@Override
public Date toJava(Long dataValue) throws Exception {
return new Date(dataValue);
}
@Override
public Long toData(Date javaValue) throws Exception {
return javaValue.getTime();
}
}
Bean's rapresentation in JSON:
{"date":1485132009409,"description":"hello","id":0}
Its rapresentation in XML (indented for readibility reasons):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bean description="hello" id="0">
<date>1485132009409</date>
</bean>
- adapter: TypeAdapter used to convert data
- Introduction
- Goals & Features
- Kotlin
- Immutable or Mutable Pojo
- Annotation Processor Args
- Credits
- Articles
- Benchmarks
- Setup
- Tutorial
- Usage
- Dependencies and inspirations
- Stackoverflow
- Documentation
- SQL logging
- Data source options
- Indices
- SQL Type adapter
- Global SQL Type adapter
- Constraints
- Live data: welcome Architectural components!!
- Paged Live data
- Dynamic parts
- Transactional and batch operations
- Async Transactional and batch operations
- Global transaction
- Support for immutable POJO
- Generate Content provider
- Generate Database schema generation
- Database migration
- BindSqlColumn
- BindContentProvider
- BindContentProviderEntry
- BindContentProviderPath
- BindDao
- BindDaoMany2Many
- BindDataSource
- BindDataSourceOptions
- BindDataSourceUpdateTask
- BindIndex
- BindSqlRelation
- BindSqlAdapter
- BindSqlChildSelect
- BindSqlDelete
- BindSqlDynamicOrderBy
- BindSqlDynamicWhere
- BindSqlDynamicWhereParams
- BindSqlInsert
- BindSqlPageSize
- BindSqlParam
- BindSqlSelect
- BindSqlUpdate
- BindSqlType
- BindSqlTransaction