@@ -584,27 +584,35 @@ public final String textValue() {
584
584
}
585
585
586
586
/**
587
- * Method that will return a valid String representation of
588
- * the contained value, if the node is a value node
589
- * (method {@link #isValueNode} returns true),
590
- * otherwise empty String.
587
+ * Method that will try to convert value of this node to a {@code String}.
588
+ * JSON Strings map naturally; other scalars map to their string representation
589
+ * (including Binary data as Base64 encoded String);
590
+ * JSON {@code null}s map to empty String.
591
+ * Other values (including structured types like Objects and Arrays, and "missing"
592
+ * value) will result in a {@link JsonNodeException} being thrown.
591
593
*<p>
592
594
* NOTE: this is NOT same as {@link #toString()} in that result is
593
- * <p>NOT VALID ENCODED JSON</p> for all nodes (but is for some, like
595
+ * <p>NOT VALID ENCODED JSON</p> for all nodes (although is for some, like
594
596
* {@code NumberNode}s and {@code BooleanNode}s).
597
+ *
598
+ * @return String representation of this node, if coercible; exception otherwise
599
+ *
600
+ * @throws JsonNodeException if node cannot be coerced to a {@code String}
595
601
*/
596
602
public abstract String asString ();
597
603
598
604
/**
599
- * Returns the text value of this node or the provided {@code defaultValue} if this node
600
- * does not have a text value. Useful for nodes that are {@link MissingNode} or
601
- * {@link tools.jackson.databind.node.NullNode}, ensuring a default value is returned instead of null or missing indicators.
602
- *
603
- * @param defaultValue The default value to return if this node's text value is absent.
604
- * @return The text value of this node, or {@code defaultValue} if the text value is absent.
605
+ * Similar to {@link #asString()}, but instead of throwing an exception for
606
+ * non-coercible values, will return specified default value.
605
607
*/
606
608
public abstract String asString (String defaultValue );
607
609
610
+ /**
611
+ * Similar to {@link #asString()}, but instead of throwing an exception for
612
+ * non-coercible values, will return {@code Optional.empty()}.
613
+ */
614
+ public abstract Optional <String > asStringOpt ();
615
+
608
616
/**
609
617
* @deprecated Use {@link #asString()} instead.
610
618
*/
@@ -672,29 +680,31 @@ public String asText(String defaultValue) {
672
680
public abstract Optional <Boolean > booleanValueOpt ();
673
681
674
682
/**
675
- * Method that will try to convert value of this node to a Java <b> boolean</b> .
676
- * JSON booleans map naturally; integer numbers other than 0 map to true, and
677
- * 0 maps to false
683
+ * Method that will try to convert value of this node to a Java {@code boolean} .
684
+ * JSON Booleans map naturally; Integer numbers other than 0 map to true, and
685
+ * 0 maps to false; {@code null} maps to false
678
686
* and Strings 'true' and 'false' map to corresponding values.
679
- *<p>
680
- * If representation cannot be converted to a boolean value (including structured types
681
- * like Objects and Arrays),
682
- * default value of <b>false</b> will be returned; no exceptions are thrown.
687
+ * Other values (including structured types like Objects and Arrays) will
688
+ * result in a {@link JsonNodeException} being thrown.
689
+ *
690
+ * @return Boolean value this node represents, if coercible; exception otherwise
691
+ *
692
+ * @throws JsonNodeException if node cannot be coerced to a Java {@code boolean}
683
693
*/
684
694
public abstract boolean asBoolean ();
685
695
686
696
/**
687
- * Method that will try to convert value of this node to a Java <b>boolean</b>.
688
- * JSON booleans map naturally; integer numbers other than 0 map to true, and
689
- * 0 maps to false
690
- * and Strings 'true' and 'false' map to corresponding values.
691
- *<p>
692
- * If representation cannot be converted to a boolean value (including structured types
693
- * like Objects and Arrays),
694
- * specified <b>defaultValue</b> will be returned; no exceptions are thrown.
697
+ * Similar to {@link #asBoolean()}, but instead of throwing an exception for
698
+ * non-coercible values, will return specified default value.
695
699
*/
696
700
public abstract boolean asBoolean (boolean defaultValue );
697
701
702
+ /**
703
+ * Similar to {@link #asBoolean()}, but instead of throwing an exception for
704
+ * non-coercible values, will return {@code Optional.empty()}.
705
+ */
706
+ public abstract Optional <Boolean > asBooleanOpt ();
707
+
698
708
// // Scalar access: Numbers, generic
699
709
700
710
/**
0 commit comments