[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. 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: |
| 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:
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>
|
| 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. |
| 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:
I hope that helps, Ross |