private JsonValue fixViolation(final JsonPointer key, final JsonValue value, final int overhead, final int threshold) { final int cutAt = Math.max(0, threshold - totalOverhead(key, overhead)); return JsonValue.of(value.asString().substring(0, cutAt)); }
private JsonValue fixViolation(final JsonPointer key, final JsonValue value, final int overhead, final int threshold) { final int cutAt = Math.max(0, threshold - totalOverhead(key, overhead)); return JsonValue.of(value.asString().substring(0, cutAt)); }
/** * Return this JSON value in string representation, without quoting if this is a JSON string already. * * @return the string representation. */ default String formatAsString() { return isString() ? asString() : toString(); } }
/** * Return this JSON value in string representation, without quoting if this is a JSON string already. * * @return the string representation. */ default String formatAsString() { return isString() ? asString() : toString(); }
private static Map<String, String> getSpecificConfiguration(final JsonObject jsonObject) { return jsonObject.getValue(JsonFields.SPECIFIC_CONFIG) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(JsonObject::stream) .map(jsonFields -> jsonFields.collect(Collectors.toMap(JsonField::getKeyName, f -> f.getValue().isString() ? f.getValue().asString() : f.getValue().toString()))) .orElse(Collections.emptyMap()); }
private static Map<String, String> getSpecificConfiguration(final JsonObject jsonObject) { return jsonObject.getValue(JsonFields.SPECIFIC_CONFIG) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(JsonObject::stream) .map(jsonFields -> jsonFields.collect(Collectors.toMap(JsonField::getKeyName, f -> f.getValue().isString() ? f.getValue().asString() : f.getValue().toString()))) .orElse(Collections.emptyMap()); }
private boolean violatesThreshold(final JsonPointer key, final JsonValue value, final int overhead, final int threshold) { return null != value && value.isString() && !value.isNull() && threshold < value.asString().length() + totalOverhead(key, overhead); }
private boolean violatesThreshold(final JsonPointer key, final JsonValue value, final int overhead, final int threshold) { return null != value && value.isString() && !value.isNull() && threshold < value.asString().length() + totalOverhead(key, overhead); }
@Override public WithEntity setEntity(final JsonValue entity) { final ConnectionStatus connectionStatusToSet = ConnectionStatus.forName(entity.asString()).orElse(ConnectionStatus.UNKNOWN); return of(connectionId, connectionStatusToSet, getDittoHeaders()); }
@Override public WithEntity setEntity(final JsonValue entity) { final ConnectionStatus connectionStatusToSet = ConnectionStatus.forName(entity.asString()).orElse(ConnectionStatus.UNKNOWN); return of(connectionId, connectionStatusToSet, getDittoHeaders()); }
/** * Creates a new {@code HeaderMapping} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the HeaderMapping to be created. * @return a new HeaderMapping which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ public static HeaderMapping fromJson(final JsonObject jsonObject) { return new ImmutableHeaderMapping(jsonObject.stream() .filter(f -> f.getValue().isString()) .collect(Collectors.toMap(JsonField::getKeyName, jsonField -> jsonField.getValue().asString()))); }
@Override public RetrievePolicyIdResponse setEntity(final JsonValue entity) { checkNotNull(entity, "entity"); return of(thingId, entity.asString(), getDittoHeaders()); }
@Override public RetrievePolicyIdResponse setEntity(final JsonValue entity) { checkNotNull(entity, "entity"); return of(thingId, entity.asString(), getDittoHeaders()); }
/** * Verifies that the actual value returns the expected simple string. If the check should apply real JSON semantics * use {@link #hasJsonString(String)} instead. * * @param expectedString the simple string the actual {@link JsonValue} is expected to return when * {@link JsonValue#toString()} is called. * @return this assert to allow method chaining. */ public JsonValueAssert hasSimpleString(final String expectedString) { isNotNull(); Assertions.assertThat(actual.asString()).isEqualTo(expectedString); return this; }
protected static Map<String, String> toMap(final JsonValueContainer<JsonField> jsonObject) { checkNotNull(jsonObject, "JSON object"); final Map<String, String> result = new HashMap<>(jsonObject.getSize()); jsonObject.forEach(jsonField -> { final JsonValue jsonValue = jsonField.getValue(); final String stringValue = jsonValue.isString() ? jsonValue.asString() : jsonValue.toString(); result.put(jsonField.getKeyName(), stringValue); }); return result; }
protected static Map<String, String> toMap(final JsonValueContainer<JsonField> jsonObject) { checkNotNull(jsonObject, "JSON object"); final Map<String, String> result = new HashMap<>(jsonObject.getSize()); jsonObject.forEach(jsonField -> { final JsonValue jsonValue = jsonField.getValue(); final String stringValue = jsonValue.isString() ? jsonValue.asString() : jsonValue.toString(); result.put(jsonField.getKeyName(), stringValue); }); return result; }
private static boolean isJsonValueIllegal(final JsonValue entity) { final boolean result; if (entity.isArray()) { result = entity.asArray().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonValueIllegal); } else if (entity.isObject()) { result = entity.asObject().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonFieldIllegal); } else if (entity.isString()) { result = isStringIllegal(entity.asString()); } else { result = false; } return result; }
private static JsonObject createPolicyJsonObjectForPut(final String jsonString, final String policyId) { final JsonObject policyJsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); policyJsonObject.getValue(Policy.JsonFields.ID.getPointer()) .ifPresent(policyIdJsonValue -> { if (!policyIdJsonValue.isString() || !policyId.equals(policyIdJsonValue.asString())) { throw PolicyIdNotExplicitlySettableException.newBuilder().build(); } }); return policyJsonObject.setValue(Policy.JsonFields.ID.getPointer(), JsonValue.of(policyId)); }
private static HttpResponse addEntityAccordingToContentType(final HttpResponse response, final JsonValue entity, final DittoHeaders dittoHeaders) { if (hasPlainTextContentType(dittoHeaders)) { return response.withEntity(CONTENT_TYPE_TEXT, ByteString.fromString(entity.asString())); } else { return response.withEntity(CONTENT_TYPE_JSON, ByteString.fromString(entity.toString())); } }
private void addInternalAttributes(final String path, final JsonValue jsonValue) { if (jsonValue == null || jsonValue.isNull()) { attributeInternally(path, null); } else if (jsonValue.isString()) { attributeInternally(path, jsonValue.asString()); } else if (jsonValue.isBoolean()) { attributeInternally(path, jsonValue.asBoolean()); } else if (jsonValue.isNumber()) { addNumberAttribute(path, jsonValue); } else if (jsonValue.isObject()) { jsonValue.asObject().getKeys().forEach(key -> // addInternalAttributes(path + PersistenceConstants.SLASH + key, jsonValue.asObject().getValue(key).orElse(null))); } }