to map a mongodb document to a json. we can use the following method
val json = document.toJson()
but this method maps the document as it is without changing the type of ObjectId or anything else.
for example when the mongodb document is
{
_id: ObjectId("xxx")
}
the output will be :
{"_id": {"$oid": "61d31135273a9076d6306993"}
to convert the document by changing the type ObjectId to String we can use JsonWriterSettings
val jsonWriterSettings : JsonWriterSettings = JsonWriterSettings.builder()
.objectIdConverter { value, writer ->
writer.writeString(value.toHexString())
}
.outputMode(RELAXED)
.build()
val json = document.toJson(jsonWriterSettings)
And the result will be :
{"_id": "61d31135273a9076d6306993"}
My question here, how to get instance of the default JsonWriterSettings used by Spring which parameterized by all other types like Date and my custom converters?