I create data class X (...) then I use X.toString ...
# getting-started
u
I create data class X (...) then I use X.toString and now I want to convert that string back to data class X object what shall I do? Is there a Kotlin way to done this?
j
toString()
is not meant for serialization, it is meant to help for printing/debugging/logging etc. If you want to serialize/deserialize, you should probably use more suited formats
u
I see thank you
e
in general the
toString()
cannot be inverted. for example,
Copy code
data class X(val a: String, val b: String)
val x1 = X(a = "a, b=b", b = "b")
val x2 = X(a = "a", b = "b, b=b")
x1 != x2
x1.toString() == x2.toString()
u
I found Kotlinx.Serialization is that a key for my problem?
👌 2
r
yes, and if you want serialize to a string, json is probably the format to go with
âž• 3