[JAVA-2695] Add support for Map<String, Object> in PojoCodec Created: 08/Dec/17  Updated: 16/Aug/18  Resolved: 30/Jan/18

Status: Closed
Project: Java Driver
Component/s: POJO
Affects Version/s: 3.5.0
Fix Version/s: 3.7.0

Type: Improvement Priority: Major - P3
Reporter: aneta stępień Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hi guys,

Our model consist of pojo classes containing Map<String,Object> properties field.
I try to user PojoCodec to save them in Mongo, however:

1. In PojoCodecImpl, method addToCache determines the codec using type parameters: "getCodecFromTypeData(typeData.getTypeParameters().get(1)));" and saving the pojo results in "CodecConfigurationException: Can't find a codec for class java.lang.Object".

2. I tried converting the Map<String,Object> field to org.bson.Document field before saving the Pojo in Mongo. However, when PojoBuilderHeper.getTypeData() method is called with org.bson.Document parameter, it checks that it is a Map, but there are no type parameters - and it throws an Exception (TypeData:114: typeParameters.size() != 2).

If I forget about Pojo and save new Document(map) or Document.parse(jsonStringRepresentationOfMap) - java mongo driver can recognize the types correctly (dates, numbers, etc.) and save them in database. But if this map is a field in a Pojo - I cannot save it.



 Comments   
Comment by Ross Lawley [ 16/Aug/18 ]

We have added JAVA-2923 to add support for this. In the meantime please see: JAVA-2920 which has a solution.

Comment by Gian Marco Gallo [ 16/Aug/18 ]

Hi

I'm using java mongodb-driver-sync version 3.8.0 but when I try to store a Map<String, List<String>> mongo returns the error:
"a custom codec or pojocodec may need to be explicity configured and registered to handle this type"

 

Any help?

Thanks

Comment by Githook User [ 30/Jan/18 ]

Author:

{'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley', 'username': 'rozza'}

Message: Added Pojo support for Map<String, Object>

JAVA-2695
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/e3c2b40cc170d3355b66eaf1c2bc3b9f078ffb50

Comment by aneta stępień [ 15/Dec/17 ]

Thank you! Using Document instead of Map works in version 3.6 - I'm able to continue work with this workaround.
Having a standard Map would be a cleaner solution, so I'm looking forward for this improvement.

Comment by Ross Lawley [ 08/Dec/17 ]

Currently, the PojoCodec expects every declared type to be supported by a Codec. I'll think on how to improve this further for your usecase.

In 3.6.0 we improved the support for Maps, so to fix this you could:

  • Add a Codec<Object> - this is risky due the chance of recursion.
  • Use Document instead of a Map<String, Object> as there will be a codec for that and it is a Map<String, Object>
  • Add a PropertyCodecProvider that handles Map<String, Object> - See the Generics support documentation

I hope that helps,

Ross

Generated at Thu Feb 08 08:57:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.