/** * Method for constructing a {@link JsonParser} out of JSON tree * representation. * * @param n Root node of the tree that resulting parser will read from * * @since 1.3 */ @Override public JsonParser treeAsTokens(JsonNode n) { return new TreeTraversingParser(n, this); }
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, can not use numeric value accessors"); } return n; }
@Override protected void _handleEOF() throws JsonParseException { _throwInternal(); // should never get called } }
@Override public BigInteger getBigIntegerValue() throws IOException, JsonParseException { return currentNumericNode().getBigIntegerValue(); }
@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().getTextValue(); case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: return String.valueOf(currentNode().getNumberValue()); 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 getTextLength() throws IOException, JsonParseException { return getText().length(); }
@Override public long getLongValue() throws IOException, JsonParseException { return currentNumericNode().getLongValue(); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException { // Multiple possibilities... JsonNode n = currentNode(); if (n != null) { // binary node? byte[] data = n.getBinaryValue(); // (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 char[] getTextCharacters() throws IOException, JsonParseException { return getText().toCharArray(); }
@Override public int getIntValue() throws IOException, JsonParseException { return currentNumericNode().getIntValue(); }
@Override public JsonParser traverse() { return new TreeTraversingParser(this); }
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, can not use numeric value accessors"); } return n; }
@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).getBinaryValue(); } } } return null; }
@Override public int getTextLength() throws IOException, JsonParseException { return getText().length(); }
@Override protected void _handleEOF() throws JsonParseException { _throwInternal(); // should never get called } }
@Override public NumberType getNumberType() throws IOException, JsonParseException { JsonNode n = currentNumericNode(); return (n == null) ? null : n.getNumberType(); }
@Override public JsonParser treeAsTokens(JsonNode n) { return new TreeTraversingParser(n, this); }
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, can not 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().getTextValue(); case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: return String.valueOf(currentNode().getNumberValue()); 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 char[] getTextCharacters() throws IOException, JsonParseException { return getText().toCharArray(); }