Daniel Pitts
10/20/2024, 7:16 PMclass Example(
name: String,
age: Int? = null,
) : JsonObjectBuilder() {
var name by required(name)
var age by optional<Int>(age, includeNulls = true)
val favoriteNumbers by optionalList<Int>()
}
fun main() {
val value = Example("John")
println(encodeToString(value))
// {"name":"John"}
value.age = 42
value.favoriteNumbers.add(1)
value.favoriteNumbers.add(2)
println(encodeToString(value))
// {"name":"John","age":42,"favoriteNumbers":[1,2]}
}
(This is utilizing kotlinx serialization under the hood, but I don't like how its interacting with it yet)Daniel Pitts
10/20/2024, 7:19 PMJoffrey
10/20/2024, 7:29 PMJoffrey
10/20/2024, 7:29 PMDaniel Pitts
10/20/2024, 7:30 PMFergus Hewson
10/20/2024, 7:30 PM@Serializable
class Example(
name: String,
age: Int? = null,
)
Daniel Pitts
10/20/2024, 7:31 PMvalue.age = null
causing
{"name":"John,"age": null}
Fergus Hewson
10/20/2024, 7:31 PMDaniel Pitts
10/20/2024, 7:32 PMFergus Hewson
10/20/2024, 7:35 PM@EncodeDefault(EncodeDefault.Mode.ALWAYS) age: Int? = null
Daniel Pitts
10/20/2024, 7:35 PMDaniel Pitts
10/20/2024, 7:40 PMFergus Hewson
10/20/2024, 7:46 PMDaniel Pitts
10/20/2024, 9:57 PMArjan van Wieringen
10/21/2024, 6:04 AM