Using Morphia and mapped models to convert BSON to POJO #2695
-
hi folks, I'm working on some integration testing I'm aiming to keep constant through from v1 to v2 upgrades. For this we defined some test data in However, I can see from this issue and discussion that it's not included functionality anymore. I'm wondering if there's anything possible through a v2.3+ upgrade as these methods have been removed from public APIs. I see the workaround in that discussion is to use some internal classes that use the CodecRegistry. However, we don't have models defined in our codec registry, just specific types. We only map our models with |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
OK. So this is going to deep in to the weeds of internal stuff that should get you where you're going but I wouldn't recommend relying on it because internals can always change without warning yadda yadda yadda. But enough with the safe harbor disclaimers... So to start off with, you're going to have a file with BSON bytes, yeah? First off, you'll want to read that in to memory. Read each bson in to a var bsonDocument = new BsonDocumentCodec()
.decode(new BsonBinaryReader(ByteBuffer.wrap(bytes)
.order(ByteOrder.LITTLE_ENDIAN)), DecoderContext.builder().build()); Once that's done, you'll have a var entity = datastore.getCodecRegistry()
.get(<your entity type here>)
.decode(new DocumentReader(document), decoderContext); That should get you to the entity you need. As you can see it's quite convoluted and relies on a number of internal details from both morphia and the driver. I would suggest a more resilient route. If you have these documents in a database already, you can export them via Anyway, I hope that gets you (mostly?) over the hump here. If you have any questions, please don't hesitate to ask and we can see what else needs to happen to get you where you want to be. |
Beta Was this translation helpful? Give feedback.
-
hi @evanchooly, thank you for the response! I think I may be missing something here. We have Morphia setup in the following manner, using v2.3:
Unfortunately I'm seeing that calling getCodecRegistry for our models is throwing a
However, these datastores work as expected for type conversion when performing actual database ops. I can see from doing runtime introspection into the Codec registry there isn't a specified codec for these types. From my understanding of the library |
Beta Was this translation helpful? Give feedback.
-
hi @evanchooly , thanks for your response. It turns out this actually was the solution. The issue was at runtime it was failing to fetch a codec for child models (that needed |
Beta Was this translation helpful? Give feedback.
OK. So this is going to deep in to the weeds of internal stuff that should get you where you're going but I wouldn't recommend relying on it because internals can always change without warning yadda yadda yadda. But enough with the safe harbor disclaimers...
So to start off with, you're going to have a file with BSON bytes, yeah? First off, you'll want to read that in to memory. Read each bson in to a
byte[]
and then you can read that in to aBsonDocument
:Once that's done, you'll have a
BsonDocument
and you can create aD…