-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
handle default value of operation #7
Comments
Kotlin/kotlinx.serialization#276 JsonObject could be the key. |
tested with iosX64 enum class Type {
FOO, BAR
}
@Serializable
data class Name(val value: String)
@Serializable
data class User(val id: String, val age: Int, val name: Name, val type: Type)
fun test() {
val json = json {
"key" to "string"
"key2" to 0
"user" to Json.plain.toJson(User.serializer(), User("idstring", 32, Name("namestring"), Type.BAR))
}
val stringified = Json.stringify(JsonObjectSerializer, json)
println("map: $stringified")
// map: {"key":"string","key2":0,"user":{"id":"idstring","age":32,"name":{"value":"namestring"},"type":"BAR"}}
} root element can be JsonObject. This way we can ignore value for arguments not provided |
draft sealed class KgqlValue<out T : Any?> {
data class Some<out T : Any?>(val value: T) : KgqlValue<T>()
object None : KgqlValue<Nothing>()
}
class Variables(val login: String) {
private var name: KgqlValue<Int?> = KgqlValue.None
private var id: KgqlValue<Int?> = KgqlValue.None
private var company: KgqlValue<String?> = KgqlValue.None
private var foo: KgqlValue<Float?> = KgqlValue.None
private var logins: KgqlValue<List<String?>?> = KgqlValue.None
fun name(name: Int?): Variables2 {
this.name = KgqlValue.Some(name)
return this
}
fun id(id: Int?): Variables2 {
this.id = KgqlValue.Some(id)
return this
}
fun company(company: String?): Variables2 {
this.company = KgqlValue.Some(company)
return this
}
fun foo(foo: Float?): Variables2 {
this.foo = KgqlValue.Some(foo)
return this
}
fun logins(logins: List<String?>?): Variables2 {
this.logins = KgqlValue.Some(logins)
return this
}
fun asJsonObject(): JsonObject {
return json {
"login" to login
(name as? KgqlValue.Some)?.let { "name" to it.value }
(id as? KgqlValue.Some)?.let { "id" to it.value }
(company as? KgqlValue.Some)?.let { "company" to it.value }
(foo as? KgqlValue.Some)?.let { "foo" to it.value }
(logins as? KgqlValue.Some)?.let {
"logins" to Json.plain.toJson(
NullableSerializer(NullableSerializer(String.serializer()).list),
it.value
)
}
}
}
}
|
in order to serialize enum value properly, we need to know if a target value is enum or not. But to do this, we need schema. Without schema we can not distinguish enum from other custom type we first need to resolve #1 |
another workaround is to let users define serializer function for enums like below
|
No description provided.
The text was updated successfully, but these errors were encountered: