jsonp-api
jsonp-api copied to clipboard
Improvement to JsonCollectors interface
The current signature of the collector JsonCollectors#toJsonObject() is:
static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObject> toJsonObject()
That signature forces the following code:
Map<String, JsonArray> map = ...;
JsonObject obj = map.entrySet()
.stream()
.collect(toJsonObject());
To create another Map.Entry just to cast the value to JsonValue in order to work:
Map<String, JsonArray> map = ...;
JsonObject obj = map.entrySet()
.stream()
.map(e -> Map.entry(e.getKey(), (JsonValue) e.getValue()))
.collect(toJsonObject());
My suggestion is to change the signature of JsonCollectors#toJsonObject() to:
static Collector<Map.Entry<String, ? extends JsonValue>, JsonObjectBuilder, JsonObject> toJsonObject()
So Map of String to any JsonValue sub-type could be collected to JsonObject with little effort.
And I guess that modification should be applied to the other signatures that have JsonValue on the receiving side as well:
static Collector<? extends JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
groupingBy(Function<? extends JsonValue, String> classifier)
static <T extends JsonArrayBuilder>
Collector<? extends JsonValue, Map<String, T>, JsonObject>
groupingBy(Function<? extends JsonValue, String> classifier, Collector<? extends JsonValue, T, JsonArray> downstream)
static Collector<? extends JsonValue, JsonArrayBuilder, JsonArray> toJsonArray()
static Collector<? extends JsonValue, JsonObjectBuilder, JsonObject>
toJsonObject(Function<? extends JsonValue, String> keyMapper, Function<? extends JsonValue, JsonValue> valueMapper)