0

When I find documents in my collections and parse them into POJOs, I would like to see exceptions, if additional keys are available in the MongoDB, that do not correspondent to my POJO. Can't find a way to configure that.

What I do

data class MyPojo(var a: Int)

val mongoClient = KMongo.createClient(...)
val collection = mongoClient...
val results = collection.aggregate<MyPojo>(...)

and if a result document is

{ "a": 1, "b": 2 }

What I get:

MyPojo(a=1)

I would like to see an exception of sort

kotlinx.serialization.json.JsonDecodingException: Invalid JSON...: Encountered an unknown key b

Does anyone know how to do that?

CFrei
  • 3,552
  • 1
  • 15
  • 29

1 Answers1

0

You have to specify strictMode = true in your JsonConfiguration for example:

install(ContentNegotiation) {
    serialization(
        contentType = ContentType.Application.Json,
        json = Json(
            JsonConfiguration(
                strictMode = true,
                prettyPrint = true
            )
        )
    )
}
Biscuit
  • 4,840
  • 4
  • 26
  • 54