infinum/kotlin-jsonapix

'meta' is null when trying to convertFromString using generated TypeAdapter

Closed this issue · 2 comments

This is my json:

{
  "data": {
    "type": "st",
    "id": "1",
    "attributes": {
      "end_date": "2222-12-31",
      "payment_id": "1",
      "start_date": "2020-12-13",
      "status": "ACTIVE"
    },
    "meta": {
      "detail": "user details"
    }
  }
}

This is my model:

@Serializable
@JsonApiX(type = "st")
data class StModel(
    @SerialName("end_date") val subscriptionEndDate: String? = null,
    @SerialName("start_date") val subscriptionStartDate: String? = null,
    @SerialName("status") val subscriptionStatus: String? = null,
    @SerialName("payment_id") val paymentId: String? = null,
) : JsonApiModel()

@Serializable
@JsonApiXMeta(type = "st")
data class StMeta(val detail: String) : Meta

and I am using below statement to get StModel from jsonString:
TypeAdapter_StModel().convertFromString(jsonString)

and I get null meta.

Hello @HussainChachuliya0306,

I've noticed that in your JSON structure, the meta is nested inside the data block. According to JsonApi standards, this positions it as a resource object meta rather than a root meta. In JsonApiX, the default setting targets the root meta. To adjust for your structure, you'll need to set the metaPlacementStrategy in the JsonApiXMeta annotation to DATA. Here's an example:

@Serializable
@JsonApiXMeta(type = "st", placementStrategy = MetaPlacementStrategy.DATA)
data class StMeta(val detail: String) : Meta

With this setup, you can then access this meta using

model.resourceObjectMeta

Additionally, I'd recommend reconsidering the naming convention of your data classes. Avoid using XModel as a suffix. The library generates additional classes with a Model suffix, which could result in a class named StModelModel in your case. This could be confusing and potentially impact the readability and clarity of your code.

Hello @HussainChachuliya0306,

I've noticed that in your JSON structure, the meta is nested inside the data block. According to JsonApi standards, this positions it as a resource object meta rather than a root meta. In JsonApiX, the default setting targets the root meta. To adjust for your structure, you'll need to set the metaPlacementStrategy in the JsonApiXMeta annotation to DATA. Here's an example:

@Serializable
@JsonApiXMeta(type = "st", placementStrategy = MetaPlacementStrategy.DATA)
data class StMeta(val detail: String) : Meta

With this setup, you can then access this meta using

model.resourceObjectMeta

Additionally, I'd recommend reconsidering the naming convention of your data classes. Avoid using XModel as a suffix. The library generates additional classes with a Model suffix, which could result in a class named StModelModel in your case. This could be confusing and potentially impact the readability and clarity of your code.

Thanks for the placementStrategy tip, @thisAAY.