Skip to content
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

exception when use MutableCollection #27

Open
4o4E opened this issue Dec 8, 2023 · 0 comments
Open

exception when use MutableCollection #27

4o4E opened this issue Dec 8, 2023 · 0 comments

Comments

@4o4E
Copy link

4o4E commented Dec 8, 2023

use MutableCollection<User>

import com.github.jershell.kbson.KBson
import kotlinx.serialization.Serializable
import org.bson.BsonArray
import org.bson.BsonDocument
import org.bson.BsonElement
import org.bson.BsonString

@Serializable
data class Container(
    val users: MutableCollection<User>
)

@Serializable
data class User(val name: String)

fun main() {
    val bsonDocument = BsonDocument(
        mutableListOf(
            BsonElement("users", BsonArray(mutableListOf(BsonDocument("name", BsonString("a"))))),
        )
    )
    println(KBson.default.parse(Container.serializer(), bsonDocument))
}

exception

Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.
	at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:689)
	at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721)
	at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449)
	at com.github.jershell.kbson.FlexibleDecoder.beginStructure(BsonFlexibleDecoder.kt:51)
	at kotlinx.serialization.internal.AbstractPolymorphicSerializer.deserialize(AbstractPolymorphicSerializer.kt:121)
	at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:257)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
	at Container$$serializer.deserialize(a.kt:8)
	at Container$$serializer.deserialize(a.kt:8)
	at kotlinx.serialization.encoding.Decoder$DefaultImpls.decodeSerializableValue(Decoding.kt:257)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:16)
	at com.github.jershell.kbson.KBson.parse(KBson.kt:22)
	at AKt.main(a.kt:22)
	at AKt.main(a.kt)

if use List<User>, no exception

@Serializable
data class Container(
    val users: List<User>
)

@Serializable
data class User(val name: String)

fun main() {
    val bsonDocument = BsonDocument(
        mutableListOf(
            BsonElement("users", BsonArray(mutableListOf(BsonDocument("name", BsonString("a"))))),
        )
    )
    println(KBson.default.parse(Container.serializer(), bsonDocument))
}
@4o4E 4o4E changed the title org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY. exception when use MutableCollection Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant