question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

kotlin data class can not be decoded

See original GitHub issue
data class ShipDetail(
    var _id: String? = null,
    var order_id: String? = null)
val collection = database.getCollection<ShipDetail>("ship_detail")
var r = collection.findOne();

produce exception:

Exception in thread “main” org.bson.codecs.configuration.CodecConfigurationException: Can’t find a codec for class ShipDetail. at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37) at com.mongodb.FindIterableImpl.createQueryOperation(FindIterableImpl.java:228) at com.mongodb.FindIterableImpl.execute(FindIterableImpl.java:224) at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:205) at org.litote.kmongo.ExtensionsKt.findOne(extensions.kt:163) at org.litote.kmongo.ExtensionsKt.findOne$default(extensions.kt:162)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vsct-jburetcommented, Nov 28, 2017

@frzleaf Replace the line val client = MongoClient(MongoClientURI(“mongodb://root:secret$@localhost:27017/ship_info”)) with val client = KMongo.createClient(MongoClientURI(“mongodb://root:secret$@localhost:27017/ship_info”)) and it should work

0reactions
frzleafcommented, Nov 28, 2017

Thank you so much, it worked 😄

MongoClient object must be created by KMongo with codecRegistry = DEFAULT_CODEC_REGISTRY

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data classes
In Kotlin, these are called data classes and are marked with data : ... Data classes cannot be abstract, open, sealed, or inner....
Read more >
Kotlin data class copy method not deep copying all members
It seems like for some members, a (deep) copy is not actually created and the references are still to the original.
Read more >
MongoDB CodecConfigurationException for Kotlin data class
I am new to kotlin and mongo db. Cannot find a public constructor for 'TodoItem'. A custom Codec or PojoCodec may need to...
Read more >
Introduction to Data Classes in Kotlin
Although the properties of a data class can be mutable (declared using var ), It's strongly recommended to use immutable properties (declared ...
Read more >
Kotlin Data Class
In this article, you will learn to create data classes in Kotlin. You will also learn about requirements that data class must fulfill,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found