19
19
import java .util .Collection ;
20
20
import java .util .Date ;
21
21
import java .util .Map ;
22
+ import java .util .StringJoiner ;
22
23
import java .util .function .Function ;
23
- import java .util .stream .Collectors ;
24
24
import java .util .stream .StreamSupport ;
25
25
26
26
import org .bson .BsonValue ;
27
27
import org .bson .Document ;
28
28
import org .bson .conversions .Bson ;
29
29
import org .bson .json .JsonParseException ;
30
+
30
31
import org .springframework .core .convert .converter .Converter ;
31
32
import org .springframework .lang .Nullable ;
32
33
import org .springframework .util .ObjectUtils ;
@@ -155,12 +156,13 @@ public static Document toDocumentOrElse(String source, Function<String, Document
155
156
156
157
/**
157
158
* Serialize the given {@link Document} as Json applying default codecs if necessary.
158
- *
159
+ *
159
160
* @param source
160
161
* @return
161
- * @since 2.1 .1
162
+ * @since 2.2 .1
162
163
*/
163
- public static String toJson (Document source ) {
164
+ @ Nullable
165
+ public static String toJson (@ Nullable Document source ) {
164
166
165
167
if (source == null ) {
166
168
return null ;
@@ -173,7 +175,8 @@ public static String toJson(Document source) {
173
175
}
174
176
}
175
177
176
- private static String toJson (Object value ) {
178
+ @ Nullable
179
+ private static String toJson (@ Nullable Object value ) {
177
180
178
181
if (value == null ) {
179
182
return null ;
@@ -218,12 +221,13 @@ private static String toString(Collection<?> source) {
218
221
return iterableToDelimitedString (source , "[ " , " ]" , BsonUtils ::toJson );
219
222
}
220
223
221
- private static <T > String iterableToDelimitedString (Iterable <T > source , String prefix , String postfix ,
222
- Converter <? super T , Object > transformer ) {
224
+ private static <T > String iterableToDelimitedString (Iterable <T > source , String prefix , String suffix ,
225
+ Converter <? super T , String > transformer ) {
226
+
227
+ StringJoiner joiner = new StringJoiner (", " , prefix , suffix );
228
+
229
+ StreamSupport .stream (source .spliterator (), false ).map (transformer ::convert ).forEach (joiner ::add );
223
230
224
- return prefix
225
- + StringUtils .collectionToCommaDelimitedString (
226
- StreamSupport .stream (source .spliterator (), false ).map (transformer ::convert ).collect (Collectors .toList ()))
227
- + postfix ;
231
+ return joiner .toString ();
228
232
}
229
233
}
0 commit comments