下面列出了怎么用com.fasterxml.jackson.core.JsonPointer的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void attach(String path, DataModelTree tree) throws WorkflowException {
if (root == null || root instanceof MissingNode) {
throw new WorkflowException("Invalid root node");
}
JsonPointer ptr = JsonPointer.compile(path);
if (!(tree instanceof JsonDataModelTree)) {
throw new WorkflowException("Invalid subTree(" + tree + ")");
}
JsonNode attachingNode = ((JsonDataModelTree) tree).root();
attach(ptr, attachingNode);
}
/**
* Adds a {@link Token} of the specified {@code appId} to the specified {@code projectName}.
*/
public CompletableFuture<Revision> addToken(Author author, String projectName,
String appId, ProjectRole role) {
requireNonNull(author, "author");
requireNonNull(projectName, "projectName");
requireNonNull(appId, "appId");
requireNonNull(role, "role");
return getTokens().thenCompose(tokens -> {
final Token token = tokens.appIds().get(appId);
checkArgument(token != null, "Token not found: " + appId);
final TokenRegistration registration = new TokenRegistration(appId, role,
UserAndTimestamp.of(author));
final JsonPointer path = JsonPointer.compile("/tokens" + encodeSegment(registration.id()));
final Change<JsonNode> change =
Change.ofJsonPatch(METADATA_JSON,
asJsonArray(new TestAbsenceOperation(path),
new AddOperation(path, Jackson.valueToTree(registration))));
final String commitSummary = "Add a token '" + registration.id() +
"' to the project '" + projectName + "' with a role '" + role + '\'';
return metadataRepo.push(projectName, Project.REPO_DOGMA, author, commitSummary, change);
});
}
@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
Model model = doc.getModel();
AbstractNode node = model.find(pointer);
List<AbstractNode> nodes = model.findByType(JsonPointer.compile("/definitions/operation"));
Iterator<AbstractNode> it = nodes.iterator();
AbstractNode found = null;
while (it.hasNext() && found == null) {
AbstractNode current = it.next();
AbstractNode value = current.get("operationId");
if (value != null && Objects.equals(node.asValue().getValue(), value.asValue().getValue())) {
found = value;
}
}
if (found != null) {
IRegion target = doc.getRegion(found.getPointer());
if (target != null) {
return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
}
}
return null;
}
/**
* Adds the specified {@code member} to the {@link ProjectMetadata} of the specified {@code projectName}
* with the specified {@code projectRole}.
*/
public CompletableFuture<Revision> addMember(Author author, String projectName,
User member, ProjectRole projectRole) {
requireNonNull(author, "author");
requireNonNull(projectName, "projectName");
requireNonNull(member, "member");
requireNonNull(projectRole, "projectRole");
final Member newMember = new Member(member, projectRole, UserAndTimestamp.of(author));
final JsonPointer path = JsonPointer.compile("/members" + encodeSegment(newMember.id()));
final Change<JsonNode> change =
Change.ofJsonPatch(METADATA_JSON,
asJsonArray(new TestAbsenceOperation(path),
new AddOperation(path, Jackson.valueToTree(newMember))));
final String commitSummary =
"Add a member '" + newMember.id() + "' to the project '" + projectName + '\'';
return metadataRepo.push(projectName, Project.REPO_DOGMA, author, commitSummary, change);
}
private CompletableFuture<Revision> removeToken(String projectName, Author author, String appId,
boolean quiet) {
final String commitSummary = "Remove the token '" + appId + "' from the project '" + projectName + '\'';
return metadataRepo.push(
projectName, Project.REPO_DOGMA, author, commitSummary,
() -> fetchMetadata(projectName).thenApply(metadataWithRevision -> {
final ImmutableList.Builder<JsonPatchOperation> patches = ImmutableList.builder();
final ProjectMetadata metadata = metadataWithRevision.object();
metadata.repos().values()
.stream().filter(repo -> repo.perTokenPermissions().containsKey(appId))
.forEach(r -> patches.add(
new RemoveOperation(perTokenPermissionPointer(r.name(), appId))));
if (quiet) {
patches.add(new RemoveIfExistsOperation(JsonPointer.compile("/tokens" +
encodeSegment(appId))));
} else {
patches.add(new RemoveOperation(JsonPointer.compile("/tokens" +
encodeSegment(appId))));
}
final Change<JsonNode> change =
Change.ofJsonPatch(METADATA_JSON, Jackson.valueToTree(patches.build()));
return HolderWithRevision.of(change, metadataWithRevision.revision());
})
);
}
DiffProcessor(ReplaceMode replaceMode, final Supplier<Map<JsonPointer, JsonNode>> unchangedValuesSupplier) {
this.replaceMode = replaceMode;
this.unchangedValuesSupplier = new Supplier<Map<JsonPointer, JsonNode>>() {
@Nullable
private Map<JsonPointer, JsonNode> unchangedValues;
@Override
public Map<JsonPointer, JsonNode> get() {
if (unchangedValues == null) {
unchangedValues = unchangedValuesSupplier.get();
}
return unchangedValues;
}
};
}
protected void validateOperationIdReferences(Model model, AbstractNode node, Set<SwaggerError> errors) {
JsonPointer schemaPointer = JsonPointer.compile("/definitions/link/properties/operationId");
if (node != null && node.getType() != null && schemaPointer.equals(node.getType().getPointer())) {
List<AbstractNode> nodes = model.findByType(operationPointer);
Iterator<AbstractNode> it = nodes.iterator();
boolean found = false;
while (it.hasNext() && !found) {
AbstractNode current = it.next();
AbstractNode value = current.get("operationId");
found = value != null && Objects.equals(node.asValue().getValue(), value.asValue().getValue());
}
if (!found) {
errors.add(error(node, IMarker.SEVERITY_ERROR, Messages.error_invalid_operation_id));
}
}
}
void addTestData(Object value) {
JsonPointer ptr = getPointer(null, true, true);
if (!testDataCache.exists(ptr)) {
try {
testDataCache.set(ptr, value);
} catch (CacheException e) {
LOGGER.error("Unable to update test data cache for " + ptr, e);
}
}
}
@Override
public Object get(JsonPointer ptr) throws CacheException {
Object result;
if (root == null) {
result = null;
} else {
try {
JsonNode node = root.at(ptr);
switch (node.getNodeType()) {
case ARRAY:
case OBJECT:
result = node;
break;
case BINARY:
result = node.binaryValue();
break;
case BOOLEAN:
result = node.booleanValue();
break;
case NUMBER:
result = node.numberValue();
break;
case POJO:
result = ((POJONode) node).getPojo();
break;
case STRING:
result = node.textValue();
break;
default:
result = null;
break;
}
} catch (IOException e) {
throw new CacheException(e);
}
}
return result;
}
@Override
public Number getNumber(JsonPointer ptr, Number defaultValue) throws CacheException {
Objects.requireNonNull(defaultValue, "defaultValue is required");
Number result;
if (exists(ptr)) {
result = getNumber(ptr);
} else {
set(ptr, defaultValue);
result = defaultValue;
}
return result;
}
void valueReplaced(final JsonPointer pointer, final JsonNode oldValue, final JsonNode newValue) {
switch (replaceMode) {
case RFC6902:
diffs.add(new ReplaceOperation(pointer, newValue));
break;
case SAFE:
diffs.add(new SafeReplaceOperation(pointer, oldValue, newValue));
break;
}
}
@Override
public BigInteger getBigInteger(JsonPointer ptr, BigInteger defaultValue) throws CacheException {
Objects.requireNonNull(defaultValue, "defaultValue is required");
BigInteger result;
if (exists(ptr)) {
result = getBigInteger(ptr);
} else {
set(ptr, defaultValue);
result = defaultValue;
}
return result;
}
@Override
public double getDouble(JsonPointer ptr, double defaultValue) throws CacheException {
Objects.requireNonNull(defaultValue, "defaultValue is required");
double result;
if (exists(ptr)) {
result = getDouble(ptr);
} else {
set(ptr, defaultValue);
result = defaultValue;
}
return result;
}
public IRegion getRegion(JsonPointer pointer) {
Model model = getModel();
if (model == null) {
return null;
}
AbstractNode node = model.find(pointer);
if (node == null) {
return new Region(0, 0);
}
Position position = node.getPosition(this);
return new Region(position.getOffset(), position.getLength());
}
@Test
public void test() throws URISyntaxException {
JsonReferenceFactory factory = new JsonReferenceFactory();
JsonReference result = factory.doCreate("#/definitions/Foo", null);
assertEquals(new URI(null, null, "/definitions/Foo"), result.getUri());
assertTrue(result.isLocal());
assertFalse(result.isAbsolute());
assertEquals(JsonPointer.compile("/definitions/Foo"), result.getPointer());
}
@Override
public String getString(JsonPointer ptr, String defaultValue) {
Objects.requireNonNull(defaultValue, "defaultValue is required");
String result;
if (exists(ptr)) {
result = getString(ptr);
} else {
set(ptr, defaultValue);
result = defaultValue;
}
return result;
}
@Override
public BigDecimal getBigDecimal(JsonPointer ptr, BigDecimal defaultValue) throws CacheException {
Objects.requireNonNull(defaultValue, "defaultValue is required");
BigDecimal result;
if (exists(ptr)) {
result = getBigDecimal(ptr);
} else {
set(ptr, defaultValue);
result = defaultValue;
}
return result;
}
/**
* Gets json node on specific json pointer as ArrayNode.
*
* @param ptr json pointer
* @return ArrayNode type json node on specific json pointer.
* @throws WorkflowException workflow exception
*/
public ArrayNode arrayAt(JsonPointer ptr) throws WorkflowException {
if (root == null || root instanceof MissingNode) {
throw new WorkflowException("Invalid root node");
}
JsonNode node = root.at(ptr);
if (node instanceof MissingNode) {
return null;
}
if (!(node instanceof ArrayNode)) {
throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
}
return (ArrayNode) node;
}
protected ValueNode deserializeValueNode(JsonParser p, DeserializationContext context, JsonLocation startLocation)
throws IOException {
final Model model = (Model) context.getAttribute(ATTRIBUTE_MODEL);
final AbstractNode parent = (AbstractNode) context.getAttribute(ATTRIBUTE_PARENT);
final JsonPointer ptr = (JsonPointer) context.getAttribute(ATTRIBUTE_POINTER);
Object v = context.readValue(p, Object.class);
ValueNode node = model.valueNode(parent, ptr, v);
node.setStartLocation(createLocation(startLocation));
node.setEndLocation(createLocation(p.getCurrentLocation()));
return node;
}
private JsonSchemaPreProcessor getProcessor(boolean allowDeprecatedProperties) {
return new JsonSchemaPreProcessor(
getSchema("basic.yml"),
JsonPointer.compile("/eventType"),
allowDeprecatedProperties
);
}
@Test
public void test4() throws URISyntaxException {
JsonReferenceFactory factory = new JsonReferenceFactory();
JsonReference result = factory.doCreate("file://path/to/file/doc.yaml#/definitions/Foo", null);
assertEquals(URI.create("file://path/to/file/doc.yaml#/definitions/Foo"), result.getUri());
assertFalse(result.isLocal());
assertTrue(result.isAbsolute());
assertEquals(JsonPointer.compile("/definitions/Foo"), result.getPointer());
}
@Override
public JsonCache add(String path, int value) {
return add(JsonPointer.compile(path), value);
}
@Override
public long getLong(String path, long defaultValue) throws CacheException {
return getLong(JsonPointer.compile(path), defaultValue);
}
@JsonCreator
public RemoveOperation(@JsonProperty("path") final JsonPointer path) {
super("remove", path);
}
private ChildCacheImpl(JsonCache parent, JsonPointer basePtr) {
this.parent = parent;
this.basePtr = basePtr;
}
@Override
public short getShort(JsonPointer ptr) {
return root == null ? (short) 0 : root.at(ptr).shortValue();
}
@Override
public TreeNode at(JsonPointer jp) {
return tree().at(jp);
}
public ReferenceTypeDefinition(JsonSchema schema, JsonPointer pointer, JsonNode definition) {
super(schema, pointer, definition, JsonType.UNDEFINED);
}
@Override
public JsonCache set(String path, long value) throws CacheException {
parent.set(basePtr.append(JsonPointer.compile(path)), value);
return this;
}
@Override
public JsonCache add(String path, BigDecimal value) throws CacheException {
parent.add(basePtr.append(JsonPointer.compile(path)), value);
return this;
}