org.eclipse.ditto.json.JsonObject.remove()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(136)

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

JsonObject.remove介绍

[英]Removes the JSON field to which the given pointer points to. The pointer's leaf is the key of the field to be removed. For example, if on the following JSON object

{ 
"someObjectAttribute": { 
"someKey": { 
"someNestedKey": 42 
} 
} 
}

this method is called with the pointer "/someObjectAttribute/someKey/someNestedKey" the returned object is

{ 
"someObjectAttribute": { 
"someKey": {} 
} 
}

[中]移除给定指针指向的JSON字段。指针的叶是要删除的字段的键。例如,如果在以下JSON对象上

{ 
"someObjectAttribute": { 
"someKey": { 
"someNestedKey": 42 
} 
} 
}

使用指针“/someObjectAttribute/someKey/someNestedKey”调用此方法,则返回的对象为

{ 
"someObjectAttribute": { 
"someKey": {} 
} 
}

代码示例

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

@Override
public FeatureProperties remove(final CharSequence key) {
  return determineResult(() -> wrapped.remove(key));
}

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

@Override
public Attributes remove(final CharSequence key) {
  return determineResult(() -> wrapped.remove(key));
}

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

@Override
public FeatureProperties remove(final CharSequence key) {
  return determineResult(() -> wrapped.remove(key));
}

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

@Override
public Attributes remove(final CharSequence key) {
  return determineResult(() -> wrapped.remove(key));
}

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

