Skip to content

@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:

Bean

@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:

new version Bean

@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;
}

DateAdapter

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>

Attributes

  • adapter: TypeAdapter used to convert data

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally