Class JsonCollectors


  • public final class JsonCollectors
    extends java.lang.Object
    This class contains some implementations of java.util.stream.Collector for accumulating JsonValues into JsonArray and JsonObject.

    This is a near-exact copy of the same-titled Java EE 8 Class: javax.json.stream.JsonCollectors
    Commenting has been slightly modified to accompany HiLited Code Examples.
    Java Source Code remains identical to the Sun-Oracle & 'GlassFish' Released Distributions.

    Read included License: HERE, and the CDDL+GPL-1.1
    All javax.json.stream.* Code Obtained From: GitHub JavaEE jsonp  Public Archive.
    Since:
    1.1


    • Method Detail

      • toJsonArray

        🡇     🗕  🗗  🗖
        public static java.util.stream.Collector<JsonValue,​JsonArrayBuilder,​JsonArraytoJsonArray
                    ()
        
        Constructs a java.util.stream.Collector that accumulates the input JsonValue elements into a JsonArray.
        Returns:
        the constructed Collector
        Code:
        Exact Method Body:
         return Collector.of(
                 Json::createArrayBuilder,
                 JsonArrayBuilder::add,
                 JsonArrayBuilder::addAll,
                 JsonArrayBuilder::build);
        
      • toJsonObject

        🡅  🡇     🗕  🗗  🗖
        public static java.util.stream.Collector<java.util.Map.Entry<java.lang.String,​JsonValue>,​JsonObjectBuilder,​JsonObjecttoJsonObject
                    ()
        
        Constructs a java.util.stream.Collector that accumulates the input Map.Entry<String,JsonValue> elements into a JsonObject.
        Returns:
        the constructed Collector
        Code:
        Exact Method Body:
         return Collector.of(
                 Json::createObjectBuilder,
                 (JsonObjectBuilder b, Map.Entry<String, JsonValue> v) -> b.add(v.getKey(), v.getValue()),
                 JsonObjectBuilder::addAll,
                 JsonObjectBuilder::build);
        
      • toJsonObject

        🡅  🡇     🗕  🗗  🗖
        public static java.util.stream.Collector<JsonValue,​JsonObjectBuilder,​JsonObjecttoJsonObject​
                    (java.util.function.Function<JsonValue,​java.lang.String> keyMapper,
                     java.util.function.Function<JsonValue,​JsonValue> valueMapper)
        
        Constructs a java.util.stream.Collector that accumulates the input JsonValue elements into a JsonObject. The name/value pairs of the JsonObject are computed by applying the provided mapping functions.
        Parameters:
        keyMapper - a mapping function to produce names.
        valueMapper - a mapping function to produce values
        Returns:
        the constructed Collector
        Code:
        Exact Method Body:
         return Collector.of(
                 Json::createObjectBuilder,
                 (b, v) -> b.add(keyMapper.apply(v), valueMapper.apply(v)),
                 JsonObjectBuilder::addAll,
                 JsonObjectBuilder::build,
                 Collector.Characteristics.UNORDERED);
        
      • groupingBy

        🡅  🡇     🗕  🗗  🗖
        public static <T extends JsonArrayBuilder> java.util.stream.Collector<JsonValue,​java.util.Map<java.lang.String,​T>,​JsonObjectgroupingBy​
                    (java.util.function.Function<JsonValue,​java.lang.String> classifier,
                     java.util.stream.Collector<JsonValue,​T,​JsonArray> downstream)
        
        Constructs a java.util.stream.Collector that implements a "group by" operation on the input JsonValue elements. A classifier function maps the input JsonValues to keys, and the JsonValues are partitioned into groups according to the value of the key. A reduction operation is performed on the JsonValues in each group, using the downstream Collector. For each group, the key and the results of the reduction operation become the name/value pairs of the resultant JsonObject.
        Type Parameters:
        T - the intermediate accumulation JsonArrayBuilder of the downstream collector
        Parameters:
        classifier - a function mapping the input JsonValues to a String, producing keys
        downstream - a Collector that implements a reduction operation on the JsonValues in each group.
        Returns:
        the constructed Collector
        Code:
        Exact Method Body:
         BiConsumer<Map<String, T>, JsonValue> accumulator =
             (map, value) -> {
                 String key = classifier.apply(value);
                 if (key == null) {
                     throw new JsonException("element cannot be mapped to a null key");
                 }
                 // Build a map of key to JsonArrayBuilder
                 T arrayBuilder =
                     map.computeIfAbsent(key, v->downstream.supplier().get());
                 // Add elements from downstream Collector to the arrayBuilder.
                 downstream.accumulator().accept(arrayBuilder, value);
             };
         Function<Map<String, T>, JsonObject> finisher =
             map -> {
                 // transform the map of name: JsonArrayBuilder to
                 //                      name: JsonArray
                 // using the downstream collector for reducing the JsonArray
                 JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
                 map.forEach((k, v) -> {
                     JsonArray array = downstream.finisher().apply(v);
                     objectBuilder.add(k, array);
                 });
                 return objectBuilder.build();
             };
         BinaryOperator<Map<String, T>> combiner =
             (map1, map2) -> {
                 map1.putAll(map2);
                 return map1;
             };
         return Collector.of(HashMap::new, accumulator, combiner, finisher,
             Collector.Characteristics.UNORDERED);
        
      • groupingBy

        🡅     🗕  🗗  🗖
        public static java.util.stream.Collector<JsonValue,​java.util.Map<java.lang.String,​JsonArrayBuilder>,​JsonObjectgroupingBy​
                    (java.util.function.Function<JsonValue,​java.lang.String> classifier)
        
        Constructs a java.util.stream.Collector that implements a "group by" operation on the input JsonValue elements. A classifier function maps the input JsonValues to keys, and the JsonValues are partitioned into groups according to the value of the key. The JsonValues in each group are added to a JsonArray. The key and the JsonArray in each group becomes the name/value pair of the resultant JsonObject.
        Parameters:
        classifier - a function mapping the input JsonValues to a String, producing keys
        Returns:
        the constructed Collector
        Code:
        Exact Method Body:
         return groupingBy(classifier, toJsonArray());