@@ -239,10 +239,41 @@ static Json of(Collection<? extends JsonEncodable> value) {
239239 );
240240 }
241241
242+ /**
243+ * Creates {@link Json} from a {@link Collection} of items which
244+ * can be encoded with a provided {@link JsonEncoder}.
245+ *
246+ * @param value The value to be encoded.
247+ * @return An instance of {@link Json}.
248+ */
249+ static <T > Json of (Collection <? extends T > value , JsonEncoder <T > encoder ) {
250+ return value == null
251+ ? JsonNull .instance ()
252+ : new ArrayImpl (
253+ value .stream ()
254+ .map (v -> {
255+ var result = encoder .encode (v );
256+ if (result == null ) {
257+ return JsonNull .instance ();
258+ }
259+ else {
260+ return result ;
261+ }
262+ })
263+ .toList ()
264+ );
265+ }
266+
267+
242268 /**
243269 * Creates {@link Json} from a {@link Map} with {@link String} keys to values which
244270 * implement {@link JsonEncodable}.
245271 *
272+ * <p>
273+ * Note that this method is null-safe when provided a null container,
274+ * but only null-safe for map values if the provided encoder also is.
275+ * </p>
276+ *
246277 * @param value The value to be encoded.
247278 * @return An instance of {@link Json}.
248279 */
@@ -262,6 +293,40 @@ static Json of(Map<String, ? extends JsonEncodable> value) {
262293 );
263294 }
264295
296+ /**
297+ * Creates {@link Json} from a {@link Map} with {@link String} keys to values which
298+ * can be encoded with a provided {@link JsonEncoder}.
299+ *
300+ * <p>
301+ * Note that this method is null-safe when provided a null container,
302+ * but only null-safe for map values if the provided encoder also is.
303+ * </p>
304+ *
305+ * @param value The value to be encoded.
306+ * @return An instance of {@link Json}.
307+ */
308+ static <T > Json of (Map <String , ? extends T > value , JsonEncoder <T > encoder ) {
309+ return value == null
310+ ? JsonNull .instance ()
311+ : new ObjectImpl (
312+ value
313+ .entrySet ()
314+ .stream ()
315+ .collect (Collectors .toUnmodifiableMap (
316+ Map .Entry ::getKey ,
317+ entry -> {
318+ var result = encoder .encode (entry .getValue ());
319+ if (result == null ) {
320+ return JsonNull .instance ();
321+ }
322+ else {
323+ return result ;
324+ }
325+ }
326+ ))
327+ );
328+ }
329+
265330 /**
266331 * Creates a new {@link JsonObject.Builder}.
267332 *
0 commit comments