`{\"customerId\":\"123\",\"id\":\"registration\",\...
# getting-started
a
{\"customerId\":\"123\",\"id\":\"registration\",\"value\":0,\"balance\":0,\"expirationDate\":1675122800125}
Please how to convert the above string to this Promotion object
Copy code
sealed class Promotion(customerId: String, id: String, value: BigDecimal, balance: BigDecimal, expirationDate: Timestamp)
s
Looks like a JSON string, so you could use something like #serialization or #jackson-kotlin to deserialize it to an object.
a
can i use fasterxml?
s
The #jackson-kotlin module is an extension for the FasterXML Jackson library to enable it to work better with Kotlin classes. Yes, you could use it for this.
a
okay thank you, let me try
Do you is the method?
Copy code
val promotions = jacksonObjectMapper().readValue(promotion, Promotion::class.java)
s
👍 that should work. You might need to make your class a
data
class instead of a
sealed
class, though.
a
and also
@Serializable
right?
s
You don’t need that annotation if you’re using Jackson. That would only be needed if you choose to use Kotlin #serialization instead.
a
ohk, thank you Sir
🐕 1
Please what do you think if the string is in this format
{{\"customerId\":\"123\",\"id\":\"registration\",\"value\":0,\"balance\":0,\"expirationDate\":1675086187278}, {\"customerId\":\"124\",\"id\":\"registration\",\"value\":0,\"balance\":0,\"expirationDate\":1675086187278}}
s
That looks a bit odd. It seems like a list containing two objects, but a JSON list is supposed to be in square brackets
[]
, not braces
{}
. If you had a valid JSON list, you could do
jacksonObjectMapper().readValue<List<Promotion>>(promotions)
. If your list really does have braces instead of square brackets, it’s not valid JSON and you would need to fix it before passing it to a JSON deserializer.
a
ohk, thank you Sir