本文整理汇总了Java中com.jayway.jsonpath.DocumentContext类的典型用法代码示例。如果您正苦于以下问题:Java DocumentContext类的具体用法?Java DocumentContext怎么用?Java DocumentContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DocumentContext类属于com.jayway.jsonpath包,在下文中一共展示了DocumentContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getKvEntries
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
protected List<KvEntry> getKvEntries(DocumentContext document, List<? extends TransformerKVMapping> mappings) {
List<KvEntry> result = new ArrayList<>();
if (mappings != null) {
for (TransformerKVMapping mapping : mappings) {
String key = eval(document, mapping.getKey());
String strVal = eval(document, mapping.getValue());
DataValueTransformer transformer = mapping.getTransformer();
if (transformer != null && transformer.isApplicable(strVal)) {
result.add(getKvEntry(mapping, key, strVal, transformer));
} else if (transformer == null) {
result.add(getKvEntry(mapping, key, strVal));
}
}
}
return result;
}
示例2: parse
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
private List<DeviceData> parse(String topic, List<String> srcList) {
List<DeviceData> result = new ArrayList<>(srcList.size());
for (String src : srcList) {
DocumentContext document = JsonPath.parse(src);
long ts = System.currentTimeMillis();
String deviceName;
if (!StringUtils.isEmpty(deviceNameTopicExpression)) {
deviceName = eval(topic);
} else {
deviceName = eval(document, deviceNameJsonExpression);
}
if (!StringUtils.isEmpty(deviceName)) {
List<KvEntry> attrData = getKvEntries(document, attributes);
List<TsKvEntry> tsData = getKvEntries(document, timeseries).stream().map(kv -> new BasicTsKvEntry(ts, kv))
.collect(Collectors.toList());
result.add(new DeviceData(deviceName, attrData, tsData, timeout));
}
}
return result;
}
示例3: getKvEntries
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
private List<KvEntry> getKvEntries(DocumentContext document, List<? extends KVMapping> mappings) {
List<KvEntry> result = new ArrayList<>();
if (mappings != null) {
for (KVMapping mapping : mappings) {
String key = eval(document, mapping.getKey());
String strVal = eval(document, mapping.getValue());
switch (mapping.getType().getDataType()) {
case STRING:
result.add(new StringDataEntry(key, strVal));
break;
case BOOLEAN:
result.add(new BooleanDataEntry(key, Boolean.valueOf(strVal)));
break;
case DOUBLE:
result.add(new DoubleDataEntry(key, Double.valueOf(strVal)));
break;
case LONG:
result.add(new LongDataEntry(key, Long.valueOf(strVal)));
break;
}
}
}
return result;
}
示例4: getTsKvEntries
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
private List<TsKvEntry> getTsKvEntries(DocumentContext document, List<? extends TimeseriesMapping> mappings, long defaultTs) throws ParseException {
List<TsKvEntry> result = new ArrayList<>();
if (mappings != null) {
for (TransformerKVMapping mapping : mappings) {
String key = eval(document, mapping.getKey());
String strVal = eval(document, mapping.getValue());
long ts = defaultTs;
if (!StringUtils.isEmpty(mapping.getTs())) {
String tsVal = eval(document, mapping.getTs());
if (!StringUtils.isEmpty(mapping.getTsFormat())) {
SimpleDateFormat formatter = formatters.computeIfAbsent(mapping.getTsFormat(), SimpleDateFormat::new);
ts = formatter.parse(tsVal).getTime();
} else {
ts = Long.parseLong(tsVal);
}
}
DataValueTransformer transformer = mapping.getTransformer();
if (transformer != null && transformer.isApplicable(strVal)) {
result.add(new BasicTsKvEntry(ts, getKvEntry(mapping, key, strVal, transformer)));
} else if (transformer == null) {
result.add(new BasicTsKvEntry(ts, getKvEntry(mapping, key, strVal)));
}
}
}
return result;
}
示例5: convert
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
@Override
public List<DeviceData> convert(String topic, MqttMessage msg) throws Exception {
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
log.trace("Parsing json message: {}", data);
if (!filterExpression.isEmpty()) {
try {
log.debug("Data before filtering {}", data);
DocumentContext document = JsonPath.parse(data);
document = JsonPath.parse((Object) document.read(filterExpression));
data = document.jsonString();
log.debug("Data after filtering {}", data);
} catch (RuntimeException e) {
log.debug("Failed to apply filter expression: {}", filterExpression);
throw new RuntimeException("Failed to apply filter expression " + filterExpression);
}
}
JsonNode node = mapper.readTree(data);
List<String> srcList;
if (node.isArray()) {
srcList = new ArrayList<>(node.size());
for (int i = 0; i < node.size(); i++) {
srcList.add(mapper.writeValueAsString(node.get(i)));
}
} else {
srcList = Collections.singletonList(data);
}
return parse(topic, srcList);
}
示例6: parseDeviceData
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
protected DeviceData parseDeviceData(DocumentContext document) throws ParseException {
long ts = System.currentTimeMillis();
String deviceName = eval(document, deviceNameJsonExpression);
String deviceType = null;
if (!StringUtils.isEmpty(deviceTypeJsonExpression)) {
deviceType = eval(document, deviceTypeJsonExpression);
}
if (!StringUtils.isEmpty(deviceName)) {
List<KvEntry> attrData = getKvEntries(document, attributes);
List<TsKvEntry> tsData = getTsKvEntries(document, timeseries, ts);
return new DeviceData(deviceName, deviceType, attrData, tsData);
} else {
return null;
}
}
示例7: testNavDate
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
@Test
public void testNavDate() throws IOException {
OffsetDateTime navDate = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
Manifest manifest = new Manifest("http://some.uri", "A label for the Manifest");
manifest.setNavDate(navDate);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(manifest);
DocumentContext ctx = JsonPath.parse(json);
JsonPathAssert.assertThat(ctx).jsonPathAsString("navDate").isEqualTo("1970-01-01T00:00:00Z");
assertThat(mapper.readValue(json, Manifest.class).getNavDate()).isEqualTo(manifest.getNavDate());
Collection coll = new Collection("http://some.uri", "A label for the Collection");
coll.setNavDate(navDate);
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(coll);
ctx = JsonPath.parse(json);
JsonPathAssert.assertThat(ctx).jsonPathAsString("navDate").isEqualTo("1970-01-01T00:00:00Z");
assertThat(mapper.readValue(json, Collection.class).getNavDate()).isEqualTo(navDate);
}
示例8: convert
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
public AttributeRequest convert(String topic, MqttMessage msg) {
deviceNameTopicPattern = checkAndCompile(deviceNameTopicPattern, deviceNameTopicExpression);
attributeKeyTopicPattern = checkAndCompile(attributeKeyTopicPattern, attributeKeyTopicExpression);
requestIdTopicPattern = checkAndCompile(requestIdTopicPattern, requestIdTopicExpression);
String data = new String(msg.getPayload(), StandardCharsets.UTF_8);
DocumentContext document = JsonPath.parse(data);
AttributeRequest.AttributeRequestBuilder builder = AttributeRequest.builder();
builder.deviceName(eval(topic, deviceNameTopicPattern, document, deviceNameJsonExpression));
builder.attributeKey(eval(topic, attributeKeyTopicPattern, document, attributeKeyJsonExpression));
builder.requestId(Integer.parseInt(eval(topic, requestIdTopicPattern, document, requestIdJsonExpression)));
builder.clientScope(this.isClientScope());
builder.topicExpression(this.getResponseTopicExpression());
builder.valueExpression(this.getValueExpression());
return builder.build();
}
示例9: validate
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
@Override
public ValidationResult validate(ScriptValue value) {
switch(value.getType()) {
case JSON:
DocumentContext doc = value.getValue(DocumentContext.class);
if (!doc.jsonString().startsWith("{")) {
return ValidationResult.fail("not a json object");
} else {
return ValidationResult.PASS;
}
case JS_OBJECT:
case MAP:
return ValidationResult.PASS;
default:
return ValidationResult.fail("not a json object");
}
}
示例10: getEntityInternal
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
private T getEntityInternal(ScriptValue body, String mediaType) {
if (body.isJsonLike()) {
if (mediaType == null) {
mediaType = APPLICATION_JSON;
}
DocumentContext json = body.getAsJsonDocument();
return getEntity(json.jsonString(), mediaType);
} else if (body.isXml()) {
Node node = body.getValue(Node.class);
if (mediaType == null) {
mediaType = APPLICATION_XML;
}
return getEntity(XmlUtils.toString(node), mediaType);
} else if (body.isStream()) {
InputStream is = body.getValue(InputStream.class);
if (mediaType == null) {
mediaType = APPLICATION_OCTET_STREAM;
}
return getEntity(is, mediaType);
} else {
if (mediaType == null) {
mediaType = TEXT_PLAIN;
}
return getEntity(body.getAsString(), mediaType);
}
}
示例11: toJsonDoc
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
public static DocumentContext toJsonDoc(ScriptValue sv, ScriptContext context) {
if (sv.getType() == JSON) { // optimize
return (DocumentContext) sv.getValue();
} else if (sv.isListLike()) {
return JsonPath.parse(sv.getAsList());
} else if (sv.isMapLike()) {
return JsonPath.parse(sv.getAsMap());
} else if (sv.isUnknownType()) { // POJO
return JsonUtils.toJsonDoc(sv.getValue());
} else if (sv.isStringOrStream()) {
ScriptValue temp = evalKarateExpression(sv.getAsString(), context);
if (temp.getType() != JSON) {
throw new RuntimeException("cannot convert, not a json string: " + sv);
}
return temp.getValue(DocumentContext.class);
} else {
throw new RuntimeException("cannot convert to json: " + sv);
}
}
示例12: getKvEntries
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
private List<KvEntry> getKvEntries(DocumentContext document, List<? extends KVMapping> mappings) {
List<KvEntry> result = new ArrayList<>();
if (mappings != null) {
for (KVMapping mapping : mappings) {
String key = eval(document, mapping.getKey());
String strVal = eval(document, mapping.getValue());
switch (mapping.getType().getDataType()) {
case STRING:
result.add(new StringDataEntry(key, strVal));
break;
case BOOLEAN:
result.add(new BooleanDataEntry(key, Boolean.valueOf(strVal)));
break;
case DOUBLE:
result.add(new DoubleDataEntry(key, Double.valueOf(strVal)));
break;
case LONG:
result.add(new LongDataEntry(key, Long.valueOf(strVal)));
break;
}
}
}
return result;
}
示例13: 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 );
}
示例14: getAsJsonDocument
import com.jayway.jsonpath.DocumentContext; //导入依赖的package包/类
public DocumentContext getAsJsonDocument() {
switch (type) {
case JSON:
return getValue(DocumentContext.class);
case JS_ARRAY: // happens for json resulting from nashorn
ScriptObjectMirror som = getValue(ScriptObjectMirror.class);
return JsonPath.parse(som.values());
case JS_OBJECT: // is a map-like object, happens for json resulting from nashorn
case MAP: // this happens because some jsonpath operations result in Map
Map<String, Object> map = getValue(Map.class);
return JsonPath.parse(map);
case LIST: // this also happens because some jsonpath operations result in List
List list = getValue(List.class);
return JsonPath.parse(list);
default:
throw new RuntimeException("cannot convert to json: " + this);
}
}
示例15: 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;
}