why
04/21/2023, 10:23 AM@Serializable
@SerialName("video")
data class Video(
val title: String,
val videoId: String,
...
) : Result
Javier
04/21/2023, 10:25 AM@JsonNames(...)
why
04/21/2023, 10:26 AMJavier
04/21/2023, 10:26 AM@SerialName
over classes, only over propertieswhy
04/21/2023, 10:29 AMJavier
04/21/2023, 10:59 AMwhy
04/21/2023, 11:26 AMJavier
04/21/2023, 11:29 AMwhy
04/21/2023, 11:31 AMJavier
04/21/2023, 11:37 AMwhy
04/21/2023, 11:40 AMJavier
04/21/2023, 11:41 AMwhy
04/21/2023, 11:43 AMJavier
04/21/2023, 11:56 AMdata class Response(@JsonNames("foo", "bar") val fooBar: FooBar)
sealed class FooBar {
data class Foo(val foo: Foo) : FooBar()
data class Bar(val bar: Bar) : FooBar()
}
data class Response(@SerialName("foo") val foo: FooBar, @SerialName "bar") val bar: FooBar)
// or even
data class Response(@SerialName("foo") val foo: Foo, @SerialName("bar") val bar: Bar)
sealed class FooBar {
data class Foo(val foo: Foo) : FooBar()
data class Bar(val bar: Bar) : FooBar()
}
So I can be missing somethingwhy
04/21/2023, 12:08 PM/*
[
{
type: "type1",
title: String,
...
},
{
type: "type2",
title: String,
...
},
{
type: "type3",
title: String,
...
}
]
*/
Javier
04/21/2023, 12:12 PMsealed class Foo {
data class(val type: Type1, val title: String, ...) : Foo()
data class(val type: Type2, val title: String, ...) : Foo()
data class(val type: Type3, val title: String, ...) : Foo()
}
But probably you can extract common fields:
sealed class Foo<T> {
abstract val type: T
abstract val title: String
...
}
type
is changing the name, then:
sealed class Foo {
data class(val type1: Type1, val title: String, ...) : Foo()
data class(val type2: Type2, val title: String, ...) : Foo()
data class(val type3: Type3, val title: String, ...) : Foo()
}
why
04/21/2023, 12:14 PMJavier
04/21/2023, 12:16 PMwhy
04/21/2023, 12:16 PMJavier
04/21/2023, 12:17 PMwhy
04/21/2023, 12:17 PMJavier
04/21/2023, 12:19 PMval list: List<Foo> // backend response
data class Foo(
@JsonNames("type1", "type2", ...) val type: Type,
val title: String,
...
)
sealed class Type {
data class Type1(...) : Type()
data class Type1(...) : Type()
...
}
why
04/21/2023, 12:23 PMAdam S
04/24/2023, 12:08 PMJavier
04/24/2023, 2:59 PM@SerialName
is used on fields, over classes I haven't triedAdam S
04/24/2023, 3:01 PM@SerialName
is used for JSON properties, and also to determine the type
field for polymorphic objects.why
05/03/2023, 3:46 PMAdam S
05/03/2023, 3:52 PM@Serializable
@SerialName("video")
data class Video(
val title: String,
val videoId: String,
) : Result
if I use this to serialize some data
fun main() {
val video = Video(title = "Star Wars", videoId = "001011101")
println(Json.encodeToString(video))
}
I’d end up with
{
"title": "Star Wars",
"videoId": "001011101"
}
and there’s no reference to @SerialName("video")
, because JSON objects don’t have names.
Where should the @SerialName("video")
be used? Where should it go in the JSON?why
05/03/2023, 3:54 PM/*
[
{
type: "type1",
title: String,
...
},
{
type: "type2",
title: String,
...
},
{
type: "type3",
title: String,
...
}
]
*/
Adam S
05/03/2023, 4:04 PM