0
votes

I am having an issue if I take JSONObject in room database entity.

@Entity(tableName = RoomDbConstant.formTable)
data class FormEntity(
    @NonNull
    @SerializedName("id")
    @PrimaryKey(autoGenerate = false)
    var formId: String,
    @NonNull
    @SerializedName("title")
    var formTitle: String?,
    @NonNull
    var formCategoryId: String?,
    @NonNull
    var formSubCategoryId: String?,
    @NonNull
    var formData: String?,
    @NonNull
    var is_active: String? = AppConstants.ACTIVE,
    @NonNull
    var status: String? = AppConstants.NONE,
    @NonNull
    var is_sent_server: String? = AppConstants.NOT_SENT,
    @NonNull
    var tempEntryExisted: Boolean? = false,
    @NonNull
    @Embedded(prefix="FormDataEntity")
    var formDataEntity: FormDataEntity?,
) : Comparable<FormEntity?> {
    override fun compareTo(other: FormEntity?): Int {
        return formTitle?.compareTo(other?.formTitle.toString(), ignoreCase = true)!!
    }
}

data class FormDataEntity(
    @Embedded
    @NonNull
    var response: FormFieldsEntity
)

data class FormFieldsEntity(
    @NonNull
    var is_active: String = AppConstants.ACTIVE,
    @NonNull
    @Embedded
    var fields: ArrayList<FieldEntity>,
    @NonNull
    @SerializedName("title")
    var formTitle: String,
    @NonNull
    @SerializedName("id")
    var formId: String,
    @NonNull
    @SerializedName("gfpdf_form_settings")
    var gfpdfFormSettings: JSONObject,
)

I have three classes First class is FormEntity which has sub-class FormDataEntity and that FormDataEntity class has a subclass that has JSONObject. SO I am getting data from api in which there is one JSONObject with the name "gfpdf_form_settings". enter image description here

So I am adding data in room databse when fetching data by converting using GSON from api.

val data: FormDataEntity = Gson().fromJson(obj, FormDataEntity::class.java)
                    val formModel = FormEntity(formId.toString(),Utilities.separateTitle(data.response.formTitle)[0].toString()
                        ,mCatId,mSubCatId.toString(),obj,data.response.is_active,
                        AppConstants.NONE,AppConstants.NOT_SENT,false,data)
But not able to do because I get error of roomdatabase.

Engineering Forms/Version 2 Room database offline functionality/EngineeringForm/app/build/tmp/kapt3/stubs/debug/com/app/engineeringform/controller/database/entities/formEntities/FormFieldsEntity.java:20: error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. private org.json.JSONObject gfpdfFormSettings; ^[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

I have tried every way but not able to find solution how to use JSONObject in entity class.

1

1 Answers

0
votes

You cannot save a JSONObject as entity in roomdb. In this case, you may copy the entire gfpdfForm JSON into JSON into Kotlin data class plugin to make unique data class for it.

You may reference here: https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-

There are many answer on converting JSON into data class

  1. insert JSON data using Room Library
  2. Android Room: Efficient way to transform json result into db object