javaee / json-processing-spec

Legacy JSON Processing spec. Please use the link below to find the current JSON P project
https://github.com/jakartaee/jsonp-api
Other
8 stars 3 forks source link

Simple JSON object queries #82

Closed glassfishrobot closed 7 years ago

glassfishrobot commented 7 years ago

68 introduced simple API for JSON array queries. It would be nice to add the same functionality for JSON object.

JsonObject in = ...
Collector<Map.Entry<String,JsonValue>, JsonObjectBuilder, JsonObject> col = JsonCollectors.toJsonObject();
JsonObject out = in.getEntries().stream()
        // Do whatever you want (e.g. filter())
        .collect(col);

Affected Versions

[1.1]

glassfishrobot commented 7 years ago

Reported by kratz

glassfishrobot commented 7 years ago

kratz said: Diff to be applied on JSON-P master:

diff --git a/api/src/main/java/javax/json/stream/JsonCollectors.java b/api/src/main/java/javax/json/stream/JsonCollectors.java
index b6db550..0ccfaf8 100644
--- a/api/src/main/java/javax/json/stream/JsonCollectors.java
+++ b/api/src/main/java/javax/json/stream/JsonCollectors.java
@@ -82,6 +82,19 @@ public final class JsonCollectors {
     }

     /**
+     * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code Map.Entry<String,JsonValue>}
+     * elements into a {@code JsonObject}.
+     * @return the constructed Collector
+     */
+    public static Collector<Map.Entry<String,JsonValue>, JsonObjectBuilder, JsonObject> toJsonObject() {
+        return Collector.of(
+Json::createObjectBuilder,
+(JsonObjectBuilder b, Map.Entry<String,JsonValue> v) -> b.add(v.getKey(), v.getValue()),
+JsonObjectBuilder::addAll,
+JsonObjectBuilder::build);
+    }
+
+    /**
      * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code JsonValue}
      * elements into a {@code JsonObject}.  The name/value pairs of the {@code JsonObject} are computed
      * by applying the provided mapping functions.

Edit: There is no need to modify JsonObject because this interface extends Map and entrySet().stream() is enough to open the stream of Map.Entry<String,JsonValue>.

glassfishrobot commented 7 years ago

kratz said: Also new toJsonObject() is counterpart to JsonParser#getObjectStream() the same way as toJsonArray() method is for JsonParser#getArrayStream(). Using those collectors with JsonParser is another use-case for API changes introduced in #68 and this task.

Anither question is, whether it would look better to hide Map.Entry<String,JsonValue> behind some better identifier (interface), e.g. ObjectValueMapping.

glassfishrobot commented 7 years ago

@lukasj said: https://java.net/projects/jsonp/sources/git/revision/2eed5bf

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA JSON_PROCESSING_SPEC-82

glassfishrobot commented 7 years ago

Marked as fixed on Monday, January 30th 2017, 8:29:42 am