0

My problem is that I need to transform a data class in kotlin to a map, because I need to work with this structure as a requirement, because this response will be used for a groovy classes and there is a post-process where there are validations iterations etc, with this map. My data class is the next (Podcast):

data class PodCast(val id: String, val type: String, val items: List<Item>, val header: Header, val cellType:String? = "")

data class Item(val type: String, val parentId: String, val parentType: String, val id: String, val action: Action, val isNew: Boolean)

data class Header(val color: String, val label: String)

data class Action(val type: String, val url: String)

I made the transformation manually, but I need a more sophisticated way to achieve this task.

Thanks.

rasilvap
  • 1,771
  • 3
  • 31
  • 70
  • What should the `Map` look like? You made the transformation manually...where? What sort of approach qualifies as "more sophisticated"? – Slaw Apr 04 '19 at 14:37
  • Which class do you need to transform into a `Map`? – Louis Apr 04 '19 at 14:37
  • The map should be a key value map, more sophisticated I mean a generic way to achieve this. – rasilvap Apr 04 '19 at 14:39
  • "_a key value map_". That's what a map _is_. What should be the keys and what should be the values? What have you already tried and why isn't it generic enough? – Slaw Apr 04 '19 at 14:40
  • I need to transform this in a map because I have to work with this map since groovy and there is a post-process with this map, itereations, ask for files etc. – rasilvap Apr 04 '19 at 14:41
  • Possible duplicate of [Java introspection: object to map](https://stackoverflow.com/questions/6796187/java-introspection-object-to-map) – Salem Apr 04 '19 at 15:44
  • Possible duplicate of [How to convert a Kotlin data class object to map?](https://stackoverflow.com/questions/49860916/how-to-convert-a-kotlin-data-class-object-to-map) – Can_of_awe Apr 04 '19 at 15:58
  • The final one (https://stackoverflow.com/questions/49860916/how-to-convert-a-kotlin-data-class-object-to-map) is the opposite way. I need to transforma an object to a map. And the other one is the java way (https://stackoverflow.com/questions/6796187/java-introspection-object-to-map), I am looking for a more kolin way. – rasilvap Apr 04 '19 at 16:10

2 Answers2

3

You can also do this with Gson, by serializing the data class to json, and then deserializing the json to a map. Conversion in both directions shown here:

val gson = Gson()

//convert a data class to a map
fun <T> T.serializeToMap(): Map<String, Any> {
    return convert()
}

//convert a map to a data class
inline fun <reified T> Map<String, Any>.toDataClass(): T {
    return convert()
}

//convert an object of type I to type O
inline fun <I, reified O> I.convert(): O {
    val json = gson.toJson(this)
    return gson.fromJson(json, object : TypeToken<O>() {}.type)
}

See similar question here

Tom Hanley
  • 1,252
  • 11
  • 21
1

I have done this very simple. I got the properties of the object, just using the .properties groovy method, which gave me the object as a map.

rasilvap
  • 1,771
  • 3
  • 31
  • 70