I have added a new issue on Youtrack <https://yout...
# announcements
n
I have added a new issue on Youtrack https://youtrack.jetbrains.com/issue/KT-25180 Here I want to add a toMap() function to Kotlin Any class which will return a hashmap of the variables of the class mapped to their instance value.
e
What’s the use case of this?
l
it is useless... IMHO
n
It’s kind of what we do with Gson.toJson(instance).
l
if you need this extension - just write it with reflection
you need jsonobject
or use
by map
n
Why would I need Jason object
Json
n
And why this will be bad idea to add a extension function toMap
e
What you do is basically transforming a object to a
HashMap
version of
JsonObject
n
But many classes are not constructed using Map
@edwardwongtl so is there currently a way to directly convert any class instance to JsonObject without using Gson or any json object
k
Note that you example class is really badly written, use
class Animal(private var name: String, private var type: Int)
instead.
n
@karelpeeters done.. thanks for pointing out
g
@navi There is no way without reflections and making private members accessible. So definitely not a method for stdlib. If you need this, just write a library
1