@@ -541,18 +541,47 @@ public Optional<JsonNode> asOptional() {
541
541
// // Scalar access: Strings
542
542
543
543
/**
544
- * Method to use for accessing String values.
545
- * Does <b>NOT</b> do any conversions for non- String value nodes;
546
- * for non-String values (ones for which {@link #isString} returns
547
- * false) null will be returned.
548
- * For String values, null is never returned (but empty Strings may be)
544
+ * Method that will try to access value of this node as a Java {@code String}
545
+ * which works if (and only if) node contains JSON String value:
546
+ * if not, a {@link JsonNodeException} will be thrown.
547
+ *<p>
548
+ * NOTE: for more lenient conversions, use {@link #asString()}
549
549
*<p>
550
550
* NOTE: in Jackson 2.x, was {@code textValue()}.
551
551
*
552
- * @return String value this node contains, iff node is created from
553
- * a String value.
552
+ * @return {@code String} value this node represents (if JSON String)
553
+ *
554
+ * @throws JsonNodeException if node value is not a JSON String value
554
555
*/
555
- public String stringValue () { return null ; }
556
+ public abstract String stringValue ();
557
+
558
+ /**
559
+ * Method similar to {@link #stringValue()}, but that will return specified
560
+ * {@code defaultValue} if this node does not contain a JSON String.
561
+ *
562
+ * @param defaultValue Value to return if this node does not contain a JSON String.
563
+ *
564
+ * @return Java {@code String} value this node represents (if JSON String);
565
+ * {@code defaultValue} otherwise
566
+ */
567
+ public abstract String stringValue (String defaultValue );
568
+
569
+ /**
570
+ * Method similar to {@link #stringValue()}, but that will return
571
+ * {@code Optional.empty()} if this node does not contain a JSON String.
572
+ *
573
+ * @return {@code Optional<String>} value (if node represents JSON String);
574
+ * {@code Optional.empty()} otherwise
575
+ */
576
+ public abstract Optional <String > stringValueOpt ();
577
+
578
+ /**
579
+ * @deprecated Use {@link #asString()} instead.
580
+ */
581
+ @ Deprecated // since 3.0
582
+ public final String textValue () {
583
+ return stringValue ();
584
+ }
556
585
557
586
/**
558
587
* Method that will return a valid String representation of
@@ -595,29 +624,53 @@ public String asText(String defaultValue) {
595
624
// // Scalar access: Binary
596
625
597
626
/**
598
- * Method to use for accessing binary content of binary nodes (nodes
599
- * for which {@link #isBinary} returns true); or for String Nodes
600
- * (ones for which {@link #stringValue} returns non-null value),
601
- * to read decoded base64 data.
602
- * For other types of nodes, returns null.
627
+ * Method that will try to access value of this node as binary value (Java {@code byte[]})
628
+ * which works if (and only if) node contains binary value (for JSON, Base64-encoded
629
+ * String, for other formats native binary value): if not,
630
+ * a {@link JsonNodeException} will be thrown.
631
+ * To check if this method can be used, you may call {@link #isBinary()}.
632
+ *<p>
633
+ * @return Binary value this node represents (if node contains binary value)
603
634
*
604
- * @return Binary data this node contains, iff it is a binary
605
- * node; null otherwise
635
+ * @throws JsonNodeException if node does not contain a Binary value (a
606
636
*/
607
637
public abstract byte [] binaryValue ();
608
638
609
639
// // Scalar access: Boolean
610
640
611
641
/**
612
- * Method to use for accessing JSON boolean values (value
613
- * literals 'true' and 'false').
614
- * For other types, always returns false.
642
+ * Method that will try to access value of this node as a Java {@code boolean}
643
+ * which works if (and only if) node contains JSON boolean value: if not,
644
+ * a {@link JsonNodeException} will be thrown.
645
+ *<p>
646
+ * NOTE: for more lenient conversions, use {@link #asBoolean()}
647
+ *
648
+ * @return {@code boolean} value this node represents (if JSON boolean)
615
649
*
616
- * @return Boolean value this node contains, if any; false for
617
- * non-boolean nodes.
650
+ * @throws JsonNodeException if node does not represent a JSON boolean value
618
651
*/
619
652
public abstract boolean booleanValue ();
620
653
654
+ /**
655
+ * Method similar to {@link #booleanValue()}, but that will return specified
656
+ * {@code defaultValue} if this node does not contain a JSON boolean.
657
+ *
658
+ * @param defaultValue Value to return if this node does not contain a JSON boolean.
659
+ *
660
+ * @return Java {@code boolean} value this node represents (if JSON boolean);
661
+ * {@code defaultValue} otherwise
662
+ */
663
+ public abstract boolean booleanValue (boolean defaultValue );
664
+
665
+ /**
666
+ * Method similar to {@link #booleanValue()}, but that will return
667
+ * {@code Optional.empty()} if this node does not contain a JSON boolean.
668
+ *
669
+ * @return {@code Optional<Boolean>} value (if node represents JSON boolean);
670
+ * {@code Optional.empty()} otherwise
671
+ */
672
+ public abstract Optional <Boolean > booleanValueOpt ();
673
+
621
674
/**
622
675
* Method that will try to convert value of this node to a Java <b>boolean</b>.
623
676
* JSON booleans map naturally; integer numbers other than 0 map to true, and
0 commit comments