本文介绍了可以将用于Gson的POJO重新用于与Room一起使用的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Gson时,已创建POJO用于​​解析/序列化远程服务的json数据结果.可能会有一些Gson的注释

when using Gson it has POJO created for parsing/serializing the json data result from the remote service. It may have some Gson's annotation

public class User {
    @SerializedName("_id")
    @Expose
    public String id;
    @SerializedName("_name")
    @Expose
    public String name;
    @SerializedName("_lastName")
    @Expose
    public String lastName;

    @SerializedName("_age")
    @Expose
    public Integer age;
}

但是对于与Room一起使用的类,它可能具有自己的注释:

but for the class using with Room, it may have its own annotation:

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;

@Entity
public class User {
    public @PrimaryKey String id;
    public String name;
    public String lastName;
    public int age;
}

可以将这两个库与来自两个库的所有注释合并为一个(如果存在注释冲突(不希望),则必须使用长程序包名称来解决)?

could these two be combined into one with all of the annotation from two libs (if there is annotation clash (hope not), it would have to be resolved with long package names)?

推荐答案

是的,您可以为room和gson制作一个单独的pojo类.以及何时向服务器发送请求,以及如果未在服务器中发送任何数据,则该时间使用 transient 关键字,并且如果不想插入任何使用的字段表 @忽略.

yes you can make one single pojo class for room and gson.and when request to server and if any data not be send in server that time used transient keyword and if you want not insert any field table used @Ignore.

用于gson和room的以下代码.

used below code for gson and room..

 @Entity
public class User {

    @PrimaryKey(autoGenerate = true)
    @SerializedName("_id")
    @ColumnInfo(name = "user_id")
    public long userId;
    @SerializedName("_fName")
    @ColumnInfo(name = "first_name")
    private String name;

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这篇关于可以将用于Gson的POJO重新用于与Room一起使用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:44