kml*_*ckr 5 android realm gson
我正在使用带有gson的领域.我有一个模态,其中包含一个int类型字段列表.Realm不支持当前的基元列表.要解决这个问题,有一个解决方案.我创建了我的RealmInt类.
import io.realm.RealmObject;
public class RealmInt extends RealmObject {
private int val;
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个类似的大型模态对象..
public class Product extends RealmObject {
@PrimaryKey
private int productID;
private int priority;
private boolean isFavourite;
.....
.....
.....
private RealmList<Document> documents;
private RealmList<ProductInfoGroup> productInfoGroups;
private RealmList<RealmInt> categories;
Run Code Online (Sandbox Code Playgroud)
我必须将下面的json数组反序列化为Product modals.
[{
"productID": 776,
"categories": [
35
],
"name": "",
"priority": 3,
......
"status": 2,
"documents": [
{
"documentID": 74,
"productID": 776,
"name": null,
....
"isDefault": true
}
],
"productInfoGroups": [
{
"productInfoGroupID": 1575,
"productID": 776,
.....
"productInfos": [
{
"productInfoID": 2707,
"productInfoGroupID": 1575,
"title": "",
...
},
{
"productInfoID": 2708,
"productInfoGroupID": 1575,
...
},
{
"productInfoID": 2709,
.....
}
]
}
],
"lastUpdateDate": 130644319676570000,
"isActive": true
},....]
Run Code Online (Sandbox Code Playgroud)
这里有一个解决方案,但它不适用于大型物体.我只需要更改类别数组,其他反序列化必须通过默认的gson反序列化来完成.
您必须为每个与JSON表示不同的变量指定自定义类型适配器.自动处理所有其他对象.在您的情况下,它只是categories
变量,因为其余变量应自动映射.
JSON:
[
{ "name" : "Foo",
"ints" : [1, 2, 3]
},
{ "name" : "Bar",
"ints" : []
}
]
Run Code Online (Sandbox Code Playgroud)
型号类:
public class RealmInt extends RealmObject {
private int val;
public RealmInt() {
}
public RealmInt(int val) {
this.val = val;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
}
public class Product extends RealmObject {
private String name;
private RealmList<RealmInt> ints;
// Getters and setters
}
Run Code Online (Sandbox Code Playgroud)
GSON配置:
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.registerTypeAdapter(new TypeToken<RealmList<RealmInt>>() {}.getType(), new TypeAdapter<RealmList<RealmInt>>() {
@Override
public void write(JsonWriter out, RealmList<RealmInt> value) throws IOException {
// Ignore
}
@Override
public RealmList<RealmInt> read(JsonReader in) throws IOException {
RealmList<RealmInt> list = new RealmList<RealmInt>();
in.beginArray();
while (in.hasNext()) {
list.add(new RealmInt(in.nextInt()));
}
in.endArray();
return list;
}
})
.create();
JsonElement json = new JsonParser().parse(new InputStreamReader(stream));
List<Product> cities = gson.fromJson(json, new TypeToken<List<Product>>(){}.getType());
Run Code Online (Sandbox Code Playgroud)
如果你有多个包装变量,比如RealmInt,你需要为每个变量定义一个TypeAdapter.另请注意,上面的TypeAdapter总是会遇到一个数组.如果你可能发送JSON null
而不是[]
你需要在TypeAdapter中进行额外的检查.
归档时间: |
|
查看次数: |
3123 次 |
最近记录: |