Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to JsonCollectors interface #269

Open
andrebreves opened this issue Nov 20, 2020 · 0 comments
Open

Improvement to JsonCollectors interface #269

andrebreves opened this issue Nov 20, 2020 · 0 comments

Comments

@andrebreves
Copy link

andrebreves commented Nov 20, 2020

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)
@andrebreves andrebreves changed the title Improvement to JsonCollectors#toJsonObject​() Improvement to JsonCollectors interface Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant