/** * Sets message, description and link from a JSON object of it has matching fields with valid values. * * @param jsonObject The JSON object to read from. * @throws NullPointerException if {@code jsonObject} is {@code null}. */ public DittoRuntimeExceptionBuilder<T> loadJson(final JsonObject jsonObject) { jsonObject.getValue(MESSAGE).ifPresent(this::message); jsonObject.getValue(DESCRIPTION).ifPresent(this::description); jsonObject.getValue(HREF).map(URI::create).ifPresent(this::href); return this; }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { return wrapped.getValueOrThrow(fieldDefinition); }
@Override public JsonObject get(final JsonPointer pointer) { return wrapped.get(pointer); }
/** * A "payload" object was wrapping the events payload until the introduction of "cr-commands 1.0.0". This field has * to be used as fallback for already persisted events with "things-model" < 3.0.0. Removing this workaround is * possible if we are sure that no "old" events are ever loaded again! */ private static JsonObject migratePayload(final JsonObject jsonObject) { return jsonObject.getValue(PAYLOAD) .map(obj -> jsonObject.remove(PAYLOAD.getPointer()).setAll(obj)) .orElse(jsonObject); }
private static boolean needsSourceFilterMigration(final JsonObject connectionJsonObject) { final Optional<JsonArray> sources = connectionJsonObject.getValue(Connection.JsonFields.SOURCES); return sources.isPresent() && sources.get().stream().filter(JsonValue::isObject) .map(JsonValue::asObject).anyMatch(source -> source.contains("filters")); }
private static void handleObject(final String path, final JsonValue value, final List<Document> flatAttributes) { final JsonObject jsonObject = value.asObject(); jsonObject.getKeys().forEach(key -> { final String newPath = path + PersistenceConstants.SLASH + key; final JsonValue innerValue = jsonObject.getValue(key).orElse(null); toFlatAttributesList(newPath, innerValue, flatAttributes); }); }
/** * Creates a new {@code MappingContext} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the MappingContext to be created. * @return a new MappingContext 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 MappingContext fromJson(final JsonObject jsonObject) { final String mappingEngine = jsonObject.getValueOrThrow(JsonFields.MAPPING_ENGINE); final Map<String, String> options = jsonObject.getValueOrThrow(JsonFields.OPTIONS).stream() .collect(Collectors.toMap( e -> e.getKey().toString(), e -> e.getValue().isString() ? e.getValue().asString() : e.getValue().toString()) ); return of(mappingEngine, options); }
private String encodeCorrelationId(final DittoHeaders dittoHeaders) { return JsonObject.newBuilder() .set(BATCH_ID_FIELD, batchId) .set(RANDOM_FIELD, UUID.randomUUID().toString()) .set(ORIGINAL_CORRELATION_ID, dittoHeaders.getCorrelationId().orElse(UUID.randomUUID().toString())) .build() .toString(); }
@Override public Stream<JsonField> stream() { return wrapped.stream(); }
@Override public boolean contains(final CharSequence key) { return wrapped.contains(key); }
private static Optional<Policy> getInlinedOrDefaultPolicyForCreateThing(final CreateThing createThing) { final Optional<JsonObject> initialPolicy = createThing.getInitialPolicy(); if (initialPolicy.isPresent()) { final JsonObject policyJson = initialPolicy.get(); final JsonObjectBuilder policyJsonBuilder = policyJson.toBuilder(); final Thing thing = createThing.getThing(); if (thing.getPolicyId().isPresent() || !policyJson.contains(Policy.JsonFields.ID.getPointer())) { final String policyId = thing.getPolicyId().orElse(createThing.getThingId()); policyJsonBuilder.set(Policy.JsonFields.ID, policyId); } return Optional.of(PoliciesModelFactory.newPolicy(policyJsonBuilder.build())); } else { return getDefaultPolicy(createThing.getDittoHeaders().getAuthorizationContext(), createThing.getThingId()); } }
private static boolean needsSourceFilterMigration(final JsonObject connectionJsonObject) { final Optional<JsonArray> sources = connectionJsonObject.getValue(Connection.JsonFields.SOURCES); return sources.isPresent() && sources.get().stream().filter(JsonValue::isObject) .map(JsonValue::asObject).anyMatch(source -> source.contains("filters")); }
@SuppressWarnings("squid:CallToDeprecatedMethod") private static JsonObject migrateId(final JsonObject jsonObject) { return jsonObject.getValue(Event.JsonFields.ID) .map(name -> name.replaceFirst("thing", "")) .map(Introspector::decapitalize) .map(name -> ThingEvent.TYPE_PREFIX + name) .map(JsonValue::of) .map(type -> jsonObject.setValue(Event.JsonFields.TYPE.getPointer(), type)) .orElse(jsonObject); }
@Override @Nonnull public JsonObject toJson(@Nonnull final JsonSchemaVersion schemaVersion, @Nonnull final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder(); jsonObjectBuilder.set(JSON_STREAMING_TYPE, streamingType.getDistributedPubSubTopic(), predicate); jsonObjectBuilder.set(JSON_SUBSCRIBED, subscribed, predicate); return jsonObjectBuilder.build(); }
@Override public Stream<JsonField> stream() { return wrapped.stream(); }
@Override public boolean contains(final CharSequence key) { return wrapped.contains(key); }
private static Thing createThingForPost(final String jsonString) { final JsonObject inputJson = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); if (inputJson.contains(Thing.JsonFields.ID.getPointer())) { throw ThingIdNotExplicitlySettableException.newBuilder(true).build(); } final String thingId = ThingBuilder.generateRandomThingId(); final JsonObjectBuilder outputJsonBuilder = inputJson.toBuilder(); outputJsonBuilder.set(Thing.JsonFields.ID.getPointer(), thingId); return ThingsModelFactory.newThingBuilder(outputJsonBuilder.build()) .setId(thingId) .build(); }
@Override protected String resolveType(final JsonObject jsonObject) { return jsonObject.getValueOrThrow(DevOpsCommandResponse.JsonFields.TYPE); }
/** * Sets message, description and link from a JSON object of it has matching fields with valid values. * * @param jsonObject The JSON object to read from. * @throws NullPointerException if {@code jsonObject} is {@code null}. */ public DittoRuntimeExceptionBuilder<T> loadJson(final JsonObject jsonObject) { jsonObject.getValue(MESSAGE).ifPresent(this::message); jsonObject.getValue(DESCRIPTION).ifPresent(this::description); jsonObject.getValue(HREF).map(URI::create).ifPresent(this::href); return this; }