Skip to content

Commit 02ee69e

Browse files
committed
Fix #4991: add/update JsonNode.xxxValue() for non-number scalars
1 parent b06a588 commit 02ee69e

File tree

6 files changed

+79
-6
lines changed

6 files changed

+79
-6
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Versions: 3.x (for earlier see VERSION-2.x)
100100
#4956: Rename `JsonNode.isContainerNode()` as `isContainerNode()`
101101
#4958: Extend, improve set of number value accessors for `JsonNode`
102102
(`JsonNode.intValue()` etc) [JSTEP-3]
103+
#4991: Extend, improve set of non-number scalar value accessors for `JsonNode`
104+
(`JsonNode.booleanValue()` etc) [JSTEP-3]
103105
#4992: Rename `JsonNodeFactory.textNode()` as `JsonNodeFactory.stringNode()` [JSTEP-3]
104106
- Remove `MappingJsonFactory`
105107
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)

src/main/java/tools/jackson/databind/JsonNode.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,39 @@ public String asText(String defaultValue) {
612612
* Method to use for accessing JSON boolean values (value
613613
* literals 'true' and 'false').
614614
* For other types, always returns false.
615+
*
616+
* Method that will try to access value of this node as a Java {@code boolean}
617+
* which works if (and only if) node contains JSON boolean value: if not,
618+
* a {@link JsonNodeException} will be thrown.
619+
*<p>
620+
* NOTE: for more lenient conversions, use {@link #asBoolean()}
621+
*
622+
* @return {@code boolean} value this node represents (if JSON boolean)
615623
*
616-
* @return Boolean value this node contains, if any; false for
617-
* non-boolean nodes.
624+
* @throws JsonNodeException if node value is not a JSON boolean value
618625
*/
619626
public abstract boolean booleanValue();
620627

628+
/**
629+
* Method similar to {@link #booleanValue()}, but that will return specified
630+
* {@code defaultValue} if this node does not contain a JSON boolean.
631+
*
632+
* @param defaultValue Value to return if this node does not contain a JSON boolean.
633+
*
634+
* @return Java {@code boolean} value this node represents (if JSON boolean);
635+
* {@code defaultValue} otherwise
636+
*/
637+
public abstract boolean booleanValue(boolean defaultValue);
638+
639+
/**
640+
* Method similar to {@link #booleanValue()}, but that will return
641+
* {@code Optional.empty()} if this node does not contain a JSON boolean.
642+
*
643+
* @return {@code Optional<Boolean>} value (if node represents JSON boolean);
644+
* {@code Optional.empty()} otherwise
645+
*/
646+
public abstract Optional<Boolean> booleanValueOpt();
647+
621648
/**
622649
* Method that will try to convert value of this node to a Java <b>boolean</b>.
623650
* JSON booleans map naturally; integer numbers other than 0 map to true, and

src/main/java/tools/jackson/databind/node/BaseJsonNode.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,19 @@ public byte[] binaryValue() {
198198

199199
@Override
200200
public boolean booleanValue() {
201-
return false;
201+
return _reportCoercionFail("booleanValue()", Boolean.TYPE, "value type not boolean");
202+
}
203+
204+
@Override
205+
public boolean booleanValue(boolean defaultValue) {
206+
// Overridden by BooleanNode, for other types return default
207+
return defaultValue;
208+
}
209+
210+
@Override
211+
public Optional<Boolean> booleanValueOpt() {
212+
// Overridden by BooleanNode, for other types return default
213+
return Optional.empty();
202214
}
203215

204216
@Override

src/main/java/tools/jackson/databind/node/BooleanNode.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tools.jackson.databind.node;
22

3+
import java.util.Optional;
4+
35
import tools.jackson.core.*;
46
import tools.jackson.databind.SerializationContext;
57

@@ -18,6 +20,9 @@ public class BooleanNode
1820
public final static BooleanNode TRUE = new BooleanNode(true);
1921
public final static BooleanNode FALSE = new BooleanNode(false);
2022

23+
private final static Optional<Boolean> OPT_FALSE = Optional.of(false);
24+
private final static Optional<Boolean> OPT_TRUE = Optional.of(true);
25+
2126
private final boolean _value;
2227

2328
/*
@@ -61,14 +66,25 @@ protected String _valueDesc() {
6166
@Override
6267
public BooleanNode deepCopy() { return this; }
6368

69+
/*
70+
/**********************************************************************
71+
/* Overridden JsonNode methods, scalar access
72+
/**********************************************************************
73+
*/
74+
6475
@Override
6576
public boolean booleanValue() {
6677
return _value;
6778
}
6879

6980
@Override
70-
public String asString() {
71-
return _value ? "true" : "false";
81+
public boolean booleanValue(boolean defaultValue) {
82+
return _value;
83+
}
84+
85+
@Override
86+
public Optional<Boolean> booleanValueOpt() {
87+
return _value ? OPT_TRUE : OPT_FALSE;
7288
}
7389

7490
@Override
@@ -94,6 +110,17 @@ public double asDouble(double defaultValue) {
94110
return _value ? 1.0 : 0.0;
95111
}
96112

113+
@Override
114+
public String asString() {
115+
return _value ? "true" : "false";
116+
}
117+
118+
/*
119+
/**********************************************************************
120+
/* Overridden JsonNode methods, other
121+
/**********************************************************************
122+
*/
123+
97124
@Override
98125
public final void serialize(JsonGenerator g, SerializationContext provider)
99126
throws JacksonException {

src/main/java/tools/jackson/databind/node/IntNode.java

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ public boolean asBoolean(boolean defaultValue) {
150150
return _value != 0;
151151
}
152152

153+
/*
154+
/**********************************************************************
155+
/* Overridden JsonNode methods, other
156+
/**********************************************************************
157+
*/
158+
153159
@Override
154160
public final void serialize(JsonGenerator g, SerializationContext provider)
155161
throws JacksonException

src/test/java/tools/jackson/databind/node/NullNodeTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public void testBasicsWithNullNode() throws Exception
4545
assertFalse(n.canConvertToExactIntegral());
4646

4747
// fallback accessors
48-
assertFalse(n.booleanValue());
4948

5049
// may be odd but...
5150
assertEquals("null", n.asString());

0 commit comments

Comments
 (0)