Acticeandroid's introduction
Activeandroid is an active, record-style ORM (Object Relational mapping) library. Activeandroid allows you to save and retrieve the SQLite database record without writing an SQL statement. Each database record is neatly wrapped into a model class, like the Save Method () and delete ().
AA use material: https://github.com/pardom/ActiveAndroid/wiki/Getting-started.
AA in the database Save,select action, can only handle basic data types, int,long,string, etc., for custom types, you need to use Typeserializer type serialization. Several kinds of Typeserializer are also implemented in AAAPI.
Problem 1:AA combined with Gson use
Gson New Gson (); LOG.D ("activeandroid", "Item ToJson:" +gson.tojson (obj));//obj inherit Com.activeandroid.Model;
The report object serialization exception will appear:
This is because the activeandroid framework needs to inherit the model Com.activeandroid.Model class, and this class seemingly can not be serialized, the specific reasons did not delve into, interested students can see activeandroid source.
After reviewing the issue on activeandroid git, developers were found to have a similar problem, but there was no effective workaround.
On-Line solution: We can solve this serialization problem by ignoring the properties of the parent class (Com.activeandroid.Model) when serializing, implementing the Com.google.gson.ExclusionStrategy interface policy to build the Gson.
Public classActiveandroidstrategyImplementsExclusionstrategy {PrivateClass<?>Excludedthisclass; PrivateClass<?>Excludedthisclassfields; PublicActiveandroidstrategy (class<?> Excludedthisclass, class<?>excludedthisclassfields) { This. Excludedthisclass =Excludedthisclass; This. Excludedthisclassfields =Excludedthisclassfields; } @Override Public BooleanShouldskipfield (fieldattributes f) {returnF.getdeclaringclass (). Equals (Excludedthisclassfields); } @Override Public BooleanShouldskipclass (class<?>clazz) { if(Clazz = =NULL){ return false; } if(Clazz.equals (Excludedthisclass)) {return true; } returnShouldskipclass (Clazz.getsuperclass ()); }}
Build Gson
Gson New Gsonbuilder (). Setexclusionstrategies (new activeandroidstrategy (null, Model. class ) . Serializenulls (). Create (); log.d ("activeandroid", "Item ToJson:" +gson.tojson (obj));
Question Two
For activeandroid custom model type, or the collection of model as an attribute of an entity, without using Typeserializer, the value saved to the data is empty.
Example: The entity item contains a custom entity Category,childitem collection
@Table (name = "Items")publicclassextends Model{ = "name") Public String name; = "category") public category category; = "ChildItems") public list<childitem> childitems; }
Read the data in items from the database print:
The Category,childitems value is null, note that the value is null instead of an empty string, and the friend constructs the Gson object, calling Serializenulls ().
Solution: For the reference type of the field, save to the database we are accustomed to save as a JSON format string, read, the string into the corresponding reference type object, Typeserializer interface just do this work.
Public Abstract classSerializerextendstypeserializer{protectedGson Gson =NewGsonbuilder (). Setexclusionstrategies (NewActiveandroidstrategy (NULL, Model.class) . Create ();
The serialized class type @Override PublicClass<?>Getserializedtype () {returnString.class; }
Serialized As String @Override PublicObject Serialize (object data) {if(Data = =NULL){ return NULL; } returnGson.tojson (data); }}
Custom Entity Serializer Interface
public class Categoryserializer extends serializer {
The type after deserialization
Public class<?> Getdeserializedtype () {
return category.class;
}
Deserialize to Category Object
public object Deserialize (object data) {
if (data = = NULL) {
return null;
}
Return Gson.fromjson (String) data,category.class);
}
}
Entity Collection Serializer
public class Listserializer extends serializer{
@Override
Public class<?> Getdeserializedtype () {
return list.class;
}
@Override
Public List Deserialize (Object data) {
if (data = = NULL) {
return null;
}
Return Gson.fromjson (String) data,new typetoken<list<childitem>> () {
}.gettype ());
}
}
Add metadata in Adnroidminatest.xml Typeserializer
<meta-data android:name= "Aa_serializers" android:value= "Com.sihaixuan.practice.test.entity.CategorySerializer
, Com.sihaixuan.practice.test.entity.ListSerializer "/>
To read items from the database, the test results are as follows:
You can see the category, and the ChildItems are worth it.
Use a small complaint of AA:
1.AA in the largest number of categories are final, open interfaces are few, can be customized low, such as I want to dynamically add Typeserializer, it is not, violent points, can only be achieved through reflection.
2. If there are many entities, or if there are many types of entity collections, the Typeserializer class will not swell and there is no good way to avoid it.
Test DEMO:HTTP://PAN.BAIDU.COM/S/1DDEPZWT
Open Source Library activeandroid + Gson use