handling empty values in collections
To reproduce please run the following code:
public class JaxBsonCollectionNulls {
public static void main(String[] args) {
TestModel testModel = new TestModel();
testModel.integerSet = new HashSet<>(Arrays.asList(1, 2, 3, null));
testModel.stringSet = new HashSet<>(Arrays.asList("1", "2", "3", null));
testModel.integerList = Arrays.asList(1, 2, 3, null);
final JaxBsonContext jaxBsonContext = JaxBsonContext.newInstance(TestModel.class);
Document document = jaxBsonContext.toBson(testModel);
System.out.println("document: " + document);
}
private static class TestModel {
private ObjectId _id;
private Set<Integer> integerSet;
private Set<String> stringSet;
private List<Integer> integerList;
}
}
To solve the problem in org/qnixyz/jbson/JaxBsonToBson.java we should add if statement for null values, ie:
private NumberBasedCollectionToBson numberBasedCollectionToBson = (JaxBsonFieldContext fieldCtx, Collection<Object> src) -> {
final List<Object> ret = new ArrayList<>();
src.forEach(e -> {
NumberBased.BsonNumber bsonNumber =
fieldCtx.getConfiguration().getNumberBased().getToBson().convert(fieldCtx, e);
ret.add(bsonNumber == null ? null : bsonNumber.getValue());
});
return ret;
};
Edited by Paweł Wys