/** * Method for constructing a {@link JsonParser} out of JSON tree * representation. * * @param n Root node of the tree that resulting parser will read from */ @Override public JsonParser treeAsTokens(TreeNode n) { return new TreeTraversingParser((JsonNode) n, this); }
@Override public int getTextLength() throws IOException, JsonParseException { return getText().length(); }
protected JsonNode currentNumericNode() throws JsonParseException { JsonNode n = currentNode(); if (n == null || !n.isNumber()) { JsonToken t = (n == null) ? null : n.asToken(); throw _constructError("Current token ("+t+") not numeric, cannot use numeric value accessors"); } return n; }
@Override public String getText() { if (_closed) { return null; } // need to separate handling a bit... switch (_currToken) { case FIELD_NAME: return _nodeCursor.getCurrentName(); case VALUE_STRING: return currentNode().textValue(); case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: return String.valueOf(currentNode().numberValue()); case VALUE_EMBEDDED_OBJECT: JsonNode n = currentNode(); if (n != null && n.isBinary()) { // this will convert it to base64 return n.asText(); } default: return (_currToken == null) ? null : _currToken.asString(); } }
@Override public int getIntValue() throws IOException, JsonParseException { return currentNumericNode().intValue(); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException { // Multiple possibilities... JsonNode n = currentNode(); if (n != null) { // binary node? byte[] data = n.binaryValue(); // (or TextNode, which can also convert automatically!) if (data != null) { return data; } // Or maybe byte[] as POJO? if (n.isPojo()) { Object ob = ((POJONode) n).getPojo(); if (ob instanceof byte[]) { return (byte[]) ob; } } } // otherwise return null to mark we have no binary content return null; }
@Override public double getDoubleValue() throws IOException, JsonParseException { return currentNumericNode().doubleValue(); }
@Override public Object getEmbeddedObject() { if (!_closed) { JsonNode n = currentNode(); if (n != null) { if (n.isPojo()) { return ((POJONode) n).getPojo(); } if (n.isBinary()) { return ((BinaryNode) n).binaryValue(); } } } return null; }
@Override public long getLongValue() throws IOException, JsonParseException { return currentNumericNode().longValue(); }
@Override public BigInteger getBigIntegerValue() throws IOException, JsonParseException { return currentNumericNode().bigIntegerValue(); }
@Override public BigDecimal getDecimalValue() throws IOException, JsonParseException { return currentNumericNode().decimalValue(); }
@Override public Number getNumberValue() throws IOException, JsonParseException { return currentNumericNode().numberValue(); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException { // Multiple possibilities... JsonNode n = currentNode(); if (n != null) { // [databind#2096]: although `binaryValue()` works for real binary node // and embedded "POJO" node, coercion from TextNode may require variant, so: if (n instanceof TextNode) { return ((TextNode) n).getBinaryValue(b64variant); } return n.binaryValue(); } // otherwise return null to mark we have no binary content return null; }
@Override public NumberType getNumberType() throws IOException, JsonParseException { JsonNode n = currentNumericNode(); return (n == null) ? null : n.numberType(); }
public <O> O decodeBase64(String pointer, Class<O> type, ObjectMapper objectMapper) { byte[] data; try { TreeTraversingParser parser = new TreeTraversingParser(getPointer(pointer != null ? pointer : "", contextToNode()), objectMapper); parser.nextToken(); data = Base64.getDecoder().decode(parser.getText()); } catch (IOException e) { throw new IllegalArgumentException("Value in stage context at pointer " + pointer + " is not base 64 encoded", e); } try { return objectMapper.readValue(data, type); } catch (IOException e) { throw new RuntimeException("Could not convert " + new String(data, UTF_8) + " to " + type.getSimpleName(), e); } }
@Override protected void _handleEOF() throws JsonParseException { _throwInternal(); // should never get called } }
@Override public boolean isNaN() { if (!_closed) { JsonNode n = currentNode(); if (n instanceof NumericNode) { return ((NumericNode) n).isNaN(); } } return false; }
@Override public String getText() { if (_closed) { return null; } // need to separate handling a bit... switch (_currToken) { case FIELD_NAME: return _nodeCursor.getCurrentName(); case VALUE_STRING: return currentNode().textValue(); case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: return String.valueOf(currentNode().numberValue()); case VALUE_EMBEDDED_OBJECT: JsonNode n = currentNode(); if (n != null && n.isBinary()) { // this will convert it to base64 return n.asText(); } } return (_currToken == null) ? null : _currToken.asString(); }
@Override public int getIntValue() throws IOException, JsonParseException { return currentNumericNode().intValue(); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException { // Multiple possibilities... JsonNode n = currentNode(); if (n != null) { // binary node? byte[] data = n.binaryValue(); // (or TextNode, which can also convert automatically!) if (data != null) { return data; } // Or maybe byte[] as POJO? if (n.isPojo()) { Object ob = ((POJONode) n).getPojo(); if (ob instanceof byte[]) { return (byte[]) ob; } } } // otherwise return null to mark we have no binary content return null; }