/** * Returns a list view for the array. The value and the type of the elements * in the list is specified by the {@code func} argument. * <p>This method can be used to obtain a list of the unwrapped types, such as * <pre>{@code * List<String> strings = ary1.getValuesAs(JsonString::getString); * List<Integer> ints = ary2.getValuesAs(JsonNumber::intValue); * } </pre> * or a list of simple projections, such as * <pre> {@code * List<Integer> stringsizes = ary1.getValueAs((JsonString v)->v.getString().length(); * } </pre> * @param <K> The element type (must be a subtype of JsonValue) of this JsonArray. * @param <T> The element type of the returned List * @param func The function that maps the elements of this JsonArray to the target elements. * @return A List of the specified values and type. * @throws ClassCastException if the {@code JsonArray} contains a value of wrong type * * @since 1.1 */ default <T, K extends JsonValue> List<T> getValuesAs(Function<K, T> func) { @SuppressWarnings("unchecked") Stream<K> stream = (Stream<K>) stream(); return stream.map(func).collect(Collectors.toList()); }
private Set<String> extractContentTagSet(JsonObject talkObject) { if (talkObject.containsKey("tags")) { return talkObject.getJsonArray("tags").stream() .map(JsonObject.class::cast) .filter(tagObject -> !tagObject.getString("value").isEmpty()) .map(tagObject -> tagObject.getString("value")) .collect(Collectors.toSet()); } return new HashSet<>(); }
/** * Extract a key value from json object that is a list of strings if present. * * @param json JsonObject to read key from * @param key key of the value we want to read * @return List of String value if present * @throws JwtException in case the key is of invalid content */ public static Optional<List<String>> getStrings(JsonObject json, String key) throws JwtException { return Optional.ofNullable(json.getJsonArray(key)) .map(it -> { try { return it.stream().map(it2 -> ((JsonString) it2).getString()).collect(Collectors.toList()); } catch (Exception e) { throw new JwtException("Invalid value. Expecting a string array for key " + key); } }); }
private boolean hasValidAudience(JsonObject claims) throws RealmUnavailableException { if (this.audiences.isEmpty()) return true; JsonValue audience = claims.get("aud"); if (audience == null) { log.debug("Token does not contain an audience claim"); return false; } JsonArray audClaimArray; if (JsonValue.ValueType.STRING.equals(audience.getValueType())) { audClaimArray = Json.createArrayBuilder().add(audience).build(); } else { audClaimArray = (JsonArray) audience; } boolean valid = audClaimArray.stream() .map(jsonValue -> (JsonString) jsonValue) .anyMatch(audience1 -> audiences.contains(audience1.getString())); if (!valid) { log.debugf("Audience check failed. Provided [%s] but was expected [%s].", audClaimArray.toArray(), this.audiences); } return valid; }
@Override public Stream<JsonValue> getArrayStream() { //X TODO this implementation is very simplistic //X I find it unintuitive what the spec intends here //X we probably need to improve this return getArray().stream(); }
@Override public Stream<JsonValue> getArrayStream() { //X TODO this implementation is very simplistic //X I find it unintuitive what the spec intends here //X we probably need to improve this return getArray().stream(); }
private <T> Collection<T> deserializeCollection( ModuleDescriptor module, CollectionType collectionType, JsonArray json ) { return (Collection<T>) json.stream() .map( item -> doDeserialize( module, collectionType.collectedType(), item ) ) .collect( toCollection( () -> collectionType.isSet() ? new LinkedHashSet<>() : new ArrayList<>() ) ); }
@SuppressWarnings("unchecked") public static <T> T unbox(JsonValue value) { return (T) unbox(value, json -> json.getValueType() == ValueType.ARRAY ? ((JsonArray) json).stream() .map(JsonUtil::unbox) .collect(Collectors.toList()) : ((JsonObject) json).entrySet().stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey(),unbox(entry.getValue()))) .filter(entry -> entry.getValue() != null) .collect(Collectors.toMap(Entry::getKey, Entry::getValue))); }
private <T> Collection<T> deserializeCollection( ModuleDescriptor module, CollectionType collectionType, JsonArray json ) { return (Collection<T>) json.stream() .map( item -> doDeserialize( module, collectionType.collectedType(), item ) ) .collect( toCollection( () -> collectionType.isSet() ? new LinkedHashSet<>() : new ArrayList<>() ) ); }
private Set<String> extractContentTagSet(JsonObject talkObject) { if (talkObject.containsKey("tags")) { return talkObject.getJsonArray("tags").stream() .map(JsonObject.class::cast) .filter(tagObject -> !tagObject.getString("value").isEmpty()) .map(tagObject -> tagObject.getString("value")) .collect(Collectors.toSet()); } return new HashSet<>(); }
@Override public Long containers() { JsonArray containers = this.getJsonArray("Containers"); return containers.stream().map(JsonObject.class::cast) .mapToLong(c -> c.getJsonNumber("SizeRootFs").longValue()) .sum(); }
@Override public Optional<Function<JsonValue, Stream<ValidationResult.ValidationError>>> create(final ValidationContext model) { return ofNullable(model.getSchema().get("required")) .filter(it -> it.getValueType() == JsonValue.ValueType.ARRAY) .map(JsonValue::asJsonArray) .filter(arr -> arr.stream().allMatch(it -> it.getValueType() == JsonValue.ValueType.STRING)) .map(arr -> arr.stream().map(it -> JsonString.class.cast(it).getString()).collect(toSet())) .map(required -> new Impl(required, model.getValueProvider(), model.toPointer())); }
public OrderResult(JsonObject jsonObject) { super( jsonObject.getBoolean("result"), 0, jsonObject .getJsonArray("orders") .stream() .map(v -> { JsonObject o = (JsonObject) v; return new Order(o); }) .collect(Collectors.toList()) .toArray(EMPTY_ORDERS) ); }
private static Candlestick[] parseCandlesticks(JsonArray jsonArray) { if (jsonArray.get(0).getValueType() == ValueType.ARRAY) { // first push contains multiple candlesticks. return jsonArray.stream().map(e -> new Candlestick(e)) .collect(toList()) .toArray(EMPTY_CANDLESTICK_ARRAY); } else { // the follows contain only one candlestick. return new Candlestick[] { new Candlestick(jsonArray) }; } }
/** * Extract a key value from json object that is a list of strings if present. * * @param json JsonObject to read key from * @param key key of the value we want to read * @return List of String value if present * @throws JwtException in case the key is of invalid content */ public static Optional<List<String>> getStrings(JsonObject json, String key) throws JwtException { return Optional.ofNullable(json.getJsonArray(key)) .map(it -> { try { return it.stream().map(it2 -> ((JsonString) it2).getString()).collect(Collectors.toList()); } catch (Exception e) { throw new JwtException("Invalid value. Expecting a string array for key " + key); } }); }
@Override public Long volumes() { JsonArray volumes = this.getJsonArray("Volumes"); return volumes.stream().map(JsonObject.class::cast) .mapToLong(v -> v.getJsonObject("UsageData") .getJsonNumber("Size").longValue()) .sum(); }
public Depth(JsonArray asks, JsonArray bids, Instant timestamp) { super(asks.stream().map(v -> { JsonArray a = (JsonArray) v; BigDecimal px = a.getJsonNumber(0).bigDecimalValue(); BigDecimal qty = a.getJsonNumber(1).bigDecimalValue(); return new BigDecimal[] { px, qty }; }).collect(Collectors.toList()).toArray(EMPTY_DEPTH), bids.stream().map(v -> { JsonArray a = (JsonArray) v; BigDecimal px = a.getJsonNumber(0).bigDecimalValue(); BigDecimal qty = a.getJsonNumber(1).bigDecimalValue(); return new BigDecimal[] { px, qty }; }).collect(Collectors.toList()).toArray(EMPTY_DEPTH)); this.timestamp = timestamp; }
public Collection<IronQueue> getQueues() { Response response = request(getSettings(), HttpMethod.GET, "queues"); if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { throw new IronClientException(response); } return response.readEntity(JsonArray.class) .stream() .map(jsonValue -> getQueue(((JsonObject) jsonValue).getString("name"))) .collect(Collectors.toList()); }
@Override public Optional<Function<JsonValue, Stream<ValidationResult.ValidationError>>> create(final ValidationContext model) { final JsonValue value = model.getSchema().get("type"); if (JsonString.class.isInstance(value)) { return Optional.of(new Impl(model.toPointer(), model.getValueProvider(), mapType(JsonString.class.cast(value)).toArray(JsonValue.ValueType[]::new))); } if (JsonArray.class.isInstance(value)) { return Optional.of(new Impl(model.toPointer(), model.getValueProvider(), value.asJsonArray().stream().flatMap(this::mapType).toArray(JsonValue.ValueType[]::new))); } throw new IllegalArgumentException(value + " is neither an array or string nor a string"); }
/** * {@inheritDoc} */ @Override public OKCoinData[] decode(Reader reader) throws DecodeException, IOException { JsonReader jsonReader = Json.createReader(reader); JsonArray jsonArray = jsonReader.readArray(); return jsonArray.stream().map(v -> { if (log.isTraceEnabled()) { log.trace("Decoding: {}", v); } JsonObject o = (JsonObject) v; String channel = o.getString("channel"); Object data = decodeData(channel, o.get("data")); return new OKCoinData(channel, data); }).collect(Collectors.toList()).toArray(EMPTY_DATA); }