GreenDao custom type converter and code obfuscation configuration in the Android ORM Series

Source: Internet
Author: User

GreenDao custom type converter and code obfuscation configuration in the Android ORM Series

Sometimes, our entity classes have some attributes, but there is no corresponding type in the database. In this case, we need to customize the converter for type conversion. The most common processing is the Date type. Generally, if the precision requirement is not high, the data will be converted to the Long type for storage. During reading, the data will be converted to the Date type. If we want to store the time string directly, GreenDao provides us with support for custom type conversion.

First, we need to implementPropertyConverterInterface to implement the two methodsConvertToEntityPropertyAndConvertToDatabaseValueThis interface requires two generic parameters. The first parameter is the type in the object class, and the second parameter is the type stored in the database. Now, if we need to convert Date to String for storage, the interface implementation should be like this.

public class DateStringConverter implements PropertyConverter
  
    {    @Override    public Date convertToEntityProperty(String databaseValue) {        return null;    }    @Override    public String convertToDatabaseValue(Date entityProperty) {        return bull;    }}
  

Next we will convert

public class DateStringConverter implements PropertyConverter
  
    {    private static final String DEFAULT_FORMAT=yyyy-MM-dd HH:mm:ss;    @Override    public Date convertToEntityProperty(String databaseValue) {        return convert2Date(databaseValue,DEFAULT_FORMAT);    }    @Override    public String convertToDatabaseValue(Date entityProperty) {        return convert2String(entityProperty,DEFAULT_FORMAT);    }    public static String convert2String(Date date,String format){        String currentDate=null;        try {            SimpleDateFormat formatter=new SimpleDateFormat(format);            currentDate=formatter.format(date);        }catch (Exception e){            e.printStackTrace();        }        return currentDate;    }    public static Date convert2Date(String day, String format) {        if (day == null || format == null)            return null;        SimpleDateFormat formatter = new SimpleDateFormat(format);        try {            Date dt = formatter.parse(day);            return dt;        } catch (ParseException e) {            e.printStackTrace();        }        return null;    }}
  

Generate entity classes later

Entity type = schema.addEntity(Demo);type.addStringProperty(test).customType(java.util.Date,cn.edu.zafu.greendao.db.converter.DateStringConverter);

AddProperty is the type in the corresponding database. Here we store it as a string, so it isAddStringPropertyAnd thenCustomTypeThe function specifies the type in the object class. Here isJava. util. DateAnd then the full class name of our type converter. Next, try to insert a piece of data into the database. We will find that it is stored directly as a string,

Add the following statements to the project to be obfuscated: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> -keepclassmembers class * extends de.greenrobot.dao.AbstractDao { public static java.lang.String TABLENAME;}-keep class **$Properties

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.