0

I have that Json that I would like to map with Moshi and store with Room

{
    "name": "My Group",
    "members": [
        {
            "id": "119075",
            "invitedUser": {
                "id": 97375,
                "email": "xxx@gmail.com"
            },
            "inviting_user": {
                "id": 323915,
                "email": "yyy@gmail.com"
            }
        },
        {
            "id": "395387",
            "invitedUser": {
                "id": 323915,
                "email": "aaa@gmail.com"
            },
            "inviting_user": {
                "id": 323915,
                "email": "bbb",
            }
        }
    ]
}

I prepared my models

@Entity(tableName = "groups")
data class Group(
    @PrimaryKey
    val id: Long,
    val members: List<Member>
)

@Entity(tableName = "members")
data class Member(
    @PrimaryKey
    val id: Long,

    @Json(name = "invited_user")
    @ColumnInfo(name = "invited_user")
    val invitedUser: User,

    @Json(name = "inviting_user")
    @ColumnInfo(name = "inviting_user")
    val invitingUser: User
)

@Entity(tableName = "users")
data class User(
    @PrimaryKey
    val id: Int,

    val email: String
)

And currently, I have error: Cannot figure out how to save this field into database. I read this https://developer.android.com/training/data-storage/room/relationships. However, if I will model relationships like in documentation I don't know how to let Moshi map the relations? Have you found the simplest solution for that problem?

jakub
  • 3,576
  • 3
  • 29
  • 55

3 Answers3

2

You have 2 options in my opinion.

  1. You split the group and users in to individual tables and insert them separately.
  2. You use TypeConverters to store the members as a field of group.

Your implementation is going to be dependent on your use-case.

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
2

Finally, I stored it by using TypeConverters

private val membersType = Types.newParameterizedType(List::class.java, Member::class.java)
private val membersAdapter = moshi.adapter<List<Member>>(membersType)

@TypeConverter
fun stringToMembers(string: String): List<Member> {
    return membersAdapter.fromJson(string).orEmpty()
}

@TypeConverter
fun membersToString(members: List<Member>): String {
    return membersAdapter.toJson(members)
}

And that are my models

@TypeConverters(Converters::class)
@Entity(tableName = "groups")
data class Group(
    @PrimaryKey
    val id: Long,
    val name: String
) {
    companion object {

        data class Member(
            val id: Long,
            val invitedUser: User,
            val invitingUser: User
        )

        data class User(
            val id: Long,
            val email: String
        )
    }
}

Does it look good for you? Probably cleaner would be to have only ids and store somewhere else users, but I like that this solution is so simple.

jakub
  • 3,576
  • 3
  • 29
  • 55
0
  1. You use TypeConverters to store the members as a field of group.

I believe this is the Implementation you need.

open class UserRequestConverter {

    private val moshi = Moshi.Builder().build()

    @TypeConverter
    fun fromJson(string: String): User? {
        if (TextUtils.isEmpty(string))
            return null

        val jsonAdapter = moshi.adapter(User::class.java)
        return jsonAdapter.fromJson(string)
    }

    @TypeConverter
    fun toJson(user: User): String {
        val jsonAdapter = moshi.adapter(User::class.java)
        return jsonAdapter.toJson(user)
    }
}



@Entity(tableName = "members")
data class Member(
    @PrimaryKey
    val id: Long,

    @Json(name = "invited_user")
    @ColumnInfo(name = "invited_user")
    @TypeConverters(UserRequestConverter::class)
    val invitedUser: User,

    @Json(name = "inviting_user")
    @ColumnInfo(name = "inviting_user")
    @TypeConverters(UserRequestConverter::class)
    val invitingUser: User
)
Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61
  • To be fair, I'm not getting why would you like to do that mapping of User? Shouldn't I map `List` as I wrote below? – jakub Apr 04 '20 at 21:10