Jackson JSON Serialization without field name from one string row - json

I have this JSON to deserialize:
"data": {
"type": 18,
"msg": "firstName,lastName,15"
"timestamp": 1551770400000
}
I want to get this data in my model:
class DataDto(
type: String,
timestamp: Long,
msg: DataMsgDto?
) {
#JsonFormat(shape = JsonFormat.Shape.STRING)
#JsonPropertyOrder("firstName", "lastName", "age")
class DataMsgDto(
firstName: String,
lastName: String,
age: Long
)
}
I use this code to get data:
DataBuffer payload //this is what I get from server
val jsonNode = objectMapper.readTree(payload.toString(StandardCharsets.UTF_8))
objectMapper.treeToValue(jsonNode, DataDto::class.java)
But this does not work because in msg I don't have fields. So, how can I do this?

You have a JSON property that contains comma-separated values. The JSON message is valid, and can be deserialized by Jackson.
It's possible to write custom Jackson code that can handle this case automatically
How to deserialize a string separated by comma to list with Jackson commonly?
Jackson json property (string) to instance
But the most practical way to solve this is unlikely to be digging around trying to make Jackson understand msg, or writing a custom deserializer. Instead, allow msg to be a String
class DataDto(
val type: String,
val timestamp: Long,
// just use a normal String for msg
val msg: String?
)
And then manually decode it in Kotlin
fun parseDataDtoMsg(dto: DataDto): DataMsgDto? {
val msgElements = dto.msg?.split(",") ?: return null
// parse each message - you can chose whether to
// throw an exception if the `msg` does not match what you expect,
// or ignore invalid data and return `null`
val firstName = ...
val lastName = ...
val age = ...
return DataDtoMsg(
firstName = firstName,
lastName = lastName,
age = age,
)
}
data class DataMsgDto(
val firstName: String
val lastName: String
val age: Long,
)

Related

Deserializing a json object property to a String using kotlinx.serialization

Given json as follows where the structure of the payload object will vary:
{
"id": 1,
"displayName": "Success",
"payload": {
"someProperty": "example",
"someOtherProperty": {
"someNestedProperty": "example"
}
}
}
...using kotlinx.serialization how can I deserialize this into the following data class, where the value of payload should be the raw json string of the payload object.
#Serializable
data class Stub(
val id: Int,
val displayName: String,
val payload: String
)
Struggled to find a way of doing this with Serializers, but it was simple enough to implement manually using JsonElement.
val jsonObject = Json.parseToJsonElement(jsonString).jsonObject
val stub = Stub(
jsonObject["id"]!!.jsonPrimitive.int,
jsonObject["displayName"]!!.jsonPrimitive.content,
jsonObject["payload"]!!.toString()
)
There is a way to handle using JSONTransformingSerializer. It allows you to transform the json prior to deserialization. In this case from a jsonElement into a jsonPrimitive (of type String).
First create a transformer as follows:
object JsonAsStringSerializer: JsonTransformingSerializer<String>(tSerializer = String.serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement {
return JsonPrimitive(value = element.toString())
}
}
Now apply this transfer to the specific element in your data class by adding...
#Serializable(with = JsonAsStringSerializer::class)
just above the property you want to transform. Like this...
#Serializable
data class Stub(
val id: Int,
val displayName: String,
#Serializable(with = JsonAsStringSerializer::class)
val payload: String
)
The value of payload will be a string:
"{'someProperty': 'example','someOtherProperty': {'someNestedProperty':'example'}"
If you are later trying to deserialize this into different models depending on the structure, check out the JsonContentPolymorphicSerializer feature.

Can't parse json in Kotlin

