private static void handleObject(final String path, final Iterable<JsonField> jsonObject, final String featureId, final List<Document> flatAttributes) { jsonObject.forEach(jsonField -> { final String newPath = path + PersistenceConstants.SLASH + jsonField.getKey(); final JsonValue innerValue = jsonField.getValue(); toFlatFeaturesList(newPath, featureId, innerValue, flatAttributes); }); }
private static List<Document> createFlatFeaturesRepresentation(final Iterable<JsonField> properties, final String featureId) { final List<Document> flatFeatures = new ArrayList<>(); properties.forEach(field -> toFlatFeaturesList(PersistenceConstants.SLASH + field.getKeyName(), featureId, field.getValue(), flatFeatures)); return flatFeatures; }
@Override public ImmutableJsonObjectBuilder setAll(final Iterable<JsonField> fields, final Predicate<JsonField> predicate) { requireNonNull(fields, "The JSON fields to be set must not be null!"); checkPredicate(predicate); StreamSupport.stream(fields.spliterator(), false) .filter(field -> !field.getDefinition().isPresent() || predicate.test(field)) .forEach(fieldToBeSet -> this.fields.put(fieldToBeSet.getKeyName(), fieldToBeSet)); return this; }
private FieldType() { predicate = jsonField -> { final Optional<JsonFieldDefinition> definition = jsonField.getDefinition(); return !definition.isPresent() || jsonField.isMarkedAs(this); }; }
/** * Creates a new {@code Resources} from the specified JSON object. * * @param jsonObject the JSON object of which a new Resources instance is to be created. * @return the {@code Resources} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * 'Resources' format. */ public static Resources fromJson(final JsonObject jsonObject) { final List<Resource> theResources = jsonObject.stream() .filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey())) .map(field -> ImmutableResource.of(ResourceKey.newInstance(field.getKeyName()), field.getValue())) .collect(Collectors.toList()); return of(theResources); }
private ImmutableJsonObjectBuilder remove(final JsonPointer pointer) { pointer.getRoot() .map(JsonKey::toString) .map(fields::get) .ifPresent(jsonField -> { final JsonValue rootValue = jsonField.getValue(); final JsonPointer nextPointerLevel = pointer.nextLevel(); if (rootValue.isObject() && !nextPointerLevel.isEmpty()) { JsonObject rootObject = rootValue.asObject(); rootObject = rootObject.remove(nextPointerLevel); set(JsonFactory.newField(jsonField.getKey(), rootObject, jsonField.getDefinition().orElse(null))); } else { fields.remove(jsonField.getKeyName()); } }); return this; }
private Optional<JsonValue> getValueForKey(final CharSequence key) { final JsonField jsonField = fields.get(key.toString()); return null != jsonField ? Optional.of(jsonField.getValue()) : Optional.empty(); }
@Override public ImmutableJsonObjectBuilder setAll(final Iterable<JsonField> fields) { requireNonNull(fields, "The JSON fields to be set must not be null!"); for (final JsonField jsonField : fields) { this.fields.put(jsonField.getKeyName(), jsonField); } return this; }
@SuppressWarnings("unchecked") private static JsonObject filterByTrie(final JsonObject self, final JsonFieldSelectorTrie trie) { if (trie.isEmpty()) { return self; } final JsonObjectBuilder builder = JsonFactory.newObjectBuilder(); for (final JsonKey key : trie.getKeys()) { self.getField(key).ifPresent(child -> { final JsonValue childValue = child.getValue(); final JsonValue filteredChildValue = childValue.isObject() ? filterByTrie(childValue.asObject(), trie.descend(key)) : childValue; final Optional<JsonFieldDefinition> childFieldDefinition = child.getDefinition(); if (childFieldDefinition.isPresent()) { builder.set(childFieldDefinition.get(), filteredChildValue); } else { builder.set(key, filteredChildValue); } }); } return builder.build(); }
private static void compareFieldDefinitions(final JsonField expectedField, final JsonField actualField) { final Optional<JsonFieldDefinition> expectedFieldDefinition = expectedField.getDefinition(); final Optional<JsonFieldDefinition> actualFieldDefinition = actualField.getDefinition(); if (expectedFieldDefinition.isPresent()) { Assertions.assertThat(actualFieldDefinition) .overridingErrorMessage("Expected JsonField <%s> to have definition <%s> but it had " + "none", expectedField.getKey(), expectedFieldDefinition.get()) .isPresent(); Assertions.assertThat(expectedFieldDefinition) .as("Definitions of JsonField <%s> are equal", expectedField.getKey()) .contains(actualFieldDefinition.get()); } }
if (1 >= pointer.getLevelCount()) { result = rootKeyValue.map( jsonValue -> JsonField.newInstance(rootKey, jsonValue, rootKeyDefinition.orElse(null))) .map(jsonField -> Collections.singletonMap(jsonField.getKeyName(), jsonField)) .map(ImmutableJsonObject::of) .orElseGet(ImmutableJsonObject::empty); .map(jsonValue -> JsonField.newInstance(rootKey, jsonValue, rootKeyDefinition.orElse(null))) .map(jsonField -> Collections.singletonMap(jsonField.getKeyName(), jsonField)) .map(ImmutableJsonObject::of) .orElseGet(ImmutableJsonObject::empty);
@Override public void endObjectValue(final List<JsonField> jsonFields, final String name) { final JsonField jsonField = JsonField.newInstance(name, jsonValue); jsonFields.add(jsonField); final StringBuilder stringBuilder = stringBuilders.peek(); if (null != stringBuilder) { stringBuilder.append(getEscapedJsonString(name)); stringBuilder.append(':'); stringBuilder.append(valueString); stringBuilder.append(DELIMITER); } }
private static Optional<JsonField> getField(final Iterable<JsonField> jsonFields, final JsonKey key) { return StreamSupport.stream(jsonFields.spliterator(), false) .filter(jsonField -> Objects.equals(jsonField.getKey(), key)) .findAny(); }
@Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); return stream() // .filter(field -> !field.getDefinition().isPresent() || predicate.test(field)) // .collect(JsonCollectors.fieldsToObject()); }
/** * Creates a new {@code Resource} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the Resource to be created. * @return a new Resource which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws DittoJsonException if {@code jsonObject} * <ul> * <li>is empty,</li> * <li>contains only a field with the schema version</li> * <li>or it contains more than two fields.</li> * </ul> */ public static Resource fromJson(final JsonObject jsonObject) { checkNotNull(jsonObject, "JSON object"); return jsonObject.stream() .filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey())) .findFirst() .map(field -> of(ResourceKey.newInstance(field.getKeyName()), field.getValue())) .orElseThrow(() -> new DittoJsonException(JsonMissingFieldException.newBuilder() .message("The JSON object for the 'resource' is missing.") .build())); }
private ImmutableJsonObjectBuilder remove(final JsonPointer pointer) { pointer.getRoot() .map(JsonKey::toString) .map(fields::get) .ifPresent(jsonField -> { final JsonValue rootValue = jsonField.getValue(); final JsonPointer nextPointerLevel = pointer.nextLevel(); if (rootValue.isObject() && !nextPointerLevel.isEmpty()) { JsonObject rootObject = rootValue.asObject(); rootObject = rootObject.remove(nextPointerLevel); set(JsonFactory.newField(jsonField.getKey(), rootObject, jsonField.getDefinition().orElse(null))); } else { fields.remove(jsonField.getKeyName()); } }); return this; }
/** * Returns a {@code Predicate} for testing if the value of a JSON field is null. * * @return the predicate. * @see JsonValue#isNull() */ static Predicate<JsonField> isValueNonNull() { return jsonField -> !jsonField.getValue().isNull(); }
@Override public ImmutableJsonObjectBuilder setAll(final Iterable<JsonField> fields) { requireNonNull(fields, "The JSON fields to be set must not be null!"); for (final JsonField jsonField : fields) { this.fields.put(jsonField.getKeyName(), jsonField); } return this; }
@SuppressWarnings("unchecked") private static JsonObject filterByTrie(final JsonObject self, final JsonFieldSelectorTrie trie) { if (trie.isEmpty()) { return self; } final JsonObjectBuilder builder = JsonObject.newBuilder(); for (final JsonKey key : trie.getKeys()) { self.getField(key).ifPresent(child -> { final JsonValue childValue = child.getValue(); final JsonValue filteredChildValue = childValue.isObject() ? filterByTrie(childValue.asObject(), trie.descend(key)) : childValue; final Optional<JsonFieldDefinition> childFieldDefinition = child.getDefinition(); if (childFieldDefinition.isPresent()) { builder.set(childFieldDefinition.get(), filteredChildValue); } else { builder.set(key, filteredChildValue); } }); } return builder.build(); }
@SuppressWarnings("unchecked") JsonObject buildJsonView(final Iterable<JsonField> jsonFields, final Set<String> subjectIds, final Permissions permissions) { final PolicyTrie defaultPolicyTrie = new PolicyTrie(grantRevokeIndex, Collections.emptyMap()); if (jsonFields instanceof JsonObject && ((JsonObject) jsonFields).isNull()) { return (JsonObject) jsonFields; } final JsonObjectBuilder outputObjectBuilder = JsonFactory.newObjectBuilder(); for (final JsonField field : jsonFields) { final JsonValue jsonView = getViewForJsonFieldOrNull(field, defaultPolicyTrie, subjectIds, permissions); if (null != jsonView) { final Optional<JsonFieldDefinition> definitionOptional = field.getDefinition(); if (definitionOptional.isPresent()) { outputObjectBuilder.set(definitionOptional.get(), jsonView); } else { outputObjectBuilder.set(field.getKey(), jsonView); } } } return outputObjectBuilder.build(); }