/**
 * 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 JsonObject migratePayload(final JsonObject jsonObject) {
  return jsonObject.getValue(PAYLOAD)
      .filter(JsonValue::isObject)
      .map(JsonValue::asObject)
      .map(obj -> jsonObject.remove(PAYLOAD.getPointer()).setAll(obj))
      .orElse(jsonObject);
}

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

/**
 * 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 JsonObject migratePayload(final JsonObject jsonObject) {
  return jsonObject.getValue(PAYLOAD)
      .filter(JsonValue::isObject)
      .map(JsonValue::asObject)
      .map(obj -> jsonObject.remove(PAYLOAD.getPointer()).setAll(obj))
      .orElse(jsonObject);
}

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

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

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

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

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

private JsonObject removeForPointer(final JsonPointer pointer) {
  final JsonObject result;
  final JsonKey rootKey = pointer.getRoot().orElse(ROOT_KEY);
  final Optional<JsonFieldDefinition> rootKeyDefinition = getDefinitionForKey(rootKey);
  if (pointer.isEmpty()) {
    result = this;
  } else if (1 == pointer.getLevelCount()) {
    result = removeValueForKey(rootKey);
  } else {
    final JsonPointer nextPointerLevel = pointer.nextLevel();
    final Predicate<JsonObject> containsNextLevelRootKey = jsonObject -> nextPointerLevel.getRoot()
        .map(jsonObject::contains)
        .orElse(false);
    result = getValueForKey(rootKey)
        .filter(JsonValue::isObject)
        .map(JsonValue::asObject)
        .filter(containsNextLevelRootKey)
        .map(jsonObject -> jsonObject.remove(nextPointerLevel)) // Recursion
        .map(withoutValue -> JsonField.newInstance(rootKey, withoutValue, rootKeyDefinition.orElse(null)))
        .map(this::set)
        .orElse(this);
  }
  return result;
}

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

private JsonObject removeForPointer(final JsonPointer pointer) {
  final JsonObject result;
  final JsonKey rootKey = pointer.getRoot().orElse(ROOT_KEY);
  final Optional<JsonFieldDefinition> rootKeyDefinition = getDefinitionForKey(rootKey);
  if (pointer.isEmpty()) {
    result = this;
  } else if (1 == pointer.getLevelCount()) {
    result = removeValueForKey(rootKey);
  } else {
    final JsonPointer nextPointerLevel = pointer.nextLevel();
    final Predicate<JsonObject> containsNextLevelRootKey = jsonObject -> nextPointerLevel.getRoot()
        .map(jsonObject::contains)
        .orElse(false);
    result = getValueForKey(rootKey)
        .filter(JsonValue::isObject)
        .map(JsonValue::asObject)
        .filter(containsNextLevelRootKey)
        .map(jsonObject -> jsonObject.remove(nextPointerLevel)) // Recursion
        .map(withoutValue -> JsonFactory.newField(rootKey, withoutValue, rootKeyDefinition.orElse(null)))
        .map(this::set)
        .orElse(this);
  }
  return result;
}

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

@Override
public Object toJournal(final Object event) {
  if (event instanceof Event) {
    final Event<?> theEvent = (Event) event;
    final JsonSchemaVersion schemaVersion = theEvent.getImplementedSchemaVersion();
    final JsonObject jsonObject =
        theEvent.toJson(schemaVersion, IS_REVISION.negate().and(FieldType.regularOrSpecial())) //
            // remove the policy entries from thing event payload
            .remove(POLICY_IN_THING_EVENT_PAYLOAD);
    final DittoBsonJson dittoBsonJson = DittoBsonJson.getInstance();
    final Object bson = dittoBsonJson.parse(jsonObject);
    final Set<String> readSubjects = calculateReadSubjects(theEvent);
    return new Tagged(bson, readSubjects);
  } else {
    throw new IllegalArgumentException("Unable to toJournal a non-'Event' object! Was: " + event.getClass());
  }
}

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

@Override
public Object toJournal(final Object event) {
  if (event instanceof Event) {
    final Event<?> theEvent = (Event) event;
    final JsonSchemaVersion schemaVersion = theEvent.getImplementedSchemaVersion();
    final JsonObject jsonObject =
        theEvent.toJson(schemaVersion, IS_REVISION.negate().and(FieldType.regularOrSpecial())) //
            // remove the policy entries from thing event payload
            .remove(POLICY_IN_THING_EVENT_PAYLOAD);
    final DittoBsonJson dittoBsonJson = DittoBsonJson.getInstance();
    final Object bson = dittoBsonJson.parse(jsonObject);
    final Set<String> readSubjects = calculateReadSubjects(theEvent);
    return new Tagged(bson, readSubjects);
  } else {
    throw new IllegalArgumentException("Unable to toJournal a non-'Event' object! Was: " + event.getClass());
  }
}

代码示例来源: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: 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: eclipse/ditto

private Optional<JsonObject> getInitialPolicyOrCopiedPolicy(final CreateThing createThing) {
  final DittoHeaders dittoHeaders = createThing.getDittoHeaders();
  final Optional<String> policyId = createThing.getPolicyIdOrPlaceholder()
      .flatMap(ReferencePlaceholder::fromCharSequence)
      .map(referencePlaceholder -> {
        log(createThing).debug(
            "CreateThing command contains a reference placeholder for the policy it wants to copy: {}",
            referencePlaceholder);
        return policyIdReferencePlaceholderResolver.resolve(referencePlaceholder, dittoHeaders);
      })
      .map(policyIdCompletionStage -> awaitPolicyIdCompletionStage(policyIdCompletionStage, createThing))
      .map(Optional::of)
      .orElse(createThing.getPolicyIdOrPlaceholder());
  if (policyId.isPresent()) {
    log(dittoHeaders).debug("CreateThing command wants to use a copy of Policy <{}>", policyId.get());
    final Policy policy = retrievePolicyWithEnforcement(policyId.get(), dittoHeaders);
    final JsonObject jsonPolicyWithoutId = policy.toJson(JsonSchemaVersion.V_2).remove("policyId");
    return Optional.of(jsonPolicyWithoutId);
  }
  log(dittoHeaders).debug("CreateThing command did not contain a policy that should be copied.");
  return createThing.getInitialPolicy();
}

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

@Test
public void fromInvalidJsonFails() {
  final JsonObject INVALID_JSON = KNOWN_JSON.remove(Connection.JsonFields.SOURCES.getPointer())
      .remove(Connection.JsonFields.TARGETS.getPointer());
  assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
      .isThrownBy(() -> ImmutableConnection.fromJson(INVALID_JSON))
      .withMessageContaining("source")
      .withMessageContaining("target")
      .withNoCause();
}

相关文章