本文整理汇总了Java中com.fasterxml.jackson.core.JsonParser.readValueAsTree方法的典型用法代码示例。如果您正苦于以下问题:Java JsonParser.readValueAsTree方法的具体用法?Java JsonParser.readValueAsTree怎么用?Java JsonParser.readValueAsTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.core.JsonParser
的用法示例。
在下文中一共展示了JsonParser.readValueAsTree方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _testTokenAccess
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
public void _testTokenAccess(JsonFactory jf, boolean useStream) throws Exception
{
final String DOC = "[ ]";
JsonParser jp = useStream ?
jf.createParser(ObjectReadContext.empty(), new ByteArrayInputStream(DOC.getBytes("UTF-8")))
: jf.createParser(ObjectReadContext.empty(), DOC);
assertNull(jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
assertNull(jp.getEmbeddedObject());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.START_ARRAY, jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
// Also: no codec defined by default
try {
jp.readValueAsTree();
fail("Should get exception without codec");
} catch (UnsupportedOperationException e) {
verifyException(e, "Operation not supported");
}
jp.close();
}
示例2: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public String[] deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
final List<String> list = new ArrayList<>();
final JsonNode node = jsonParser.readValueAsTree();
if (node.isArray()) {
final Iterator elements = node.elements();
while (elements.hasNext()) {
final JsonNode childNode = (JsonNode) elements.next();
final String value = StringUtils.trimToNull(childNode.asText());
if (value != null) {
list.add(value);
}
}
}
if (list.size() == 0) {
return null;
} else {
return list.toArray(new String[list.size()]);
}
}
示例3: createObjectNode
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
/**
* Creates an {@link ObjectNode} based on the given {@code pojo}, copying all its properties to the resulting {@link ObjectNode}.
*
* @param pojo a pojo which properties will be populates into the resulting a {@link ObjectNode}
* @return a {@link ObjectNode} with all the properties from the given pojo
* @throws IOException if the resulting a {@link ObjectNode} can not be created
*/
public static ObjectNode createObjectNode(Object pojo) throws IOException {
if (pojo == null) {
throw new IllegalArgumentException("Pojo can not be null.");
}
ObjectNode objectNode = createObjectNode();
JsonParser jsonParser = mapper.getJsonFactory().createJsonParser(writeValueAsBytes(pojo));
JsonNode jsonNode = jsonParser.readValueAsTree();
if (!jsonNode.isObject()) {
throw new RuntimeException("JsonNode [" + jsonNode + "] is not a object.");
}
objectNode.putAll((ObjectNode) jsonNode);
return objectNode;
}
示例4: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public CommentCollectionResource deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
CommentCollectionResource commentArrayResource = new CommentCollectionResource();
CommentResource commentResource = null;
JsonNode jsonNode = jp.readValueAsTree();
for (JsonNode childNode : jsonNode) {
if (childNode.has(CommentResource.JP_TASKID)) {
commentResource = new CommentResource();
commentResource.setTaskId(childNode.get(CommentResource.JP_TASKID).asText());
commentResource.setComment(childNode.get(CommentResource.JP_COMMENT).asText());
commentResource.setPosted(new Date(childNode.get(CommentResource.JP_POSTED).asLong()));
commentArrayResource.addComment(commentResource);
}
}
return commentArrayResource;
}
示例5: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public CommitMessageDto deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
final JsonNode jsonNode = p.readValueAsTree();
final JsonNode summary = jsonNode.get("summary");
if (summary == null || summary.textValue() == null) {
ctxt.reportInputMismatch(CommitMessageDto.class, "commit message should have a summary.");
// should never reach here
throw new Error();
}
final String detail = jsonNode.get("detail") == null ? "" : jsonNode.get("detail").textValue();
final JsonNode markupNode = jsonNode.get("markup");
final Markup markup = Markup.parse(markupNode == null ? "unknown" : markupNode.textValue());
return new CommitMessageDto(summary.textValue(), detail, markup);
}
示例6: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public Step deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectNode node = jp.readValueAsTree();
JsonNode stepKind = node.get(STEP_KIND);
if (stepKind != null) {
String value = stepKind.textValue();
Class<? extends Step> resourceType = getTypeForName(value);
if (resourceType == null) {
throw ctxt.mappingException("No step type found for kind:" + value);
}
return jp.getCodec().treeToValue(node, resourceType);
}
return null;
}
示例7: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public ErrorData deserialize(JsonParser jp, DeserializationContext context) throws IOException {
JsonNode errorNode = jp.readValueAsTree();
String id = SerializerUtil.readStringIfExists(ErrorDataSerializer.ID, errorNode);
String aboutLink = readAboutLink(errorNode);
String status = SerializerUtil.readStringIfExists(ErrorDataSerializer.STATUS, errorNode);
String code = SerializerUtil.readStringIfExists(ErrorDataSerializer.CODE, errorNode);
String title = SerializerUtil.readStringIfExists(ErrorDataSerializer.TITLE, errorNode);
String detail = SerializerUtil.readStringIfExists(ErrorDataSerializer.DETAIL, errorNode);
Map<String, Object> meta = readMeta(errorNode, jp);
String sourcePointer = readSourcePointer(errorNode);
String sourceParameter = readSourceParameter(errorNode);
return new ErrorData(id, aboutLink, status, code, title, detail, sourcePointer, sourceParameter, meta);
}
示例8: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public JSONOptions deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
JsonLocation l = jp.getTokenLocation();
// logger.debug("Reading tree.");
TreeNode n = jp.readValueAsTree();
// logger.debug("Tree {}", n);
if (n instanceof JsonNode) {
return new JSONOptions( (JsonNode) n, l);
} else {
throw new IllegalArgumentException(String.format("Received something other than a JsonNode %s", n));
}
}
示例9: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public TraceInfoHeaders deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode root = p.readValueAsTree();
Map<String, List<String>> request = parse(root.get("request"));
Map<String, List<String>> response = parse(root.get("response"));
return new TraceInfoHeaders(request, response);
}
示例10: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
JsonNode node = jsonParser.readValueAsTree();
if (node.asText().isEmpty()) {
return null;
}
return node.asText();
}
示例11: validateJson
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
/**
* Parse a JSON object
* @param jp the JSON parse
* @return the JSON node
* @throws IOException
*/
protected JsonNode validateJson(JsonParser jp) throws IOException {
JsonNode parsed = null;
try {
parsed = jp.readValueAsTree();
} catch (JsonProcessingException e) {
System.err.println("Could not parse JSON: " + e.getMessage());
return null;
}
return parsed;
}
示例12: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
final JsonNode node = jsonParser.readValueAsTree();
return StringUtils.trimToNull(node.asText());
}
示例13: deserialize
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
@Override
public Revision deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
final JsonNode node = p.readValueAsTree();
if (node.isNumber()) {
validateRevisionNumber(ctx, node, "major", false);
return new Revision(node.intValue());
}
if (node.isTextual()) {
try {
return new Revision(node.textValue());
} catch (IllegalArgumentException e) {
ctx.reportInputMismatch(Revision.class, e.getMessage());
// Should never reach here.
throw new Error();
}
}
if (!node.isObject()) {
ctx.reportInputMismatch(Revision.class,
"A revision must be a non-zero integer or " +
"an object that contains \"major\" and \"minor\" properties.");
// Should never reach here.
throw new Error();
}
final JsonNode majorNode = node.get("major");
final JsonNode minorNode = node.get("minor");
final int major;
validateRevisionNumber(ctx, majorNode, "major", false);
major = majorNode.intValue();
if (minorNode != null) {
validateRevisionNumber(ctx, minorNode, "minor", true);
if (minorNode.intValue() != 0) {
ctx.reportInputMismatch(Revision.class,
"A revision must not have a non-zero \"minor\" property.");
}
}
return new Revision(major);
}
示例14: readToJsonNode
import com.fasterxml.jackson.core.JsonParser; //导入方法依赖的package包/类
/**
* Decode the bytes to Json object.
* @param in input of bytes
* @param out ouput of Json object list
* @param jrContext context for the last decoding process
* @throws IOException IOException
* @throws JsonParseException JsonParseException
*/
public static void readToJsonNode(ByteBuf in, List<Object> out, JsonReadContext jrContext)
throws JsonParseException, IOException {
int lastReadBytes = jrContext.getLastReadBytes();
if (lastReadBytes == 0) {
if (in.readableBytes() < 4) {
return;
}
checkEncoding(in);
}
int i = lastReadBytes + in.readerIndex();
Stack<Byte> bufStack = jrContext.getBufStack();
for (; i < in.writerIndex(); i++) {
byte b = in.getByte(i);
switch (b) {
case '{':
if (!isDoubleQuote(bufStack)) {
bufStack.push(b);
jrContext.setStartMatch(true);
}
break;
case '}':
if (!isDoubleQuote(bufStack)) {
bufStack.pop();
}
break;
case '"':
if (in.getByte(i - 1) != '\\') {
if (!bufStack.isEmpty() && bufStack.peek() != '"') {
bufStack.push(b);
} else {
bufStack.pop();
}
}
break;
default:
break;
}
if (jrContext.isStartMatch() && bufStack.isEmpty()) {
ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
JsonParser jf = new MappingJsonFactory().createParser(new ByteBufInputStream(buf));
JsonNode jsonNode = jf.readValueAsTree();
out.add(jsonNode);
lastReadBytes = 0;
jrContext.setLastReadBytes(lastReadBytes);
break;
}
}
if (i >= in.writerIndex()) {
lastReadBytes = in.readableBytes();
jrContext.setLastReadBytes(lastReadBytes);
}
}