org.eclipse.ditto.json.JsonField类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(94)

本文整理了Java中org.eclipse.ditto.json.JsonField类的一些代码示例,展示了JsonField类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonField类的具体详情如下:
包路径:org.eclipse.ditto.json.JsonField
类名称:JsonField

JsonField介绍

[英]Represents a single JSON field. A JSON field in its simplest form is a key-value-pair. Additionally a field can be aware of its definition which allows to obtain meta information like the Java type of the value or the markers of the field. A JSON object, for example, can be understood as a tree of JSON fields.

Implementations of this interface are required to be immutable!
[中]表示单个JSON字段。最简单形式的JSON字段是键值对。此外,字段可以知道其定义,该定义允许获取元信息,如值的Java类型或字段的标记。例如,JSON对象可以理解为JSON字段树。
此接口的实现要求是不可变的

代码示例

代码示例来源:origin: org.eclipse.ditto/ditto-services-thingsearch-persistence

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);
  });
}

代码示例来源:origin: org.eclipse.ditto/ditto-services-thingsearch-persistence

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;
}

代码示例来源:origin: eclipse/ditto

@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;
}

代码示例来源:origin: org.eclipse.ditto/ditto-model-base

private FieldType() {
  predicate = jsonField -> {
    final Optional<JsonFieldDefinition> definition = jsonField.getDefinition();
    return !definition.isPresent() || jsonField.isMarkedAs(this);
  };
}

代码示例来源:origin: eclipse/ditto

/**
 * 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);
}

代码示例来源:origin: org.eclipse.ditto/ditto-json

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;
}

代码示例来源:origin: org.eclipse.ditto/ditto-json

private Optional<JsonValue> getValueForKey(final CharSequence key) {
  final JsonField jsonField = fields.get(key.toString());
  return null != jsonField ? Optional.of(jsonField.getValue()) : Optional.empty();
}

代码示例来源:origin: eclipse/ditto

@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;
}

代码示例来源:origin: org.eclipse.ditto/ditto-json

@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();
}

代码示例来源:origin: eclipse/ditto

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());
  }
}

代码示例来源:origin: eclipse/ditto

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);

代码示例来源:origin: eclipse/ditto

@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);
  }
}

代码示例来源:origin: eclipse/ditto

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();
}

代码示例来源:origin: org.eclipse.ditto/ditto-model-things

@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());
}

代码示例来源:origin: eclipse/ditto

/**
 * 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()));
}

代码示例来源:origin: eclipse/ditto

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;
}

代码示例来源:origin: org.eclipse.ditto/ditto-json

/**
 * 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();
}

代码示例来源:origin: org.eclipse.ditto/ditto-json

@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;
}

代码示例来源:origin: eclipse/ditto

@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();
}

代码示例来源:origin: eclipse/ditto

@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();
}

相关文章