I have a json like this:
{"ok":true,"result":[{"update_id":853803195,
"message":{"message_id":313,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594723984,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}},{"update_id":853803196,
"message":{"message_id":314,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594723986,"text":"e"}},{"update_id":853803197,
"message":{"message_id":325,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594734252,"text":"\ud83d\ude06"}},{"update_id":853803198,
"message":{"message_id":328,"from":{"id":104906563,"is_bot":false,"first_name":"AL","username":"alzvaracc","language_code":"en"},"chat":{"id":104906563,"first_name":"AL","username":"alzvaracc","type":"private"},"date":1594736358,"text":"5"}}]}
I'm using klaxon library.
Like in the first example I created a class:
class Response(val ok: Boolean, val result: String)
I was trying to save the second parameter to a string, so I could parse it, too, later. But I get a exception like this:
Unable to instantiate Response with parameters [ok: true, result: [, , , ]]
I tried making result a JsonObject (or a JsonArray of JsonObjects) and got this
Unable to instantiate JsonObject with parameters []
The only thing that more or less worked was a List<Any>. Result becomes a list of java objects like this:
[java.lang.Object#680362a, java.lang.Object#3569edd5, java.lang.Object#1f651cd8, java.lang.Object#7d0332e1]
But I don't know how to deal with them. So my question is what do I do? How do I get the result I will be able to work with?
Try this class
import com.beust.klaxon.*
private val klaxon = Klaxon()
data class User (
val ok: Boolean,
val result: List<Result>
) {
public fun toJson() = klaxon.toJsonString(this)
companion object {
public fun fromJson(json: String) = klaxon.parse<User>(json)
}
}
data class Result (
#Json(name = "update_id")
val updateID: Long,
val message: Message
)
data class Message (
#Json(name = "message_id")
val messageID: Long,
val from: From,
val chat: Chat,
val date: Long,
val text: String,
val entities: List<Entity>? = null
)
data class Chat (
val id: Long,
#Json(name = "first_name")
val firstName: String,
val username: String,
val type: String
)
data class Entity (
val offset: Long,
val length: Long,
val type: String
)
data class From (
val id: Long,
#Json(name = "is_bot")
val isBot: Boolean,
#Json(name = "first_name")
val firstName: String,
val username: String,
#Json(name = "language_code")
val languageCode: String
)

JSON decode nested field as Map[String, String] in Scala using circe

A circe noob here. I am trying to decode a JSON string to case class in Scala using circe. I want one of the nested fields in the input JSON to be decoded as a Map[String, String] instead of creating a separate case class for it.
Sample code:
import io.circe.parser
import io.circe.generic.semiauto.deriveDecoder
case class Event(
action: String,
key: String,
attributes: Map[String, String],
session: String,
ts: Long
)
case class Parsed(
events: Seq[Event]
)
Decoder[Map[String, String]]
val jsonStr = """{
"events": [{
"ts": 1593474773,
"key": "abc",
"action": "hello",
"session": "def",
"attributes": {
"north_lat": -32.34375,
"south_lat": -33.75,
"west_long": -73.125,
"east_long": -70.3125
}
}]
}""".stripMargin
implicit val eventDecoder = deriveDecoder[Event]
implicit val payloadDecoder = deriveDecoder[Parsed]
val decodeResult = parser.decode[Parsed](jsonStr)
val res = decodeResult match {
case Right(staff) => staff
case Left(error) => error
}
I am ending up with a decoding error on attributes field as follows:
DecodingFailure(String, List(DownField(north_lat), DownField(attributes), DownArray, DownField(events)))
I found an interesting link here on how to decode JSON string to a map here: Convert Json to a Map[String, String]
But I'm having little luck as to how to go about it.
If someone can point me in the right direction or help me out on this that will be awesome.
Let's parse the error :
DecodingFailure(String, List(DownField(geotile_north_lat), DownField(attributes), DownArray, DownField(events)))
It means we should look in "events" for an array named "attributes", and in this a field named "geotile_north_lat". This final error is that this field couldn't be read as a String. And indeed, in the payload you provide, this field is not a String, it's a Double.
So your problem has nothing to do with Map decoding. Just use a Map[String, Double] and it should work.
So you can do something like this:
final case class Attribute(
key: String,
value: String
)
object Attribute {
implicit val attributesDecoder: Decoder[List[Attribute]] =
Decoder.instance { cursor =>
cursor
.value
.asObject
.toRight(
left = DecodingFailure(
message = "The attributes field was not an object",
ops = cursor.history
)
).map { obj =>
obj.toList.map {
case (key, value) =>
Attribute(key, value.toString)
}
}
}
}
final case class Event(
action: String,
key: String,
attributes: List[Attribute],
session: String,
ts: Long
)
object Event {
implicit val eventDecoder: Decoder[Event] = deriveDecoder
}
Which you can use like this:
val result = for {
json <- parser.parse(jsonStr).left.map(_.toString)
obj <- json.asObject.toRight(left = "The input json was not an object")
eventsRaw <- obj("events").toRight(left = "The input json did not have the events field")
events <- eventsRaw.as[List[Event]].left.map(_.toString)
} yield events
// result: Either[String, List[Event]] = Right(
// List(Event("hello", "abc", List(Attribute("north_lat", "-32.34375"), Attribute("south_lat", "-33.75"), Attribute("west_long", "-73.125"), Attribute("east_long", "-70.3125")), "def", 1593474773L))
// )
You can customize the Attribute class and its Decoder, so their values are Doubles or Jsons.

Same SerializedName for two different data types sometimes, but with Kotlin

The JSON file I'm pulling from unfortunately has a node with the same variable name but could have two different data types randomly. When I make a network call (using gson) I get the error:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a BEGIN_ARRAY but was int at line 1 column 5344 path $[1].medium
the JSON looks like
{
"title": "Live JSON generator",
"url": google.com,
"medium": ["chicken", "radio", "room"]
}
//However sometimes medium can be:
"medium": 259
My Serialized class looks like:
data class SearchItem(
#SerializedName("title") var title: String,
#SerializedName("url") var urlStr: String,
#SerializedName("medium") val medium: List<String>? = null
) : Serializable {}
The way I'm making the network call is like this:
private val api: P1Api
fun onItemClicked(searchItem: SearchItem) {
api.getCollections { response, error ->
response.toString()
val searchItems: List<SearchItem> = Util.gson?.fromJson<List<SearchItem>>(
response.get("results").toString()
, object : TypeToken<List<SearchItem>>() {}.type)?.toList()!!
...
doStuffWithSearchItems(searchItems)
}
How do I handle both cases where "medium" can either be an array of strings or it could be an Int?
You could write custom JsonDeserializer for this case:
class SearchItemCustomDeserializer: JsonDeserializer<SearchItem> {
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): SearchItem {
val obj = json.asJsonObject
val title = obj.get("title").asString
val url = obj.get("url").asString
val mediumProp = obj.get("medium")
val medium = if(mediumProp.isJsonArray) {
mediumProp.asJsonArray.map { it.asString }
} else {
listOf(mediumProp.asString)
}
return SearchItem(
title = title,
urlStr = url,
medium = medium
)
}
}
With this class you "manually" deserialize json to object. For medium property we check is this array or simple json primitive with function mediumProp.isJsonArray. And if answer is yes - then deserialize field as json array of strings mediumProp.asJsonArray.map { it.asString } Else deserialize the field as string.
And then we register our custom SearchItemCustomDeserializer on GsonBuilder using method registerTypeAdapter
val gson = GsonBuilder()
.registerTypeAdapter(SearchItem::class.java, SearchItemCustomDeserializer())
.create()
And after this you can use this gson instance to deserialize yours objects

Jackson Serialize JSON array as String property

I am trying to ingest payloads from a remote web service using Jackson where the returned JSON contains properties that are defined as Arrays sometimes and plain Strings at other times.
I tried annotating the field using JsonRawValue but I am still getting the error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_ARRAY token
The following Kotlin classes are used to serialize the said JSON payload:
data class Device(
#get:JsonProperty("common.uuid")
val commonUuid: String? = null,
#field:JsonRawValue
#get:JsonProperty("user.ldap.groups.dn")
val userLdapGroupsDn: String? = null
)
data class DeviceSearchResults(
#JsonProperty("resultCount")
val resultCount: Int = 0,
#JsonProperty("totalCount")
val totalCount: Int = 0,
#JsonProperty("results")
val results: List<Device> = listOf()
)
.. and I put together the following unit test to show the error:
stubs-get-devices.json
{
"results": [
{
"common.uuid": "848ba0e8-d313-4df5-9a9e-3d9ea28951fa",
"user.ldap.groups.dn": [
"cn=qss-role-megamall-internet,ou=foo,ou=groups,dc=bar"
]
}
]
}
#Test
fun testJsonSerialization() {
val objectMapper = ObjectMapper().apply {
setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
registerModule(KotlinModule())
}
val file = File("stubs/get-devices.json")
val value = objectMapper.readValue(file, DeviceSearchResults::class.java)
assertThat(value).isNotNull
println(value)
}
Is there a way to coerce a JSON array value into a String property within the serialized Object?