本文整理汇总了Java中com.jayway.jsonpath.DocumentContext.read方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentContext.read方法的具体用法?Java DocumentContext.read怎么用?Java DocumentContext.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jayway.jsonpath.DocumentContext
的用法示例。
在下文中一共展示了DocumentContext.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAllPullRequests_should_return_all_pull_requests_as_list
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void getAllPullRequests_should_return_all_pull_requests_as_list() throws Exception {
final Repository repo = mock( Repository.class );
final String json = new String( Files.readAllBytes(
Paths.get( "src/test/resources/org/retest/rebazer/service/bitbucketservicetest/response.json" ) ) );
final DocumentContext documentContext = JsonPath.parse( json );
when( config.getTeam() ).thenReturn( "test_team" );
when( repo.getName() ).thenReturn( "test_repo_name" );
when( bitbucketTemplate.getForObject( anyString(), eq( String.class ) ) ).thenReturn( json );
final int expectedId = (int) documentContext.read( "$.values[0].id" );
final String expectedUrl =
"/repositories/" + config.getTeam() + "/" + repo.getName() + "/pullrequests/" + expectedId;
final List<PullRequest> expected = Arrays.asList( PullRequest.builder().id( expectedId ).repo( repo.getName() )
.source( documentContext.read( "$.values[0].source.branch.name" ) )
.destination( documentContext.read( "$.values[0].destination.branch.name" ) ).url( expectedUrl )
.lastUpdate( documentContext.read( "$.values[0].updated_on" ) ).build() );
final List<PullRequest> actual = cut.getAllPullRequests( repo );
assertThat( actual ).isEqualTo( expected );
}
示例2: getLastCommonCommitId
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
String getLastCommonCommitId( final PullRequest pullRequest ) {
DocumentContext jp = jsonPathForPath( pullRequest.getUrl() + "/commits" );
final int pageLength = jp.read( "$.pagelen" );
final int size = jp.read( "$.size" );
final int lastPage = (pageLength + size - 1) / pageLength;
if ( lastPage > 1 ) {
jp = jsonPathForPath( pullRequest.getUrl() + "/commits?page=" + lastPage );
}
final List<String> commitIds = jp.read( "$.values[*].hash" );
final List<String> parentIds = jp.read( "$.values[*].parents[0].hash" );
return parentIds.stream().filter( parent -> !commitIds.contains( parent ) ).findFirst()
.orElseThrow( IllegalStateException::new );
}
示例3: parsePullRequestsJson
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
private static List<PullRequest> parsePullRequestsJson( final Repository repo, final String urlPath,
final DocumentContext jp ) {
final int numPullRequests = (int) jp.read( "$.size" );
final List<PullRequest> results = new ArrayList<>( numPullRequests );
for ( int i = 0; i < numPullRequests; i++ ) {
final int id = jp.read( "$.values[" + i + "].id" );
final String source = jp.read( "$.values[" + i + "].source.branch.name" );
final String destination = jp.read( "$.values[" + i + "].destination.branch.name" );
final String lastUpdate = jp.read( "$.values[" + i + "].updated_on" );
results.add( PullRequest.builder() //
.id( id ) //
.repo( repo.getName() ) //
.source( source ) //
.destination( destination ) //
.url( urlPath + "/" + id ) //
.lastUpdate( lastUpdate ) //
.build() ); //
}
return results;
}
示例4: apply
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
protected static <T> T apply(DocumentContext document, String expression) {
try {
return document.read(expression);
} catch (RuntimeException e) {
log.debug("Failed to apply expression: {}", expression, e);
throw new RuntimeException("Failed to apply expression " + expression);
}
}
示例5: apply
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
protected static <T> T apply(DocumentContext document, String expression) {
try {
return document.read(expression);
} catch (RuntimeException e) {
log.debug("Failed to apply expression: {}", expression, e);
throw new RuntimeException("Failed to apply expression " + expression);
}
}
示例6: readPostmanJson
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public static List<PostmanRequest> readPostmanJson(String json) {
DocumentContext doc = JsonPath.parse(json);
List<Map<String, Object>> list = (List) doc.read("$.item");
List<PostmanRequest> requests = new ArrayList<>(list.size());
for (Map<String, Object> map : list) {
logger.debug("map: {}", map);
String name = (String) map.get("name");
Map<String, Object> requestInfo = (Map) map.get("request");
String url = (String) requestInfo.get("url");
String method = (String) requestInfo.get("method");
List<Map<String, Object>> headersList = (List) requestInfo.get("header");
Map<String, String> headers = new HashMap<>();
for (Map<String, Object> header : headersList) {
headers.put((String) header.get("key"), (String) header.get("value"));
}
Map<String, Object> bodyInfo = (Map) requestInfo.get("body");
String body = null;
if (bodyInfo.containsKey("raw")) {
body = ((String) bodyInfo.get("raw")).replace(System.lineSeparator(), "");
}
else if (bodyInfo.containsKey("formdata")) {
body = ((List) bodyInfo.get("formdata")).toString().replace(System.lineSeparator(), "");
}
PostmanRequest request = new PostmanRequest();
request.setName(name);
request.setUrl(url);
request.setMethod(method);
request.setHeaders(headers);
request.setBody(body);
requests.add(request);
}
return requests;
}
示例7: evalJsonPathOnVarByName
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public static ScriptValue evalJsonPathOnVarByName(String name, String exp, ScriptContext context) {
ScriptValue value = getValuebyName(name, context);
if (value.isJsonLike()) {
DocumentContext jsonDoc = value.getAsJsonDocument();
return new ScriptValue(jsonDoc.read(exp));
} else if (value.isXml()) {
Document xml = value.getValue(Document.class);
DocumentContext xmlDoc = XmlUtils.toJsonDoc(xml);
return new ScriptValue(xmlDoc.read(exp));
} else {
String str = value.getAsString();
DocumentContext strDoc = JsonPath.parse(str);
return new ScriptValue(strDoc.read(exp));
}
}
示例8: toPrettyJsonString
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public static String toPrettyJsonString(DocumentContext doc) {
Object o = doc.read("$");
StringBuilder sb = new StringBuilder();
// anti recursion / back-references
Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap());
recursePretty(o, sb, 0, seen);
sb.append('\n');
return sb.toString();
}
示例9: pathExists
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public static boolean pathExists(DocumentContext doc, String path) {
try {
return doc.read(path) != null;
} catch (PathNotFoundException pnfe) {
return false;
}
}
示例10: getAsMap
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public Map<String, Object> getAsMap() {
switch (type) {
case MAP:
case JS_OBJECT:
return getValue(Map.class);
case JSON:
DocumentContext json = getValue(DocumentContext.class);
return json.read("$");
case XML:
Node xml = getValue(Node.class);
return (Map) XmlUtils.toObject(xml);
default:
throw new RuntimeException("cannot convert to map: " + this);
}
}
示例11: getAfterConvertingFromJsonOrXmlIfNeeded
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public Object getAfterConvertingFromJsonOrXmlIfNeeded() {
switch (type) {
case JSON:
DocumentContext json = getValue(DocumentContext.class);
return json.read("$");
case XML:
Node xml = getValue(Node.class);
return XmlUtils.toObject(xml);
default:
return getValue();
}
}
示例12: testTypeDetection
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testTypeDetection() {
DocumentContext doc = JsonPath.parse("{ foo: 'bar' }");
ScriptValue sv = new ScriptValue(doc);
assertEquals(JSON, sv.getType());
doc = JsonPath.parse("[1, 2]");
sv = new ScriptValue(doc);
assertEquals(JSON, sv.getType());
Object temp = doc.read("$");
assertTrue(temp instanceof List);
}
示例13: testNonStrictJsonParsing
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testNonStrictJsonParsing() {
String raw = "{ foo: 'bar' }";
DocumentContext dc = JsonUtils.toJsonDoc(raw);
logger.debug("parsed json: {}", dc.jsonString());
String value = dc.read("$.foo");
assertEquals("bar", value);
}
示例14: testJsonArrayAsRoot
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testJsonArrayAsRoot() {
String raw = "[1, 2, 3]";
DocumentContext doc = JsonUtils.toJsonDoc(raw);
Object sec = doc.read("$[1]");
assertEquals(2, sec);
}
示例15: testJsonChunkByPath
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testJsonChunkByPath() {
String raw = "[{ foo: 'bar' }]";
DocumentContext doc = JsonUtils.toJsonDoc(raw);
Map map = doc.read("$[0]");
DocumentContext foo = JsonPath.parse(map);
assertEquals("{\"foo\":\"bar\"}", foo.jsonString());
}