下面列出了com.fasterxml.jackson.core.JsonPointer#compile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected ArrayNode deserializeArrayNode(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);
ArrayNode node = model.arrayNode(parent, ptr);
int i = 0;
while (p.nextToken() != JsonToken.END_ARRAY) {
JsonPointer pp = JsonPointer.compile(ptr.toString() + "/" + i);
context.setAttribute(ATTRIBUTE_PARENT, node);
context.setAttribute(ATTRIBUTE_POINTER, pp);
AbstractNode v = deserialize(p, context);
node.add(v);
i++;
}
node.setStartLocation(createLocation(startLocation));
node.setEndLocation(createLocation(p.getCurrentLocation()));
return node;
}
public void testJobNoScope() throws Exception {
int testResourceSet = TEST_RESOURCES_A;
prepareTestResources(testResourceSet);
try {
String endpoint = "_zentity/resolution/zentity_test_entity_a";
Request postResolution = new Request("POST", endpoint);
postResolution.setEntity(TEST_PAYLOAD_JOB_NO_SCOPE);
Response response = client.performRequest(postResolution);
JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
assertEquals(json.get("hits").get("total").asInt(), 40);
JsonPointer pathAttributes = JsonPointer.compile("/_attributes");
JsonPointer pathNull = JsonPointer.compile("/_attributes/attribute_type_string_null");
JsonPointer pathUnused = JsonPointer.compile("/_attributes/attribute_type_string_unused");
for (JsonNode doc : json.get("hits").get("hits")) {
assertEquals(doc.at(pathAttributes).isMissingNode(), false);
assertEquals(doc.at(pathNull).isMissingNode(), true);
assertEquals(doc.at(pathUnused).isMissingNode(), true);
}
} finally {
destroyTestResources(testResourceSet);
}
}
/**
* 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);
}
protected void validateParameters(Model model, AbstractNode node, Set<SwaggerError> errors) {
final JsonPointer pointer = JsonPointer.compile("/definitions/parameterOrReference");
if (node != null && node.getType() != null && pointer.equals(node.getType().getPointer())) {
// validation parameter location value
if (node.isObject() && node.asObject().get("in") != null) {
AbstractNode valueNode = node.asObject().get("in");
try {
Object value = valueNode.asValue().getValue();
if (!Arrays.asList("query", "header", "path", "cookie").contains(value)) {
errors.add(error(valueNode, IMarker.SEVERITY_ERROR, Messages.error_invalid_parameter_location));
}
} catch (Exception e) {
errors.add(error(valueNode, IMarker.SEVERITY_ERROR, Messages.error_invalid_parameter_location));
}
}
}
}
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));
}
}
}
public void remove(JsonNode jsonNode, String path) {
JsonPointer valueNodePointer = JsonPointer.compile(asJsonPath(path));
JsonPointer parentPointer = valueNodePointer.head();
JsonNode parentNode = jsonNode.at(parentPointer);
if (parentNode.getNodeType() == JsonNodeType.OBJECT) {
ObjectNode parentNodeToUpdate = (ObjectNode) parentNode;
parentNodeToUpdate.remove(StringUtils.replace(valueNodePointer.last().toString(),"/",""));
}
}
private JsonPointer getPointer(String suffix, boolean includeIndexes, boolean includeLastIndex) {
StringBuilder path = new StringBuilder();
appendPath(path, includeIndexes);
if (includeIndexes && !includeLastIndex && isIndexed())
path.setLength(path.lastIndexOf("/"));
if (suffix != null)
path.append('/').append(suffix);
return JsonPointer.compile(path.toString());
}
@Test
public void testSetWithMissingAncestors() throws Exception {
JsonPointer ptr = JsonPointer.compile("/nonExistentArray/0/nonExistentObject/stringProperty");
assertFalse(cache.exists(ptr), "exists(ptr) returned incorrect value;");
Object value = cache.get(ptr);
assertNull(value, "stringProperty is non-null;");
cache.set(ptr, "string value");
assertTrue(cache.exists(ptr), "exists(ptr) returned incorrect value;");
value = cache.get(ptr);
assertEquals("string value", value, "stringProperty is null after being set;");
}
private JsonSchemaPreProcessor getProcessor(boolean allowDeprecatedProperties) {
return new JsonSchemaPreProcessor(
getSchema("basic.yml"),
JsonPointer.compile("/eventType"),
allowDeprecatedProperties
);
}
public static JsonPointer pointer(String href) {
if (href.startsWith("#")) {
return JsonPointer.compile(href.substring(1));
} else if (href.startsWith("/")) {
return JsonPointer.compile(href);
} else {
String[] split = href.split("#");
return split.length > 1 ? JsonPointer.compile(split[1]) : null;
}
}
/**
* Adds a {@link RepositoryMetadata} of the specified {@code repoName} to the specified {@code projectName}
* with the specified {@link PerRolePermissions}.
*/
public CompletableFuture<Revision> addRepo(Author author, String projectName, String repoName,
PerRolePermissions permission) {
requireNonNull(author, "author");
requireNonNull(projectName, "projectName");
requireNonNull(repoName, "repoName");
requireNonNull(permission, "permission");
final JsonPointer path = JsonPointer.compile("/repos" + encodeSegment(repoName));
final RepositoryMetadata newRepositoryMetadata = new RepositoryMetadata(repoName,
UserAndTimestamp.of(author),
permission);
final Change<JsonNode> change =
Change.ofJsonPatch(METADATA_JSON,
asJsonArray(new TestAbsenceOperation(path),
new AddOperation(path,
Jackson.valueToTree(newRepositoryMetadata))));
final String commitSummary =
"Add a repo '" + newRepositoryMetadata.id() + "' to the project '" + projectName + '\'';
return metadataRepo.push(projectName, Project.REPO_DOGMA, author, commitSummary, change)
.handle((revision, cause) -> {
if (cause != null) {
if (Exceptions.peel(cause) instanceof ChangeConflictException) {
throw new RepositoryExistsException(repoName);
} else {
return Exceptions.throwUnsafely(cause);
}
}
return revision;
});
}
@Override
public JsonDataModelTree alloc(String path, Nodetype leaftype) throws WorkflowException {
if (root == null || root instanceof MissingNode) {
throw new WorkflowException("Invalid root node");
}
JsonPointer ptr = JsonPointer.compile(path);
return alloc(ptr, leaftype);
}
private static JsonPointer createPointer(String text) {
if (StringUtils.emptyToNull(text) == null) {
return JsonPointer.compile("");
}
if (text.startsWith("#")) {
text = text.substring(1);
}
return JsonPointer.compile(text);
}
/**
* Generates the path of {@link JsonPointer} of permission of the specified {@code memberId} in the
* specified {@code repoName}.
*/
private static JsonPointer perUserPermissionPointer(String repoName, String memberId) {
return JsonPointer.compile("/repos" + encodeSegment(repoName) +
"/perUserPermissions" + encodeSegment(memberId));
}
public JsonPointerBasedInboundEventTenantDetector(String jsonPointerExpression) {
this.jsonPointerExpression = jsonPointerExpression;
this.jsonPointer = JsonPointer.compile(jsonPointerExpression);
}
/**
* Gets json node on specific path.
*
* @param path path of json node
* @return json node on specific path
* @throws WorkflowException workflow exception
*/
public JsonNode nodeAt(String path) throws WorkflowException {
JsonPointer ptr = JsonPointer.compile(path);
return nodeAt(ptr);
}
/**
* Sets boolean on specific json path.
*
* @param path json path
* @param isTrue boolean to set
* @throws WorkflowException workflow exception
*/
public void setAt(String path, Boolean isTrue) throws WorkflowException {
JsonPointer ptr = JsonPointer.compile(path);
setAt(ptr, isTrue);
}
/**
* Sets boolean on specific json path.
*
* @param path json path
* @param jsonNode jsonNode to set
* @throws WorkflowException workflow exception
*/
public void setAt(String path, JsonNode jsonNode) throws WorkflowException {
JsonPointer ptr = JsonPointer.compile(path);
setAt(ptr, jsonNode);
}
/**
* Gets integer node on specific path.
*
* @param path path of json node
* @return integer on specific path
* @throws WorkflowException workflow exception
*/
public Integer intAt(String path) throws WorkflowException {
JsonPointer ptr = JsonPointer.compile(path);
return intAt(ptr);
}
/**
* Gets json node on specific path as ObjectNode.
*
* @param path path of json node
* @return ObjectNode type json node on specific path
* @throws WorkflowException workflow exception
*/
public ObjectNode objectAt(String path) throws WorkflowException {
JsonPointer ptr = JsonPointer.compile(path);
return objectAt(ptr);
}