I am facing an issue where the discriminator is a ...
# serialization
g
I am facing an issue where the discriminator is a name/value pair in the json file and my data classes fail on the serilization. So I receive the following json file of a user:
Copy code
{
	"header" : {...},
	"sources:": [
		{
			"discriminator" : "facebook",
			"facebook" : {
				"common" : {
	         		"lastUsedOn" : "2023-01-01T00:00:00Z"
	         		"device" : "computer"
				},
				"friends" : [...]
			}
		},
		{
			"discriminator" : "facebook",
			"facebook" : {
				"common" : {
	         		"lastUsedOn" : "2022-01-01T00:00:00Z"
	         		"device" : "mobile"
				},
				"friends" : [...]
			}
		},
		{
			"discriminator" : "instagram",
			"instagram" : {
				"common" : {
	         		"lastUsedOn" : "2000-01-01T00:00:00Z"
	         		"device" : "tablet"
				},
				"followers" : [...]
			}
		},
		{
			"discriminator" : "twitter",
			"twitter" : {
				"common" : {
	         		"lastUsedOn" : "2000-01-01T00:00:00Z"
	         		"device" : "mobile"
				},
				"tweets" : [...]
			}
		}
	]
}
and my data classes are the following:
Copy code
Serializable
data class User(
    @SerialName("header")
    private val header: Header,

    @SerialName("sources")
    private val Sources: List<Source>,
)


@Serializable
sealed class Source {
    @SerialName("common")
    abstract val common: Common

    @Serializable
    data class Common(
    	@SerialName("lastUsedOn")
        var lastUsedOn: Instant,
        @SerialName("device")
        var device: String
    )
}


@Serializable
@SerialName("facebook")
data class Facebook(
	override val common: Common,
	@SerialName("friends")
	val friends: ...
) : Source()


@Serializable
@SerialName("instagram")
data class Instagram(
	override val common: Common,
	@SerialName("followers")
	val followers: List<...>
) : Source()


@Serializable
@SerialName("twitter")
data class Twitter(
	override val common: Common,
	@SerialName("tweets")
	val tweets: List<...>
) : Source()
Obviously this fails because of the
discriminator
. Is there an elegant way where I can serialize/deserialize given these data classes? Or must I follow another different approach?
c
Are you using the right Json instance for (de)serializing?
Copy code
val json = Json {
  classDiscriminator = "discriminator"
}

val user: User = json.decodeFromString(myJsonString)
g
yes I do but it still fails
Copy code
Polymorphic serializer was not found for missing class discriminator ('null')
I assume that happens because my
Source
does not contain the
discriminator
val or am I mistaken?
edit: it fails because the discriminator in the json file lives outside of the
Source
object
and this is what I try to deal with
e
easier to have an intermediate
Copy code
@Serializable
data class SourceSurrogate(
    val discriminator: String,
    val facebook: Facebook? = null,
    val instagram: Instagram? = null,
    val twitter: Twitter? = null,
)
which you then either map manually or use https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#composite-serializer-via-surrogate on
g
thank you @ephemient, that worked! 🤝