Search code examples
kotlinserializationamazon-dynamodbdeserializationmapper

Kotlin Map Serialization and Deserialization error class with map objects


I'm trying to create serialize and deserialize an object. I'm doing on this way:

   var mapper = ObjectMapper()

   var stringTest =   mapper.writeValueAsString(stringContainsObjectFilter);
    
   val raw: Any = mapper.readValue(stringTest, Any::class.java)
   val filtersFromString = mapper.convertValue(raw, ObjectFilter::class.java)

   val objectFilters = ObjectFilter()
   quoteFilters.rosterLine= filtersFromString.rosterLine
   quoteFilters.outOfPocketMax = filtersFromString.outOfPocketMax
   quoteFilters.cost = filtersFromString.cost

This is the class that I want to serialize and deserialize

open  class ObjectFilter{

    var rosterLine: Map<RosterEnum, List<Int>>? = null

    var outOfPocketMax: Map<String?, List<Int>>? = null

    var monthlyCost: List<MonthlyCost>? = null

}

But I'm getting the following message:

"Response[ObjectFilters]; could not unconvert attribute"

Can anyone help me please? I've been trying to solve it without result :(


Solution

  • I found the solution, this is what I did and it worked:

    val rawResult = mapper.convertValue(mapper.readValue(string, Any::class.java), Any::class.java)
            val filtersFromString = rawResult  as? ObjectFilter ?: ObjectFilter()