0

In my application, I am getting a JSON response from an API which I need to parse it. The JSON looks like this:

{
"itemTemplates":
[
  {
    "type": "...",
    "properties": {
      "..": ".."
    },
    "children": [
      {
        "type": "..",
        "properties": {
          "..": ".."
        },
        "children": [
          {
            "type": ".."
          }
        ]
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      },
      {
        "type": "..",
        "properties": {
          "value": ".."
        }
      }
    ]
  }
]

}

I have created Kotlin data classes for the above response which looks like this:

data class ItemTemplate(val itemTemplates: List<Component> = listOf())


data class Component(
val children: List<Children> = listOf(),
val Properties: Properties = Properties(),
val type: String = "")


data class Children(
val properties: Properties = Properties(),
val type: String = "",
val children: List<Children> = listOf())

As you can see there is a recursive data class in the Children data class. In the JSON response, each Child can have many or zero Children itself. The problem occurs when it comes to using the data classes. i.e. I cannot loop over Children because it may also have Children which I need to iterate over that as well (and so on).

Since it is not known which Child has Children and how many times I should iterate in order to parse everything, I started thinking of maybe using a recursive function. However, not sure if it is correct to do so and how to do it.

Any suggestions? Thanks

Mehdi Satei
  • 1,225
  • 9
  • 26
  • do Children's children look exactly the same as Children ? if yes, it's simple - use GSON. if no - it's simple, write up a ChildrenChild class, and use GSON. – Shark Dec 10 '19 at 12:09
  • Thanks, @Shark. My question is mainly about traversing. How to recursively traverse those nested objects. – Mehdi Satei Dec 10 '19 at 16:07

2 Answers2

0

Based on the code provided, let's assume that this is the structure of the Properties model.

data class Properties(val test: String)

data class Component(
        val children: List<Children> = listOf(),
        val properties: Properties = Properties("test"),
        val type: String = ""
)

data class Children(
        val properties: Properties = Properties("test"),
        val type: String = "",
        val children: List<Children> = listOf()
)

Since it is not known which Child has Children and how many times I should iterate in order to parse everything, I started thinking of maybe using a recursive function. However, not sure if it is correct to do so and how to do it.

You don't necessarily need to use recursion since the structure of your JSON payload is predictable. Assuming that you will only have one subset of children list.

Sometimes...

{
    "property": {
        "children": []    
    }
}

Sometimes...

{
    "property": {
        "children": [
                {
                    "property": {
                    "children": []
                }
            }
        ]
    }
}

Option 1. You can create a different object for the sub children, if deemed necessary (assuming they have different set of parameters).

data class Children(
        val properties: Properties = Properties("test"),
        val type: String = "",
        val children: List<SubChildren>? = listOf()
)

Option 2. Use GSON library. It's one of the elegant ways on how you can serialize your data in Java and Kotlin. It will handle cases where your children or other parameters may or may not be present during serialization.

Further reading:

Joshua de Guzman
  • 2,063
  • 9
  • 24
0

"my problem is actually with how to recursively traverse all the children. Do you have any suggestion?"

how about something like:

fun getChildrenTypes() : String =
    if (children.isEmpty()) ""
    else (type.plus(children.map { it.getChildrenTypes() }.reduce { concatString : String, element -> concatString.plus(element) }))