-1

I have data class. I have inserting the room database this data class. it has string type. But I want to use back. I have calling the data. It has string. But I want to convert the data class type.

How can do this ?

Edit I used the Type Conveter for Room Database. if u have same problem. u should the this example. https://medium.com/@rasim0042/type-converter-for-room-db-2700e968a6d5

8
  • What exactly do you want to convert to what? Can you show an example of such a class and what you want to change about it? How does that relate to whatever you mean with "using back"? Commented Dec 19, 2023 at 6:59
  • data class Note( var title: String = "", var desc_text: String = "", var date: Date ?= null ) lets say. I have this model. I save to be string in room database. I want to get data in room database. But entity form is string. I want to use data model. DataModel(Application) -> String(Room) (Save) , Data Model(Application) <- String(Room) (Get Value) Commented Dec 19, 2023 at 8:06
  • Why don't you make the Note thing an entity or make an entity class similar to it? Why store it as a string? Commented Dec 19, 2023 at 8:17
  • it is already entity class. entity class input the data model. For Example. @Entitly(tablename = "...") data class saveNote( d: Int , value: Note). but its throw the error. it is "error: Cannot figure out how to save this field into database. You can consider adding a type converter for it." . I want to create type converter. Type converter save the data form string. Type converter get data from string and transform the data class module. but How can convert the string to data class. I dont know Commented Dec 19, 2023 at 8:52
  • So you want a converter between the date and strings? Commented Dec 19, 2023 at 9:49

1 Answer 1

1

If you want to use Gson with kotlin extension

Extension function

inline fun <reified T : Any> String.toDataClass(): T =
    Gson().fromJson(this, T::class.java)

inline fun <reified T : Any> T.toJsonString(): String =
    Gson().toJson(this)

Use to convert string and visaversa

// to get String from DataClass object
val jsonString: String = note.toJsonString()

// to get DataClass object from String
val note = jsonString.toDataClass<Note>()
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe its works but it is too long process. Cause of every entity has different converter type and I dont want to be manuel handle. I didnt want to convert model to string the to be manuel. Now I have create the type converter class. it is not generic. This class has from and to methods. Room Generate the class for converter. i think your solution support the "@ColumnInfo(name = "note") var note: String" this method. But Type Converter supports the "@ColumnInfo(name = "note") var note: Note" this method.
Yes, we can use @TypeConverter with room database but I thought you want to save data in string format. this @TypeConverter also save as string internally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.