本文整理汇总了Java中com.jayway.jsonpath.DocumentContext.jsonString方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentContext.jsonString方法的具体用法?Java DocumentContext.jsonString怎么用?Java DocumentContext.jsonString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jayway.jsonpath.DocumentContext
的用法示例。
在下文中一共展示了DocumentContext.jsonString方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: getAsString
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public String getAsString() {
switch (type) {
case NULL:
return null;
case JSON:
DocumentContext doc = getValue(DocumentContext.class);
return doc.jsonString();
case XML:
Node node = getValue(Node.class);
if (node.getTextContent() != null) { // for attributes, text() etc
return node.getTextContent();
} else {
return XmlUtils.toString(node);
}
case JS_ARRAY:
case LIST:
List list = getAsList();
DocumentContext listDoc = JsonPath.parse(list);
return listDoc.jsonString();
case JS_OBJECT:
case MAP:
Map map = getAsMap();
DocumentContext mapDoc = JsonPath.parse(map);
return mapDoc.jsonString();
case JS_FUNCTION:
return value.toString().replace("\n", " ");
case BYTE_ARRAY:
return FileUtils.toString(getValue(byte[].class));
case INPUT_STREAM:
return FileUtils.toString(getValue(InputStream.class));
default:
return value.toString();
}
}
示例3: testEmptyJsonArray
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testEmptyJsonArray() {
DocumentContext doc = JsonUtils.emptyJsonArray(0);
String json = doc.jsonString();
assertEquals("[]", json);
doc = JsonUtils.emptyJsonArray(1);
json = doc.jsonString();
assertEquals("[{}]", json);
doc = JsonUtils.emptyJsonArray(2);
json = doc.jsonString();
assertEquals("[{},{}]", json);
}
示例4: testEvalEmbeddedExpressionsWithJsonPath
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testEvalEmbeddedExpressionsWithJsonPath() {
ScriptContext ctx = getContext();
String ticket = "{ ticket: 'my-ticket', userId: '12345' }";
ctx.vars.put("ticket", JsonUtils.toJsonDoc(ticket));
String json = "{ foo: '#(ticket.userId)' }";
DocumentContext doc = JsonUtils.toJsonDoc(json);
Script.evalJsonEmbeddedExpressions(doc, ctx, false);
String result = doc.jsonString();
logger.debug("result: {}", result);
assertEquals("{\"foo\":\"12345\"}", 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: toStrictJsonString
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
public static String toStrictJsonString(String raw) {
DocumentContext dc = toJsonDoc(raw);
return dc.jsonString();
}
示例7: testEmptyJsonObject
import com.jayway.jsonpath.DocumentContext; //导入方法依赖的package包/类
@Test
public void testEmptyJsonObject() {
DocumentContext doc = JsonUtils.emptyJsonObject();
String json = doc.jsonString();
assertEquals("{}", json);
}