https://kotlinlang.org logo
#getting-started
Title
# getting-started
u

umitems

01/17/2022, 12:43 AM
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

Joffrey

01/17/2022, 12:45 AM
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

umitems

01/17/2022, 12:47 AM
I see thank you
e

ephemient

01/17/2022, 1:08 AM
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

umitems

01/17/2022, 4:39 AM
I found Kotlinx.Serialization is that a key for my problem?
👌 2
r

Rein T

01/17/2022, 7:57 AM
yes, and if you want serialize to a string, json is probably the format to go with
3