|
26 | 26 | import java.util.HashSet;
|
27 | 27 | import java.util.Iterator;
|
28 | 28 | import java.util.List;
|
| 29 | +import java.util.Locale; |
29 | 30 | import java.util.Map;
|
30 | 31 | import java.util.Set;
|
31 | 32 |
|
32 | 33 | import org.bson.types.ObjectId;
|
33 | 34 | import org.springframework.beans.factory.InitializingBean;
|
34 | 35 | import org.springframework.core.GenericTypeResolver;
|
35 | 36 | import org.springframework.core.convert.ConversionService;
|
| 37 | +import org.springframework.core.convert.TypeDescriptor; |
36 | 38 | import org.springframework.core.convert.converter.Converter;
|
37 | 39 | import org.springframework.core.convert.converter.ConverterFactory;
|
| 40 | +import org.springframework.core.convert.converter.GenericConverter; |
38 | 41 | import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
|
39 | 42 | import org.springframework.core.convert.support.ConversionServiceFactory;
|
40 | 43 | import org.springframework.core.convert.support.GenericConversionService;
|
@@ -64,6 +67,7 @@ public AbstractMongoConverter(GenericConversionService conversionService) {
|
64 | 67 | this.conversionService = conversionService == null ? ConversionServiceFactory.createDefaultConversionService()
|
65 | 68 | : conversionService;
|
66 | 69 | this.conversionService.removeConvertible(Object.class, String.class);
|
| 70 | + registerConverter(CustomToStringConverter.INSTANCE); |
67 | 71 | }
|
68 | 72 |
|
69 | 73 | /**
|
@@ -107,19 +111,33 @@ private void initializeConverters() {
|
107 | 111 | * @param converter
|
108 | 112 | */
|
109 | 113 | private void registerConverter(Object converter) {
|
110 |
| - Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class); |
111 |
| - if (MONGO_TYPES.contains(arguments[1]) || MONGO_TYPES.contains(arguments[0])) { |
112 |
| - customTypeMapping.add(new ConvertiblePair(arguments[0], arguments[1])); |
| 114 | + |
| 115 | + if (converter instanceof GenericConverter) { |
| 116 | + customTypeMapping.addAll(((GenericConverter) converter).getConvertibleTypes()); |
| 117 | + } else { |
| 118 | + Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class); |
| 119 | + if (MONGO_TYPES.contains(arguments[1]) || MONGO_TYPES.contains(arguments[0])) { |
| 120 | + customTypeMapping.add(new ConvertiblePair(arguments[0], arguments[1])); |
| 121 | + } |
113 | 122 | }
|
| 123 | + |
114 | 124 | boolean added = false;
|
| 125 | + |
115 | 126 | if (converter instanceof Converter) {
|
116 | 127 | this.conversionService.addConverter((Converter<?, ?>) converter);
|
117 | 128 | added = true;
|
118 | 129 | }
|
| 130 | + |
119 | 131 | if (converter instanceof ConverterFactory) {
|
120 | 132 | this.conversionService.addConverterFactory((ConverterFactory<?, ?>) converter);
|
121 | 133 | added = true;
|
122 | 134 | }
|
| 135 | + |
| 136 | + if (converter instanceof GenericConverter) { |
| 137 | + this.conversionService.addConverter((GenericConverter) converter); |
| 138 | + added = true; |
| 139 | + } |
| 140 | + |
123 | 141 | if (!added) {
|
124 | 142 | throw new IllegalArgumentException("Given set contains element that is neither Converter nor ConverterFactory!");
|
125 | 143 | }
|
@@ -222,4 +240,19 @@ public BasicDBList maybeConvertList(BasicDBList dbl) {
|
222 | 240 | }
|
223 | 241 | return newDbl;
|
224 | 242 | }
|
| 243 | + |
| 244 | + |
| 245 | + private enum CustomToStringConverter implements GenericConverter { |
| 246 | + INSTANCE; |
| 247 | + |
| 248 | + public Set<ConvertiblePair> getConvertibleTypes() { |
| 249 | + ConvertiblePair localeToString = new ConvertiblePair(Locale.class, String.class); |
| 250 | + ConvertiblePair booleanToString = new ConvertiblePair(Character.class, String.class); |
| 251 | + return new HashSet<ConvertiblePair>(Arrays.asList(localeToString, booleanToString)); |
| 252 | + } |
| 253 | + |
| 254 | + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 255 | + return source.toString(); |
| 256 | + } |
| 257 | + } |
225 | 258 | }
|
0 commit comments