0

I am newbie in kotlin and I try to copy a JSON structure to another one in an efficient way.

I have an API called getData() how send back a data structure defined as below:

data class DataA(
    var id: String,
    var cartItems: List<CartItem>, 
}

When the getData sent back the DataA structure, I have to map or translate it to another structure defined as below:

data class DataB(
    var cartItems: List<CartItem>, 
}

Is there an easy way to do it? I know that kotlin can easily encapsulate calls to make it nice.

Thanks

zsmb13
  • 85,752
  • 11
  • 221
  • 226
Seb
  • 2,929
  • 4
  • 30
  • 73

1 Answers1

0

Since you simply need to convert an instance of DataA to an instance of DataB, what you can do is DataB(dataA.cartItems), where dataA is the instance of DataA.

Note, however, that if for any reason you modify any item of cartItems from dataA, this change will be reflected also to dataB, since they share the same list object.

user2340612
  • 10,053
  • 4
  • 41
  • 66